分组扩展方法

时间:2015-01-21 09:41:43

标签: c# .net selenium .net-4.5 extension-methods

我正在使用.NET 4.5VS2013Selenium

我正在为页面流结构和字段结构有些相似的产品编写selenium测试。

要设置我在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 - 实际上是连字 遗嘱 - 产品(网上旅程),
MainCustomer - 部分页面,
标题 - 字段,

对于下一个产品,我将LpaSetMainCustomerName和大多数其他字段使它变得非常混乱。

我想拥有的是

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

作为扩展方法

有没有办法实现分组扩展方法? (或者获得类似的东西,允许在进行扩展方法的同时进行良好的分组)。

2 个答案:

答案 0 :(得分:1)

不可能直接对这样的扩展方法进行分组。

在示例中

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

您可以制作每个都返回特殊类型的WillsMainCustomer方法。然后钻取的下一级(MainCustomerSetTitle)可以关闭这些类型。搜索“流利的风格”,看​​看我的意思。我不推荐这个。它创造了大量的工作来设置只是为了得到你想要的那个小时期角色。

你也可以包装驱动程序:

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

同样,你必须编写所有这些类和成员。

我可以建议以下命名约定:

Wills_MainCustomer_SetTitle

一个非常简单的解决方案,使其更具可读性。

如果这些方法的主体总是非常相似,请考虑使用T4模板生成这些方法的所有可能情况。

答案 1 :(得分: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;
    }
}

我应该将WillsElements拉进WillsDriver课程,这会让它更整洁。