Run Selenium Tests In Headless Google Chrome
Run Selenium Tests In Headless Google Chrome
Selenium is one of the most popular, open source and efficient browser automation tools available today. With Selenium Web Driver, it automates several (browser) actions, such as opening a webpage, clicking a link, checking the URL, and so on.
But there are cases when you may need to run automation tests in ‘headless’ mode, i.e., when no browser is being displayed. In these cases, you can execute Selenium tests in headless browsers. This is useful when testing in cloud Dockers/linux
What Is A Headless Browser?
A headless browser is a simulation module/browser which do not have user interface. These programs operate like any other browser, but do not display any UI. When Selenium tests are run, it executes in the backgroundBenefits Of Headless Browser Testing:
1. Improves speed and performance
Since this type of testing does not actually open a browser, the system saves the processing power that would otherwise be used in a real browser test. Consequently, the tests are executed faster.
2. Allows testing browserless setups
There may be setups where installing a browser is not possible(like linux, docker in cloud server etc), such as servers. In these cases, headless browsers help run automation tests easily.
3. Helps you multitask
Here, the executions happens at back-end without UI. This allows user to continue with other works in his machine without any disturbances when test execution is in progress
Run Selenium Tests In Headless Google Chrome
ChromeOptions is a class in Selenium to set the arguments to ChromeDriver. In the following example, we will pass the two arguments to ChromeDriver to run in headless mode.
Code snippet:
========================================================================
public class HeadlessTesting {
public static void main(String[] args) throws IOException{
System.setProperty("webdriver.chrome.driver",
"ChromeDriverPath");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1200x600");
WebDriver driver = new ChromeDriver(options);
driver.get("https://contentstack.built.io");
driver.get("https://www.google.co.in/");
System.out.println("title is: " + driver.getTitle());
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("pathTOSaveFile"));
driver.quit();
}
}
public static void main(String[] args) throws IOException{
System.setProperty("webdriver.chrome.driver",
"ChromeDriverPath");
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
options.addArguments("window-size=1200x600");
WebDriver driver = new ChromeDriver(options);
driver.get("https://contentstack.built.io");
driver.get("https://www.google.co.in/");
System.out.println("title is: " + driver.getTitle());
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("pathTOSaveFile"));
driver.quit();
}
}
========================================================================
-Happy Testing....
Comments
Post a Comment