Why Headless Chrome?
A headless browser is a great tool for automated testing and server environments where you don’t need a visible UI shell. This can be achieved by using Selenium Java Class org.openqa.selenium.chrome.ChromeOptions.
Refer to the Google Chrome Capabilities Link
Pre-requisites:
- Chrome version installed should be greater then 59 version.
- Use the latest version of chromedriver.exe downloaded from the link https://chromedriver.storage.googleapis.com/index.html?path=2.36/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.Assert; import org.testng.annotations.Test; public class HeadLessChromeBrowser { WebDriver driver; @Test(description="Verify the Search Results contains matching jobs for Selenium") public void verifyJobSearchDetails(){ System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); //Option for Windows OS options.addArguments("--disable-gpu"); driver = new ChromeDriver(options); driver.get("http://www.indeed.co.in"); System.out.println("Title of the WebPage::" + driver.getTitle()); WebElement whatWE = driver.findElement(By.id("what")); System.out.println("Element is Displayed::"+ whatWE.isDisplayed()); whatWE.clear(); whatWE.sendKeys("Selenium"); driver.findElement(By.id("where")).sendKeys("Bangalore"); driver.findElement(By.id("fj")).click(); String actual = driver.findElement(By.className("summary")).getText(); String expected="Selenium"; System.out.println("Actual value::" + actual); System.out.println("Expected value::" + expected); Assert.assertTrue(actual.contains("Selenium")); } } |
Exception 1: unknown error: Chrome failed to start: was killed
Resolution:
Remove the following options from the code while running in windows.
options.addArguments(“–print-to-pdf”);
options.addArguments(“–screenshot”);
Exception 2: “unknown error: call function result missing ‘value'”
Resolution:
Download the latest version of chromedriver.exe solves this Issue.
Link for downloading the latest version: https://chromedriver.storage.googleapis.com/index.html?path=2.36/
Refer to the below Links for Practise |
Selenium Wiki |
Hello,
I have a test which opens a pdf page. Testing in Selenium with Chrome driver i am able to check for document type of pdf. But the same code does not work with Headless Chrome. I use the following options:
ChromeOptions options = new ChromeOptions();
options.addArguments(“headless”);
options.addArguments(“window-size=1200×600”);
WebDriver driver = new ChromeDriver(options);
What can be done to make headless chrome recognize the pdf page that opens during testing?
Thanks
Ram