Headless Testing via ChormeOptions Class.

Headless Testing means to run the headless browser while automation script execution. While running a headless browser user won’t be able to see the browser.

Headless Testing – Benefits

  1. A developer or user can use their machine while a script is running and can utilize this time for other tasks.
  2. The performance and script execution speed gets increased as it reduces the load time of the browser and all the web elements. 20-30% execution time can be saved via using headless testing.

The implementation is very simple and can be done in two steps:

  1. Create an object of ChromeOptions class.
  2. Use addArguments(List<String>) method to set the specific properties.

We can use List object or multiple arguments via comma separated.

Code Example

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
		
		  ChromeOptions options=new ChromeOptions(); 
		  
		  // Run the Browser in headless mode and user cann't see the browser.
		  options.addArguments("headless");
 WebDriver driver = new ChromeDriver(options);
String multipleDDUrl = "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple";
		driver.get(multipleDDUrl);
		System.out.println("Browser Title: "+driver.getTitle());

		driver.switchTo().frame("iframeResult");

		WebElement multDDElement = driver.findElement(By.xpath("//select[@id='cars']"));
		Select select1 = new Select(multDDElement);

		// Verify if user can select multiple option in dropdown.
		System.out.println("Is multiple value selectable: " + select1.isMultiple());

		// To select options via 0 based index
		select1.selectByIndex(1);
		
		// To select options via visible text
		select1.selectByVisibleText("Audi");
		
		// To select options via Value attribute
		select1.selectByValue("opel");
}

Useful methods in ChromeOptions & Capabilities Classes

Other than headless testing there are few more important and useful in-build methods available in ChromeOptions and Capabilities Classes.

MethodsDescription
addArguments(“headless”)Run the Browser in headless mode and the user can’t see the browser.
addArguments(“incognito”)Run the browser in incognito mode without saving any browser history and user data.
addArguments(“start-maximized”)Set the default size of the window as maximized.
setExperimentalOption(“excludeSwitches”,
Collections.singletonList(“enable-automation”))
To remove the message “Chrome is being controlled by automated test software” message while script execution.
cap.getVersion()Extract the current browser version.
cap.getBrowserName()Extract the current browser name.
cap.getPlatform()Extract the current OS platform.

Code Example

public static void main(String[] args) throws InterruptedException, AWTException {

		System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
				
		  ChromeOptions options=new ChromeOptions(); 
		  
		  // Run the Browser in headless mode and user cann't see the browser.
		  //options.addArguments("headless");
		  
		  // Run the browser in incognito mode withought saving any browser history and user data.
		  options.addArguments("incognito");
		  
		  // Set the default size of the window as maximized.
		 options.addArguments("start-maximized");
		  
		  // To remove message "Chrome is being controlled by automated test software"
		  options.setExperimentalOption("excludeSwitches", 
			        Collections.singletonList("enable-automation"));		  
		  
		  //options.addArguments("start-maximized","incognito","headless");
		  WebDriver driver = new ChromeDriver(options);
		 
		  // To access the Browser and Platform properties
		  Capabilities cap= ((ChromeDriver)driver).getCapabilities();
			 System.out.println("Browser Version via Capabilities: "+cap.getVersion());
			 System.out.println("Browser Name via Capabilities: "+cap.getBrowserName());
			 System.out.println("Platform Name via Capabilities: "+cap.getPlatform());
String multipleDDUrl = "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple";
		driver.get(multipleDDUrl);
		System.out.println("Browser Title: "+driver.getTitle());
}

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 0
  • Page views today : 0
  • Total visitors : 51,147
  • Total page view: 70,412

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.