WebDriver is a tool for automating web application testing, and it tests that everything is working fine. It aims to provide a friendly API that’s easy to explore and understand, easier to use. Which will help to make tests easier to read and maintain. It is not tied to any particular test framework, so it can be used equally well in a unit testing or from a plain ‘main’ method. So here we will start with WebDriver’s API.
Every supported browser has its own driver class implementation. Here we will have some examples on how to use those different browsers.
Firefox:
FirefoxDriver is part of WebDriver itself.
public void startFirefoxBrowser()
{
WebDriver driver = new FirefoxDriver();
driver.get("https://testingchronicle.wordpress.com/");
System.out.println(driver.getTitle());
driver.quit();
}
Internet explorer:
InternetExplorerDriver is a separate executable. We need to download this.We can get it from https://code.google.com/p/selenium/downloads/
public void startIEBrowser()
{
System.setProperty("webdriver.ie.driver",this.getClass().
getClassLoader().getResource("IEDriverServer.exe").getPath());
WebDriver driver = new InternetExplorerDriver();
driver.get("https://testingchronicle.wordpress.com/");
System.out.println(driver.getTitle());
driver.quit();
}
Chrome:
ChromeDriver is a separate driver executable, maintained by the Chromium team. Similarly we need to download this.We can get it from https://code.google.com/p/selenium/downloads/
public void startChromeBrowser()
{
System.setProperty("webdriver.chrome.driver", this.getClass().getClassLoader().getResource("chromedriver.exe").getPath());
WebDriver driver = new ChromeDriver();
driver.get("https://testingchronicle.wordpress.com/");
System.out.println(driver.getTitle());
driver.quit();
}
HTMLunit driver:
We dont need any driver executable for HTML unit driver.
public void htmlUnitDriver()
{
WebDriver driver = new HtmlUnitDriver();
driver.get("https://testingchronicle.wordpress.com/");
System.out.println(driver.getTitle());
driver.quit();
}
Remote driver:
We need to use RemoteWebDriver if we have to make use of Selenium Grid (Browsers which might be located elsewhere).
Using the RemoteWebDriver have some disadvantages, like external servlet needs to be running and it introduces extra latency to our tests.
public void remoteDriver() throws MalformedURLException
{
DesiredCapabilities cap = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL(
"http://localhost:4444/test/app"), cap);
driver.get("https://testingchronicle.wordpress.com/");
System.out.println(driver.getTitle());
driver.quit();
}
Happy Automating 🙂