2016-12-03 75 views
2

我已经在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(); 
    } 
} 
+0

你是怎么调用assertDisplayed方法的?我们可以看到代码吗? – CodingYoshi

+0

对不起,我已经添加了这个,谢谢。 – alex

+0

对不起,但代码甚至不会编译,因为assertDisplayed不是一个静态方法。 – CodingYoshi

回答

1

好吧,有几件事情需要改变。扩展方法有一些我们需要遵循的规则。规则是:

  1. 它必须在非泛型静态类。因此,该方法必须是静态的,因为您无法在静态类中使用实例方法。
  2. 它必须有一个参数,第一个参数必须有this关键字。第一个参数不能有outref
  3. 第一个参数不能是指针类型。

因此,请记住这些规则,让我们继续创建您需要的扩展方法。

namespace ParallelTests 
{ 
    public static class ExtensionMethods // I would call it ChromeDriverEntension 
    { 
     public static void AssertDisplayed(this IWebDriver driver) 
     { 
      Assert.IsTrue(driver.FindElement(By.XPath("//*[contains(text(),'Some Text')]")).Displayed); 
     } 
    } 
} 

以上是非专利静态类。它有一个参数,第一个参数有这个关键字。第一个参数是IWebDriver,因为这是我们正在扩展的。该方法也是静态的。

好吧,让我们继续使用它。

namespace ParallelTests 
{ 
    public class Base 
    { 
     public IWebDriver Driver { get; set; } 
    } 

    public class Hooks : Base 
    { 
     public Hooks() 
     { 
      Driver = new ChromeDriver(); 
     } 
    } 

    [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); 
      Driver.AssertDisplayed(); 
     } 
    } 
} 

编译器如何找到扩展方法?

当编译器注意到实例方法代码Driver.AssertDisplayed();,但没有满足签名的实例方法时,它会查找扩展方法。它搜索所有的命名空间来查找匹配。由于这个方法在上面的同一个命名空间中,它会找到它。如果它位于不同的名称空间中,则需要使用using A.B导入该名称空间,其中A.B是扩展方法所在的名称空间的名称。否则,编译器会产生一个错误,说它找不到这样的方法。

C#深度作者:Jon Skeet如果您想了解更多信息,请深入介绍扩展方法。

+0

谢谢,对于这一切都是新鲜事,但是当我这样做时,我得到'ParallelTests.Base.Driver'是一个'属性',但是像'type'一样使用,并且扩展方法必须是静态的。但是这不会起作用,因为'公共类ExtensionMethods:Hooks'需要是静态的,它不能像我所知道的那样。 – alex

+0

对不起,看到我的编辑。您需要将ChromeDriver传入其中。 – CodingYoshi

+0

谢谢,但我仍然''扩展方法必须在非公共类扩展方法:钩子'非泛型静态类中定义',正如我所提到的。 – alex