Reputation: 1029
Selenium has multiple classes providing mechanisms for certain browsers.
So there are the following drivers (I care about most): FireFoxDriver
, ChromeDriver
, InternetExplorerDriver
,
All of these, inherit from RemoteWebDriver
.
public class FireFoxDriver : RemoteWebDriver { ... }
public class ChromeDriver : RemoteWebDriver { ... }
public class InternetExplorerDriver : RemoteWebDriver { ... }
Now, I would like to provide additional functionality/helpers to each driver such as, for example, going to URL and executing the script:
public void Goto(string url)
{
Navigate().GoToUrl(url);
ExecuteScript("console.log('Using selenium');");
}
I want to have this functionality for every driver (either FireFox, Chrome and IE) so I could do:
var chrome = new ChromeDriver();
chrome.Goto("https://google.com/");
So yeah, what is the proper way to do that?
Basically, I could get it working with creating a BaseRemoteWebDriver
class, put my functionality in there and then create a BaseChromeDriver
, BaseFireFoxDriver
, BaseIEDriver
and inherit from BaseRemoteWebDriver
.
This way, I could perhaps achieve what I want, however I would endup having three, basically empty classes that inherit from BaseRemoteWebDriver
, containing only generated constructors.
Is this the only (valid) way to extend the classes in C#?
Upvotes: 0
Views: 33
Reputation: 1066
You could also use Extension Methods
.
Then you could write something like this:
public static class RemoteWebDriverExtensions
{
public static void Goto(this RemoteWebDriver driver, string url)
{
driver.Navigate().GoToUrl(url);
driver.ExecuteScript("console.log('Using selenium');");
}
}
And then call it like that:
RemoteWebDriver chromedriver = new ChromeDriver();
chromedriver.Goto("https://google.com");
Upvotes: 1