Basically, you want to run a headless-browser. Yes, it’s possible to run Selenium headlessly. Before you can begin testing, there are a couple of things you’ll need to install first:
Java
Selenium
ChromeDriver (latest)
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.
package chrome;
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();
}
}
If you run this code, it will print the title of the page and will take the screenshot as well.