Headless Browser Testing – Selenium WebDriver
Headless browser testing is simply testing a web pages functionality, without a GUI present. There are various different browsers we could use for this, including:
- Chrome
- Firefox
- PhantomJS
- HtmlUnit
PhantomJS is the only driver that must be downloaded separately, the other three, come bundled in with their corresponding driver servers, or with Selenium as a whole.
Chrome Headless Browser
To test against Chromes built-in headless browser, we can use:
package headlesstests;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeHeadless {
WebDriver driver;
@Before
public void setUp() throws Exception {
//Instantiating ChromeOptions object
ChromeOptions options = new ChromeOptions();
//Adding "headless" argument to options
options.addArguments("headless");
//Passing the options into the ChromeDriver
driver = new ChromeDriver(options);
driver.get("https://www.edgewordstraining.co.uk/webdriver2/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void test() {
System.out.println("Title of the page is -> " + driver.getTitle());
driver.findElement(By.partialLinkText("Login")).click();
driver.findElement(By.id("username")).click();
driver.findElement(By.id("password")).click();
driver.findElement(By.partialLinkText("Sub")).click();
}
}
Firefox Headless Browser
To test against Firefox’x built-in headless browser, we can use:
package headlesstests;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class FirefoxHeadless {
WebDriver driver;
@Before
public void setUp() throws Exception {
//Instantiating FirefoxOptions object
FirefoxOptions options = new FirefoxOptions();
//Adding "--headless" argument to options
options.addArguments("--headless");
//Passing the options into the FirefoxDriver
driver = new FirefoxDriver(options);
driver.get("https://www.edgewordstraining.co.uk/webdriver2/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void test() {
System.out.println("Title of the page is -> " + driver.getTitle());
driver.findElement(By.partialLinkText("Login")).click();
driver.findElement(By.id("username")).click();
driver.findElement(By.id("password")).click();
driver.findElement(By.partialLinkText("Sub")).click();
}
}
PhantomJS Headless Browser
To test against PhantomJS headless browser, does require the PhantomJS driver, which can be downloaded from: http://phantomjs.org/
You can then use:
package headlesstests;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
public class PhantomJSHeadless {
WebDriver driver;
@Before
public void setUp() throws Exception {
//Instantiate PhantomJSDriver
driver = new PhantomJSDriver();
driver.get("https://www.edgewordstraining.co.uk/webdriver2/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void test() {
System.out.println("Title of the page is -> " + driver.getTitle());
driver.findElement(By.partialLinkText("Login")).click();
driver.findElement(By.id("username")).click();
driver.findElement(By.id("password")).click();
driver.findElement(By.partialLinkText("Sub")).click();
}
}
HtmlUnit Headless Browser
To test against Selenium’s built-in headless browser, we can use:
package headlesstests;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HtmlUnitHeadless {
HtmlUnitDriver driver = new HtmlUnitDriver(true);
@Before
public void setUp() throws Exception {
driver.get("https://www.edgewordstraining.co.uk/webdriver2/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void test() {
System.out.println("Title of the page is -> " + driver.getTitle());
driver.findElement(By.partialLinkText("Login")).click();
driver.findElement(By.id("username")).click();
driver.findElement(By.id("password")).click();
driver.findElement(By.partialLinkText("Sub")).click();
}
}
For more information regarding Selenium WebDriver, please see our: