2015-01-21 62 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类,这将使它更整洁。