2016-12-03 79 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