C# Extending Selenium Webdriver Class C# Extending Selenium Webdriver Class selenium selenium

C# Extending Selenium Webdriver Class


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.

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.

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.


what if you do something like

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

execution

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