Headless Selenium Example and Usage

What is Headless Mode Selenium? Advantage of Headless Selenium

Headless mode means making UI tests without opening a browser’s UI. For example, if your machine does not have a GUI, that time you can headless mode.

Advantages:

You can check tests or screen with screenshots. Chrome browser version must be higher than 59 and we need to set the desired screen size with an additional argument.

There is a two way to use headless mode.

First one headless with an argument “ — headless”

You need to import TestNG and Selenium library codes to your project’s pom.xml file. This code opens the browser and searches text on Google.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;

public class FirstChromeSearch {
public final String DRIVER_PATH = "Drivers/chromedriver";
public final String DRIVER_TYPE = "webdriver.chrome.driver";
public final String BASE_URL = "https://www.google.com/";
public final String CHROME_SEARCHBAR_XPATH = "/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input";
public final String SEARCH_WORD = "What is TestNG";
public WebDriver driver;

@BeforeTest
public void beforeTest() {

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless","--disable-notifications","--ignore-certificate-errors","--disable-extensions","--window-size=1024,700");
System.setProperty(DRIVER_TYPE, DRIVER_PATH);
driver = new ChromeDriver(options);
driver.get(BASE_URL);
}

@Test
public void OpenChromeAndSearchText() throws Exception {

WebElement element = driver.findElement(By.xpath(CHROME_SEARCHBAR_XPATH));
element.sendKeys(SEARCH_WORD);
element.submit();
System.out.println("Title of the page" + " " + driver.getTitle());
}

@AfterClass
public void CloseBrowser() throws Exception{
driver.quit();
}
}

The second one is “setHeadless()”

You can set this method as True, its means headless mode is open.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;

public class FirstChromeSearch {
public final String DRIVER_PATH = "Drivers/chromedriver";
public final String DRIVER_TYPE = "webdriver.chrome.driver";
public final String BASE_URL = "https://www.google.com/";
public final String CHROME_SEARCHBAR_XPATH = "/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input";
public final String SEARCH_WORD = "What is TestNG";
public WebDriver driver;

@BeforeTest
public void beforeTest() {

ChromeOptions options = new ChromeOptions();
System.setProperty(DRIVER_TYPE, DRIVER_PATH);
options.setHeadless(true);
driver = new ChromeDriver(options);
driver.get(BASE_URL);
}

@Test
public void OpenChromeAndSearchText() throws Exception {

WebElement element = driver.findElement(By.xpath(CHROME_SEARCHBAR_XPATH));
element.sendKeys(SEARCH_WORD);
element.submit();
System.out.println("Title of the page" + " " + driver.getTitle());
}

@AfterClass
public void CloseBrowser() throws Exception{
driver.quit();
}
}

When you run this code you should see this mode much faster.

Quality Assurance Engineer-Automation