• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How To Run Selenium Test In Headless Mode in Chrome Browser

How To Run Selenium Test In Headless Mode in Chrome Browser

August 16, 2018 by Mukesh Otwani 13 Comments

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://learn-automation.com/”);

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.

 

Filed Under: Basic Selenium Tagged With: Headless Testing Selenium

Reader Interactions

Comments

  1. SoNick_RND says

    December 8, 2020 at 3:41 PM

    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..”

    Reply
  2. Anil Madishetty says

    August 8, 2020 at 8:14 PM

    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.

    Reply
    • Mukesh Otwani says

      August 9, 2020 at 3:13 PM

      Anil, many many thanks…:)

      Reply
  3. Vengatesan says

    May 8, 2019 at 6:14 PM

    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.

    Reply
    • Mukesh Otwani says

      May 8, 2019 at 10:32 PM

      Hi Vengatesan,

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

      Reply
  4. Sujatha Rani says

    December 7, 2018 at 7:57 PM

    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?

    Reply
    • Mukesh Otwani says

      December 11, 2018 at 10:13 AM

      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

      Reply
  5. supreet says

    November 28, 2018 at 12:53 PM

    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.

    Reply
    • Mukesh Otwani says

      November 29, 2018 at 9:36 AM

      Hi Supreet,

      Thanks for comments…:)

      Reply
  6. Shahrukh khaliq says

    November 22, 2018 at 10:33 PM

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

    Reply
    • Mukesh Otwani says

      November 29, 2018 at 9:41 AM

      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.

      Reply
  7. lena says

    October 11, 2018 at 7:53 PM

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

    Reply
    • Mukesh Otwani says

      October 12, 2018 at 1:03 PM

      Hi Lena,

      Thanks for your appreciation…:)

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?