我已经在Selenium中配置了与Nunit并行运行的测试,但工作正常,但我不确定如何将自定义方法添加到混合中,而无需打开第二个浏览器实例并打破测试。Selenium使用扩展方法进行并行测试
我有基地:
namespace ParallelTests
{
public class Base
{
public IWebDriver Driver { get; set; }
}
}
...和钩:
public class Hooks : Base
{
public Hooks()
{
Driver = new ChromeDriver(@"D:\Data\user\Documents\Visual Studio 2012\Projects\ParallelTests\ParallelTests\bin");
}
}
...和单个测试文件:
[TestFixture]
[Parallelizable]
public class ChromeTesting: Hooks
{
[Test]
public void ChromegGoogleTest()
{
Driver.Navigate().GoToUrl("https://www.google.co.uk");
Driver.FindElement(By.Id("lst-ib")).SendKeys("Deep Purple");
Driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
}
}
运行此工作正常,但如果我添加自定义方法,请说:
public class ExtensionMethods : Hooks
{
public void assertDisplayed()
{
Assert.IsTrue(Driver.FindElement(By.XPath("//*[contains(text(),'Some Text')]")).Displayed);
}
}
并调用在测试assertDisplayed()
如:
[TestFixture]
[Parallelizable]
public class ChromeTesting: Hooks
{
[Test]
public void ChromegGoogleTest()
{
Driver.Navigate().GoToUrl("https://www.google.co.uk");
Driver.FindElement(By.Id("lst-ib")).SendKeys("Deep Purple");
Driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
ExtensionMethods.assertDisplayed();
}
}
当我在上面所示的测试呼叫assertDisplayed()
它将启动一个第二空白浏览器。任何帮助非常感谢。
现在的工作基础上的建议,但下面是网页对象模型,它再次启动第二个浏览器窗口...
页面文件的例子:
namespace ParallelTests
{
class PageObject_LoggedIn : Hooks
{
public PageObject_LoggedIn()
{
PageFactory.InitElements(Driver, this);
}
[FindsBy(How = How.XPath, Using = @"//*[contains(text(),'Deep Purple | Official Site')]")]
public IWebElement SearchText = null;
[FindsBy(How = How.Id, Using = "lst-ib")]
public IWebElement SearchBox = null;
public void Search()
{
SearchBox.SendKeys("Deep Purple");
SearchBox.SendKeys(Keys.Enter);
Driver.assertDisplayed2();
}
}
}
。 ..并呼吁测试... 测试代码:
[TestFixture]
[Parallelizable]
public class ChromeTesting: Hooks
{
[Test]
public void ChromegGoogleTest()
{
PageObject_LoggedIn loggedIn = new PageObject_LoggedIn();
Driver.Navigate().GoToUrl("https://www.google.co.uk");
loggedIn.Search();
}
}
你是怎么调用assertDisplayed方法的?我们可以看到代码吗? – CodingYoshi
对不起,我已经添加了这个,谢谢。 – alex
对不起,但代码甚至不会编译,因为assertDisplayed不是一个静态方法。 – CodingYoshi