1
votes

I have build a few tests using Selenium with FireFox. For this I used the FirefoxDriver:

private FirefoxDriver driver = new FirefoxDriver():

This works great, but now I want to be able to switch browsers, based on the users choice. I had hoped to use WebDriver as the type and then use a switch to determine which browser to use.

Unfortunately WebDriver gives the error: "Can not resolve symbol "WebDriver". Pretty much every example I find uses a WebDriver type specification though.

I have a reference to the WebDriver.dll in my project. And Chrome, Firefox and IE drivers are recognised. Just the generic driver is not. Can anyone tell me what I can do to make this work?

2
You can use a properties file to specify the browser name. Based on the browser name specified you can dynamically create a browser object of the specified browser using if else blocks in your driver class. Hope this helps cheers.. - Vinay
Have you imported WebDriver? import org.openqa.selenium.WebDriver; - Nathan Merrill
Hi MrTi, I am using c#, is import equivalent to using in my environment? If so then no: I have a few usings: using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.IE; But if I add OpenQA.Selenium.WebDriver I get the error stated above. It might be I am missing a reference, but I can not find what I need to reference other than the DLL's that I have. These are WebDriver.dll, WebDriver.Support.dll, ThoughtWorks.Selenium.Core.dll and Selenium.WebdriverBackedSelenium.dll. - Steve
Have a look at this and see if it's useful: github.com/Ardesco/Selenium-Maven-Template/blob/master/src/test/… (I know it's Java but the concept should hold) - Ardesco

2 Answers

0
votes

The various drivers all implement an interface called IWebDriver. However, you are using methods such as FindElementByClassName ...which is on the RemoteWebDriver. Although I'd advise you to drop this and use the basic .FindElement and pass in the type of selector (By) that you need, there is a way around it:

You will need a using for OpenQA.Selenium.Remote

private RemoteWebDriver driver;

You can then do:

driver = new FirefoxDriver();

or...

driver = new InternetExplorerDriver();

You will still have access to your underlying FindElementBy... methods.

I will also say you do not need the ThoughtWorks or WebDriverBackedSelenium libraries at all. Remove them.

0
votes

The best way to do this for Java is to use the WebDriver interface (or even RemoteWebDriver class) to define the web driver object as a generic type.

WebDriver driver = new XYZDriver();

Where XYZ is the name of the specific browser you need to use. The image below shows the type hierarchy of the WebDriver interface for each supported browser.

enter image description here

I am not sure what the OP meant by "switch" browsers, but I assume what the OP meant was to have the ability to choose different browsers at the beginning of a session. Although each web driver concretion has different requirements to initialize a web driver instance, the general concept on how to do this is something like this:

public WebDriver initializeBrowser(string browserName) throws Exception {
    WebDriver driver = null;
    switch (browser.toLowerCase()) {
        case "chrome":
          driver = createChromeDriver();
          break;
        case "firefox":
          driver = createFirefoxDriver();
          break;
        ...
        default:
          throw new Exception("Unsupported browser: " + browserName);
    }
    return driver;
}

Each createXXXDriver should encapsulate the process of creating a driver instance, like setting the system properties for the driver location, setting any driver options needed (i.e. set headless mode, start maximized or minimized, etc.), and finally calling the related constructor.

I used this approach on a previous project and worked like a charm. In my particular case, I passed the browser name using a VM parameter (-Dbrowser=xxx). Doing so, allowed me to, not only to run my tests with different browsers on my local box, but also to configure my CI/CD environment (Jenkins) to run the exact job with different parameters; one of which was the browser I wanted to use for a particular test run.