简体   繁体   English

C#扩展Selenium Webdriver类

[英]C# Extending Selenium Webdriver Class

I would like to add a static string property that will track the name of the current test running. 我想添加一个静态字符串属性,它将跟踪当前运行的测试的名称。 I figured the best way to go about this was to use the WebDriver since it is the only object that is carried throughout all of my page objects. 我认为最好的方法是使用WebDriver,因为它是我所有页面对象中唯一的对象。

Is there a way to extend the WebDriver class to add a string property that I can set? 有没有办法扩展WebDriver类以添加我可以设置的字符串属性?

EDIT: Since WebDriver uses the IWebDriver interface rather would I extend the interface perhaps? 编辑:由于WebDriver使用IWebDriver接口,我会扩展接口吗?

EDIT #2: Adding example of what I currently have to load my WebDriver: 编辑#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;
    }
}

Ok let's stop the half answer practice (That is the full implementation of generic IWebDriver) after that you can call all the regular methods like you use in standard driver + you have your additional CurrentTest variable. 好吧,让我们停止半答案练习(这是通用IWebDriver的完整实现)之后你可以调用你在标准驱动程序中使用的所有常规方法+你有额外的CurrentTest变量。

you can add more constructors for best compatibility. 您可以添加更多构造函数以获得最佳兼容性。

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");
    }
}

You will need to wrap the WebDriver using the "Decorator" design pattern. 您需要使用“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();
    }

    ...
}

And then pass in whichever driver you are using at the time. 然后传递你当时使用的任何一个驱动程序。

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

This way you are delegating the interface methods of IWebDriver to FirefoxDriver but can add whatever additions are appropriate. 这样您就可以将IWebDriver的接口方法委托给FirefoxDriver,但可以添加任何适当的附加内容。

what if you do something like 如果你喜欢做什么怎么办?

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

execution 执行

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

Just use a sub class that has WebDriver as its parent: 只需使用具有WebDriver作为其父级的子类:

public class MyWebDriver : WebDriver
{
    private string _currentTest;
}

Then you can use MyWebDriver everywhere and set _currentTest as needed. 然后你可以在任何地方使用MyWebDriver并根据需要设置_currentTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM