C# 扩展 Selenium Webdriver Class

C# Extending Selenium Webdriver Class

我想添加一个静态字符串 属性 来跟踪当前测试的名称 运行。我认为解决此问题的最佳方法是使用 WebDriver,因为它是我所有页面对象中唯一携带的对象。

有没有办法扩展 WebDriver class 以添加我可以设置的字符串 属性?

编辑:由于 WebDriver 使用 IWebDriver 接口,我是否可以扩展接口?

编辑 #2:添加我当前必须加载我的 WebDriver 的示例:

protected static NLog.Logger _logger = LogManager.GetCurrentClassLogger();
protected static IWebDriver _driver;

/// <summary>
/// Spins up an instance of FireFox webdriver which controls the browser using a
/// FireFox plugin using a stripped down FireFox Profile.
/// </summary>
protected static void LoadDriver()
{
    ChromeOptions options = new ChromeOptions();
    try
    {
        var profile = new FirefoxProfile();
        profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream doc xls pdf txt");

        _driver = new FirefoxDriver(profile);
        _driver.Navigate().GoToUrl("http://portal.test-web01.lbmx.com/login?redirect=%2f");
    }
    catch(Exception e)
    {
        Console.WriteLine(e.Message);
        throw;
    }
}

只需使用以 WebDriver 作为其父级的子 class:

public class MyWebDriver : WebDriver
{
    private string _currentTest;
}

然后就可以到处使用MyWebDriver,根据需要设置_currentTest

如果你做类似的事情会怎么样

class MyWebDriver
{
   private IWebDriver driver;
   private static string CurrentTest;
   ....
   //make constractors / getters, setters
}

执行

MyWebDriver d = new MyWebDriver(....)
...

您需要使用 "Decorator" 设计模式包装 WebDriver。

public class MyWebDriver : IWebDriver
{
    private IWebDriver webDriver;
    public string CurrentTest { get; set; }

    public MyWebDriver(IWebDriver webDriver)
    {
        this.webDriver = webDriver
    }

    public Method1()
    {
        webDriver.Method1();
    }

    public Method2()
    {
        webDriver.Method2();
    }

    ...
}

然后传入你当时使用的驱动程序。

var profile = new FirefoxProfile();
MyWebDriver driver = new MyWebDriver(new FirefoxDriver(profile));

通过这种方式,您将 IWebDriver 的接口方法委托给 FirefoxDriver,但可以添加任何合适的内容。

好的,让我们停止半答案练习(即通用 IWebDriver 的完整实现),之后您可以像在标准驱动程序中使用的那样调用所有常规方法 + 您有额外的 CurrentTest 变量。

您可以添加更多构造函数以获得最佳兼容性。

class MyWebDriver<T> where T : IWebDriver, new()
{
    IWebDriver driver;
    public string CurrentTest { get; set; }

    public MyWebDriver()
    {
        driver = new T();
    }

    public void Dispose()
    {
        this.driver.Dispose();
    }

    public IWebElement FindElement(By by)
    {
        return this.driver.FindElement(by);
    }

    public ReadOnlyCollection<IWebElement> FindElements(By by)
    {
        return this.driver.FindElements(by);
    }

    public void Close()
    {
        this.driver.Close();
    }

    public void Quit()
    {
        this.driver.Quit();
    }

    public IOptions Manage()
    {
        return this.driver.Manage();
    }

    public INavigation Navigate()
    {
        return driver.Navigate();
    }

    public ITargetLocator SwitchTo()
    {
        return this.SwitchTo();
    }

    public string Url
    {
        get
        {
            return this.driver.Url;
        }
        set
        {
            this.driver.Url = value;
        }
    }

    public string Title
    {
        get
        {
            return this.driver.Title;
        }
    }

    public string PageSource
    {
        get
        {
            return this.driver.PageSource;
        }
    }

    public string CurrentWindowHandle
    {
        get
        {
            return this.driver.CurrentWindowHandle;
        }
    }

    public ReadOnlyCollection<string> WindowHandles
    {
        get
        {
            return this.WindowHandles;
        }
    }
}

public class MyTest
{
    public void main()
    {
        MyWebDriver<FirefoxDriver> driver = new MyWebDriver<FirefoxDriver>();
        driver.CurrentTest = "Entering to google website with Firefox Driver";
        driver.Navigate().GoToUrl("www.google.com");
    }
}