2015-01-21 60 views
2

我正在與.NET 4.5,VS2013Selenium一起工作。分組擴展方法

我正在爲其頁面流結構和字段結構有點類似的產品編寫硒測試。

將值設置爲我使用的擴展方法的領域上IWebDriver

例子:

private void WillsCustomerDetail(IWebDriver driver) 
    { 
     driver.WillsSetMainCustomerTitle("Dr"); 
     driver.WillsSetMainCustomerGender("Male"); 
     driver.WillsSetMainCustomerName("Liufa"); 
     driver.WillsSetMainCustomerSurname("Afuil"); 
     driver.WillsSetMainCustomerDateOfBirth(new DateTime(1955, 12, 26)); 
     ... 
    } 

    public static IWebElement WillsSetMainCustomerName(this IWebDriver driver, string value) 
    { 
     return driver.SetText(value, WillsElements.CustomerDetails.MainCustomer.Firstname); 
    } 

    public static IWebElement SetText(this IWebDriver driver, string value, string selector) 
    { 
     var element = driver.FindElement(By.CssSelector(selector)); 
     element.SendKeys(value); 
     return element; 
    } 

    public static class WillsElements 
    { 
     public static class CustomerDetails 
     { 
      public static class MainCustomer 
      { 
       public static string Title 
       { 
        get { return "#Customers-0--Title"; } 
       } 
      ... 
      } 
     } 
    } 

什麼我有問題就是我的方法命名

WillsSetMainCustomerTitle - 在現實中是串聯
Wills - product(web journey),
MainCustomer - partial page,
標題 - 字段,

和下一個產品我將有LpaSetMainCustomerName和大多數其他領域,使它成爲一個巨大的混亂。

我想有是

driver.Wills.MainCustomer.SetTitle("Dr"); 

爲擴展方法

是否有實現分組擴展方法的一種方式? (或者獲得類似的東西,這樣可以在仍然有擴展方法的情況下進行良好的分組)。

+0

爲什麼不能擴展'MainCustomer'的類型? – haim770 2015-01-21 09:51:07

+0

'WillsElements'的字段是什麼? – Codor 2015-01-21 09:51:46

+0

@ haim770我可以,但是我必須將'IWebDriver'傳入每個調用'Wills.MainCustomer.SetTitle(driver,「Dr」); '或者我誤解了你的問題? – 2015-01-21 09:56:50

回答

1

它不是直接可以將這樣的擴展方法分組。

在這個例子中

driver.Wills.MainCustomer.SetTitle("Dr"); 

可以使WillsMainCustomer方法,每個都返回一個特殊類型。鑽入的下一個層次(MainCustomerSetTitle)可以關閉這些類型。尋找「流利的風格」來看看我的意思。我不推薦這裏。它創造了大量的工作,只是爲了在你想要的地方得到那個小小的角色。

你也可以換司機:

new WillsDriver(driver) 
.MainCustomer 
.Title = "Dr"; 

同樣,你必須寫所有的類和成員。

我可以建議如下命名約定:

Wills_MainCustomer_SetTitle 

一個非常簡單的解決方案,使這個更具可讀性。

如果這些方法的主體總是非常相似,請考慮使用T4模板來生成這些方法的所有可能情況。

+0

我還沒有想過打包驅動程序。先生你是個天才!謝謝。 – 2015-01-21 10:02:23

0

對於那些想知道我結束了什麼。

[Test] 
    [TestCaseSource("WillsTestData")] 
    public void WillsTest(IWebDriver driver) 
    { 
     driver.Navigate().GoToUrl(WillsNavigation.Index); 
     var willsDriver = new WillsDriver(driver); 
     this.WillsCustomerDetail(willsDriver); 
     ... 
    } 

    private void WillsCustomerDetail(WillsDriver driver) 
    { 
     driver.CustomerDetail.MainCustomer.Title = "Dr"; 
     driver.CustomerDetail.MainCustomer.Gender = "Male"; 
     driver.CustomerDetail.MainCustomer.Name = "Liufa"; 
     driver.CustomerDetail.MainCustomer.Surname = "Afuil"; 
     driver.CustomerDetail.MainCustomer.DateOfBirth = new DateTime(1955, 12, 26); 
     ... 
     driver.CustomerDetail.ClickContinue(); 
    } 

public class WillsDriver 
{ 
    public WillsDriver(IWebDriver driver) 
    { 
     this.Driver = driver; 
     this.Index = new IndexClass(driver); 
     this.Quote = new QuoteClass(driver); 
     this.CustomerDetail = new CustomerDetailsClass(driver); 
    } 

    public CustomerDetailsClass CustomerDetail { get; private set; } 
    public IndexClass Index { get; private set; } 
    public QuoteClass Quote { get; private set; } 
    public IWebDriver Driver { get; set; } 
    .... 

    public class CustomerDetailsClass 
    { 
     public CustomerDetailsClass(IWebDriver driver) 
     { 
      this.Driver = driver; 
      this.MainCustomer = new MainCustomerClass(driver); 
      this.SecondCustomer = new SecondCustomerClass(driver); 
     } 

     public IWebDriver Driver { get; set; } 

     public MainCustomerClass MainCustomer { get; private set; } 

     public SecondCustomerClass SecondCustomer { get; private set; } 
     .... 
     public class MainCustomerClass 
     { 
      public MainCustomerClass(IWebDriver driver) 
      { 
       this.Driver = driver; 
      } 

      public IWebDriver Driver { get; set; } 

      public string Title 
      { 
       set 
       { 
        this.Driver.SetDropDown(value, WillsElements.CustomerDetails.MainCustomer.Title); 
        if (value == "Dr") Thread.Sleep(700); 
       } 
      } 
     .... 
     } 
} 

public class WillsElements 
{ 
    public static class CustomerDetails 
    { 
     public static class MainCustomer 
     { 
      public static string Title 
      { 
       get { return "#Customers-0--Title"; } 
      } 

      public static string Gender 
      { 
       get { return "#Customers-0--GenderSection-Gender"; } 
      } 

      public static string Firstname 
      { 
       get { return "#Customers-0--Firstname"; } 
      } 
    .... 
    } 
} 

public static class SetWillElement 
{ 
    public static IWebElement SetDropDown(this IWebDriver driver, string value, string selector) 
    { 
     var element = driver.FindElement(By.CssSelector(selector)); 
     var select = new SelectElement(element); 
     select.SelectByText(value); 
     return element; 
    } 

    public static string YesNoToCssSelector(this string name, string value) 
    { 
     return string.Format("{0}[value='{1}']", name, value == "Yes" ? "True" : "False"); 
    } 

    public static IWebElement ClickButton(this IWebDriver driver, string selector) 
    { 
     var element = driver.FindElement(By.CssSelector(selector)); 
     element.Click(); 
     return element; 
    } 

    public static IWebElement SetRadio(this IWebDriver driver, string value, string selector) 
    { 
     var element = driver.FindElement(By.CssSelector(selector)); 
     ((IJavaScriptExecutor) driver).ExecuteScript("arguments[0].checked = true;", element); 
     return element; 
    } 

    public static IWebElement SetText(this IWebDriver driver, string value, string selector) 
    { 
     var element = driver.FindElement(By.CssSelector(selector)); 
     element.SendKeys(value); 
     return element; 
    } 
} 

我也許應該拉WillsElementsWillsDriver類,這將使它更整潔。