Selenium testing without browser.


We can perform Selenium testing without a browser. This is achieved by triggering the execution in a headless mode. The headless execution can decrease the utilization of key resources and is being adopted widely.

For triggering headless execution in Chrome, the ChromeOptions class is utilized to modify the default browser characteristics. Headless is passed as a parameter to the addArguments.

Syntax

ChromeOptions opt = new ChromeOptions();
opt.addArguments("headless");
WebDriver d = new ChromeDriver(opt);

Example

Code Implementation.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.concurrent.TimeUnit;
public class WithoutBrowsr{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      //ChromeOptions object
      ChromeOptions opt = new ChromeOptions();
      //headless parameter
      opt.addArguments("headless");
      // set parameter to Chrome driver
      WebDriver driver = new ChromeDriver(opt);
      driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
      driver.get("https://www.tutorialspoint.com/questions/index.php");
      // obtain page title
      System.out.println("Page title without browser: " + driver.getTitle());
      driver.quit();
   }
}

Output

Updated on: 28-Dec-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements