Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 分组扩展方法_C#_.net_Selenium_.net 4.5_Extension Methods - Fatal编程技术网

C# 分组扩展方法

C# 分组扩展方法,c#,.net,selenium,.net-4.5,extension-methods,C#,.net,Selenium,.net 4.5,Extension Methods,我正在使用.net4.5、VS2013和Selenium 我正在为页面流结构和字段结构有些相似的产品编写selenium测试 在IWebDriver 例如: private void WillsCustomerDetail(IWebDriver driver) { driver.WillsSetMainCustomerTitle("Dr"); driver.WillsSetMainCustomerGender("Male"); dr

我正在使用
.net4.5
VS2013
Selenium

我正在为页面流结构和字段结构有些相似的产品编写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
——实际上是
遗嘱-产品(网络旅行),
主客户-部分页面,
标题-字段

对于下一个产品,我将有
LpaSetMainCustomerName
和大多数其他字段,这些字段将使它变得一团糟

我想要的是

driver.Wills.MainCustomer.SetTitle("Dr"); 
作为一种扩展方法


有没有实现分组扩展方法的方法?(或者得到类似的东西,这样在仍然有扩展方法的情况下可以进行很好的分组)。

这样的扩展方法分组是不可能的

在这个例子中

driver.Wills.MainCustomer.SetTitle("Dr"); 
您可以创建
Wills
MainCustomer
方法,每个方法都返回一个专用类型。然后,钻取的下一级(
MainCustomer
SetTitle
)可以关闭这些类型。搜索“流畅风格”来了解我的意思。我不推荐这个。它创建了大量的工作来设置,只是为了得到你想要的那个小句号角色

您还可以包装驱动程序:

new WillsDriver(driver)
 .MainCustomer
 .Title = "Dr";
同样,您必须编写所有这些类和成员

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

Wills_MainCustomer_SetTitle
这是一个非常简单的解决方案,可以提高可读性


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

对于那些我想知道最终结果的人。

    [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
类中,这样会更整洁

为什么您不能扩展
MainCustomer
的类型?WillsElements的字段是什么?@haim770我可以,但是我必须将
IWebDriver
传递到每个调用
Wills.MainCustomer.SetTitle(driver,“Dr”)
或者我误解了你的问题?@Codor WillsElements是一个静态类,里面有更多的静态类,它为我提供了一个很好的结构,可以将css选择器保存到页面上所有可用的元素。这很方便。正如您所能想象的,每个可选元素都有很多。再多一些`公共静态类willsements{公共静态类Index{公共静态字符串WillsQuoteType{get{return“#WillsQuoteType”}}公共静态字符串MaritalStatus{get{return“#MaritalStatus”}`我还没想到把司机包起来。先生,你真是个天才!谢谢。