Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Two Great Web Drivers to Use With Selenium

DZone's Guide to

Two Great Web Drivers to Use With Selenium

Read on to learn the basics of two popular web drivers supported by Selenium that can allow you to create awesomely dynamic web pages.

· Web Dev Zone ·
Free Resource

Classify programming languages: Build a model that predicts a code's programming language based on its text.  Content provided by IBM Developer. 

Headless Browser in HtmlUnit Driver

HtmlUnit Driver is getting famous because of its lightweight and fast features. It is also known as a headless browser driver. It does not provide any GUI while executing programs.

Some Examples of Headless Drivers Are:

  • HtmlUnit
  • PhantomJS
  • ZombieJS
  • Watir-webdriver
  • Ghost

Advantages of HtmlUnit Driver

  • Lack of GUI, which is why it takes less time and memory to run.
  • Works fast compared to Google Chrome, Firefox or Safari.
  • It supports Java and JavaScript.
  • Easy to use and understand

Disadvantages of HtmlUnit Driver

  • Difficult to perform user interface testing.
  • Exhaustive testing is impossible using this driver.

Code Snippet

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class Test {

public static void main(String[] args) throws InterruptedException 
{
WebDriver driver = new HtmlUnitDriver();
driver.get("http://softwaretestingbooks.com/");
System.out.println(driver.getTitle());
driver.quit();
}
}

Also, check:

Basic Login Script Using Selenium Webdriver

Multi Browser or Cross Browser Testing in Selenium WebDriver


Using Gecko Driver in Selenium 3.0

If you are using Firefox 47 or above, then Gecko Driver is required to run scripts in Selenium Webdriver.

If you are using Selenium version 2 or below, you don’t need Gecko Driver. You can download selenium-server-standalone-2.53.1.jar from here.

Without Gecko Driver:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test 
{
public static void main(String[] args) 
{
WebDriver driver = new FirefoxDriver();

driver.get("http://www.google.com");

driver.quit();
}
}

Output :

Throws an exception “java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;”

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:112)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:302)
at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:312)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:272)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:267)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:263)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:122)
at Test.main(Test.java:8)

Using Gecko Driver in Selenium 3.0:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {

public static void main(String[] args) throws InterruptedException 
{
System.setProperty("webdriver.gecko.driver", "E:\\selenium\\geckodriver.exe"); //("webdriver.gecko.driver","Write path of Gecko Driver exe")
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println("Code Run Successfully");
Thread.sleep(2000);
driver.quit();
}

}

Output:

Image title

Start coding something amazing with our library of open source Cloud code patterns. Content provided by IBM.

Topics:
selenium ,webdriver ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}