Basic Selenium

How To Run Selenium Test In Headless Mode in Chrome Browser

Run Selenium Test In Headless Mode

Headless Testing is very useful in Test Automation and Selenium Webdriver also supports now Headless Testing. Previously we have used HTMLUnitDriver and PhantomJS driver Run Selenium Test In Headless Mode.

In case you are not familiar with Headless Mode then in just line I can say that “Test will run without GUI” it means test will be running in background and you will get the result after execution.

 

Run Selenium Test In Headless Mode

 

Now Current Chrome Browser and Firefox Browser also supports Headless mode.In order to run your test in Headless mode make sure you use latest Chrome browser and latest Chrome Driver as well..

Program 1- Run Selenium Test In Headless Mode

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;

public class ChromeHeadlessBrowser {

@Test
public void myfirst()
{
// Set the chrome driver
System.setProperty(“webdriver.chrome.driver”,”./chromedriver.exe”);

// Create Object of ChromeOption Class
ChromeOptions option=new ChromeOptions();

//Set the setHeadless is equal to true which will run test in Headless mode
option.setHeadless(true);

// pass the option object in ChromeDriver constructor
WebDriver driver=new ChromeDriver(option);

// Provide any url
driver.get(“https://vistasadprojects.com/mukeshotwani-blogs-v2/”);

System.out.println(“Title is “+driver.getTitle());

// Close the instance
driver.quit();

}

}

we also have another approach where we can set argument and then pass the options class object to ChromeDriver constructor. I found  a very interesting blog where author has mentioned many arguments which we can pass in ChromeOption and many of them are useful for our Selenium Test as well.

Recently I also published one YouTube video which talks about ChromeOption in Selenium and Different Usage of the same.

Another approach to  Run Selenium Test In Headless Mode

// Create Object of ChromeOption Class
ChromeOptions option=new ChromeOptions();

//add the –headless argument in option class which will run test in Headless mode
option.addArguments(“–headless”);

// pass the option object in ChromeDriver constructor
WebDriver driver=new ChromeDriver(option);

Selenium Test in Headless Mode for Firefox ?

Note- Same thing will be application for Firefox as well so need to make much Changes in Code in First Program just change ChromeDriver to FirefoxDriver and rest process will remain same. FirefoxDriver required Gecko Driver now so make sure you use Latest Gecko driver to work Firefox in Headless mode.

I also have detailed article and video where we discussed how to use Gecko driver in Selenium

Before we end let’s talk about screenshot as well, you must be thinking if test is running in headless mode then how it will capture screenshot right? Selenium already handled this so no need to change the code for screenshot. You can use old way to capture screenshot and it will work in same way.

I have detailed article and video where we discussed how to capture screenshot and capture screenshot only when test fails.

Hope you enjoyed this article if yes then share with your friends and let me know your feedback in comment section.

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

13 thoughts on “How To Run Selenium Test In Headless Mode in Chrome Browser

  1. SoNick_RND says:

    Hi, please, add one space between “Headless mode.” and “In order”
    and remove extra dot after “as well.”
    “Now Current Chrome Browser and Firefox Browser also supports Headless mode.In order to run your test in Headless mode make sure you use latest Chrome browser and latest Chrome Driver as well..”

  2. Anil Madishetty says:

    Hi Mukesh, information given in this blog is very systematic and clear with examples. Thanks for sharing this type of information, it is so useful for me. great work keep it up.

    Thanks.

    1. Anil, many many thanks…:)

  3. Vengatesan says:

    Hi Mukesh,
    I am using @FindBy() annotation with PageFactory util in my page object model framework. I am unable to run my automation code headless. I tired htmlunitdriver & as per your example.
    I am getting null while find the element from the page.

    1. Hi Vengatesan,

      NullPointerException is most common exception and appears when decalared object doesn’t get initialize. Kindly debug your code.

  4. Sujatha Rani says:

    Hi Mugesh,

    I am just starting with selenium. While I am trying to launch the chrome driver in eclipse, I am not able to get the import the selenium while placing on webDriver & ChromeDriver.

    Could you please help me on this?

    1. Hi Sujata,

      please add below jar in your project and try again https://bit.ly/2TlkRyu

      Note- Use Java 8 in order to work with Selenium.

      Thanks
      Mukesh Otwani

  5. supreet says:

    hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up.

    1. Hi Supreet,

      Thanks for comments…:)

  6. Shahrukh khaliq says:

    How can we define headless public so that child classes also access it

    1. Hi Shahrukh,

      Yes ,you can define driver as public or protected or default. It depends on how much visibility you want to provide for driver instance.

  7. lena says:

    Thank you, sir lot of information there in your blog keeps going.

    1. Hi Lena,

      Thanks for your appreciation…:)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.