Chrome Headless – Execute Automation Script in Chrome Headless Browser

How to Run Automation Test on Chrome Headless Browser?

Hello Friends! Welcome to our Selenium Tutorial. In this tutorial, we will learn how to execute automation script in Chrome Headless Browser, what is the benefit of it is and step by step guide for implementation along with Code and their explanation.

What is Chrome Headless Browser?

Google Chrome launched Chrome Headless browser since Google Chrome 59. This browser is extensively popular for Selenium Automation Test Execution and server environments where you need not visible UI shell. During Headless Execution browser will not open but complete execution will happen as similar to a real browser.

Benefits of Headless Execution

Automation script execution preferred on Chrome Headless mode because of following reason.

  1. Quick execution as compared to real browser
  2. No user interruption as it runs in the backend

Prerequisite: 

  1. Java should be installed on the machine(if not then refer link- step by step guide for java installation)
  2. Selenium Project should be set up on your machine( if not then refer link – step by step guide to create selenium project)

Step 1: 

Open Selenium Project and create a new class for Chrome Headless browser test.

public class ChromeHeadless {

}

Step 2:

Write method and use ChromeOptions and pass “headless” in an argument. ChromeOptions used to configure browser properties. In this example, we will configure browser so that it will run in headless mode.

 public static void main(String args[])

    {

        WebDriverManager.chromedriver().setup();

        // Use Chrome Options pass arguments “headless”  for headless method       

ChromeOptions options = new ChromeOptions();

        options.addArguments(“headless”);

        options.addArguments(“window-size=1200×600”);

    }

}

Step 3:

Invoke Webdriver,  navigate to website and get title and print:

WebDriver driver = new ChromeDriver(options);

driver.manage().window().maximize();

driver.get(“https://sdethub.blogspot.com/”);

String title = driver.getTitle();

System.out.println(“Website Title: “+title);

driver.close();

Step 4:

Now complete code and execute. You will find browser will not open but your test completed. Refer below for compete code.

Complete Code:

import io.github.bonigarcia.wdm.WebDriverManager;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

public class ChromeHeadless {

    public static void main(String args[])

    {

        WebDriverManager.chromedriver().setup();

        // Use Chrome Options pass arguments “headless”  for headless method                       

ChromeOptions options = new ChromeOptions();

        options.addArguments(“headless”);

        options.addArguments(“window-size=1200×600”);

        WebDriver driver = new ChromeDriver(options);

        driver.manage().window().maximize();

        driver.get(“https://sdethub.blogspot.com/”);

        String title = driver.getTitle();

        System.out.println(“Website Title: “+title);

        driver.close();

    }

}

Console Output:

Summary:

i. Use ChromeOptions and add arguments “headless” in options object.

  options.addArguments(“headless”);

ii. Add window size as per your application compatibility.

    options.addArguments(“window-size=1200×600”);

Congratulations

Now you learned how to run an automation script on Chrome Headless browser and implemented code. If you like this tutorial then please write feedback on thoughtcoders2020@gmail.com also follow us on social media. To learn more about Selenium refer to our other tutorials.

0 0 vote
Article Rating
close

Subscribe to our Newsletter:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Open chat
Feel free to contact