In this tutorial, we will discuss Running Selenium Tests in Headless Mode. Selenium WebDriver supports headless mode with different browsers like Chrome, Firefox, and Edge. Safari WebDriver does not support headless mode.
What is the Headless mode in Selenium WebDriver?
In headless mode, we can run Tests Without a GUI
Why Headless mode and what are its advantages?
Headless mode can run faster than GUI in Selenium because it eliminates the overhead of running a visual browser, such as rendering web pages.
To run the test without the GUI
Headless mode in Chrome – Selenium Java Code Snippet
ChromeOptions options = new ChromeOptions(); options.setHeadless(true); WebDriver driver = new ChromeDriver(options);
Headless mode in Firefox – Selenium Java Code Snippet
FirefoxOptions options = new FirefoxOptions(); options.setHeadless(true); WebDriver driver = new FirefoxDriver(options);
Headless mode in Firefox – Selenium Java Code Snippet
EdgeOptions options = new EdgeOptions();
options.setHeadless(true);
WebDriver driver = new EdgeDriver(options);
Example:
package TestAutomationCentral;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Headless {
public static void main(String[] args) {
//TestAutomationCentral.com
System.setProperty("webdriver.chrome.driver", "C:\\temp\\drivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com/");
System.out.println(driver.getTitle());
}
}