• Selenium Video Tutorials

Selenium Webdriver - Headless Execution



Selenium Webdriver can be used for headless execution of tests on any browser. A headless execution is the one in which the test execution takes place without the browser being visible.

Headless execution is preferred now, since execution of test cases are faster there as the web pages are not rendered during this process. Also, system resources are less required while executing tests in headless mode. Headless execution is mostly preferred while we are running tests to scrape data from a web application.

Also, in case we are triggering a parallel execution involving multiple browsers, resource utilization is limited if we are doing a headless execution.

However, we would not be able to perform live debugging of the tests and reporting an issue to the developers on the failure tests using the headless mode of execution.

Let us take an example of the below page, where we would launch an application with a URL in a headless mode using Selenium version above 4.10 −

Then, we would get its page title: Selenium Practice - Student Registration Form. Next we would enter the text Selenium in the input box beside the Name label.

Selenium Headless Execution 1

Then, we would click on the Login button, after which we would be navigated to another page having a browser title as Selenium Practice - Login.

Selenium Headless Execution 2

Syntax

Syntax on headless execution −

// setting Chrome options to browser in headless mode
ChromeOptions opt = new ChromeOptions();
opt.addArguments("--headless=new");

// Initiate the Webdriver
WebDriver driver = new ChromeDriver(opt);

// Opening the webpage
driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

// getting page title
System.out.println("Getting the page title: " + driver.getTitle());

// identify edit box then enter text
WebElement e = driver.findElement(By.name("<value of name>"));
e.sendKeys("Selenium");

// get test entered
System.out.println("Value entered: " + e.getAttribute("value"));

// perform click
WebElement b = driver.findElement(By.xpath("<value of xpath>"));
b.click();

// getting page title after click
System.out.println("Getting the current title after click: " + driver.getTitle());

Example

Code Implementation on Headless.java class file.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeOptions;

public class Headless {
   public static void main(String[] args) throws InterruptedException {
      
      // setting Chrome options to browser in headless mode
      ChromeOptions opt = new ChromeOptions();
      opt.addArguments("--headless=new");

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 20 secs
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // identify edit box then enter text
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));
      e.sendKeys("Selenium");

      // get test entered
      System.out.println("Value entered: " + e.getAttribute("value"));

      // perform click
      WebElement b = driver.findElement(By.xpath("//*[@id='collapseTwo']/div/ul/li[2]/a"));
      b.click();

      // getting page title after click
      System.out.println("Getting the current title after click: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

Dependencies added in pom.xml file −

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
   </dependencies>
</project>

Output

Getting the page title: Selenium Practice - Student Registration Form
   Value entered: Selenium
Getting the current title after click: Selenium Practice - Login

Process finished with exit code 0

In the above example, we had launched a URL in headless mode on a browser and obtained the browser page title with the message in the console - Getting the page title: Selenium Practice - Student Registration Form. Next, entered the text Selenium in an input box and retrieved the value entered with the message in the console - Value entered: Selenium. Then, we clicked the Login and got the current page title after navigation with the message in console: Getting the current title after click: Selenium - Selenium Practice - Login.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Let us take an example of the below page, where we would launch an application with another URL in a headless mode Selenium version above 4.10 and obtain its page title - Selenium Practice - Date Picker.

Selenium Headless Execution 3

Syntax

// setting Chrome options to browser in headless mode
ChromeOptions opt = new ChromeOptions();
opt.addArguments("--headless=new");

// Initiate the Webdriver
WebDriver driver = new ChromeDriver(opt);

// Opening the webpage
driver.get("https://www.tutorialspoint.com/selenium/practice/date-picker.php");

// getting page title
System.out.println("Getting the page title: " + driver.getTitle());

Example

Code Implementation on HeadlessBrow.java class file.

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeOptions;

public class HeadlessBrow {
   public static void main(String[] args) throws InterruptedException {

      // setting Chrome options to browser in headless mode
      ChromeOptions opt = new ChromeOptions();
      opt.addArguments("--headless=new");

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver(opt);

      // adding implicit wait of 20 secs
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/date-picker.php");

      // getting page title
      System.out.println("Getting the page title: " + driver.getTitle());

      // Quitting browser
      driver.quit();
   }
}

Dependencies added in pom.xml file −

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
   </dependencies>
</project>

Output

Getting the page title: Selenium Practice - Date Picker

Process finished with exit code 0

In the above example, we had launched a URL in headless mode on a browser and obtained the browser page title with the message in the console - Getting the page title: Selenium Practice - Date Picker.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

In this tutorial, we had discussed how to perform headless execution using Selenium Webdriver.

Advertisements