微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#扩展Selenium Webdriver类

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

有没有一种方法可以扩展WebDriver类以添加可以设置的字符串属性

编辑:由于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.

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,但是可以添加任何适当的附加内容.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐