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 an automation script in Chrome Headless Browser, what 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 the 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 is preferred on Chrome Headless mode because of the following reasons.
- Quick execution as compared to real browser
- Another key point is no user interruption as it runs in the backend
Prerequisite:
- Java should be installed on the machine(if not then refer link- step by step guide for java installation)
- 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=1200x600"); } }
Step 3: Invoke Webdriver, navigate to website and get title and print:
WebDriver driver = new ChromeDriver(options); driver.manage().window().maximize(); driver.get("https://thoughtcoders.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=1200x600"); 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 at info@thoughtcoders.com also follow us on social media. To learn more about Selenium refer to our other tutorials.