I have this Java app that on Ubuntu, with Selenium and ChromeDriver, scraps some websites and retrieves some data. I wanted to automate error logging, including a screenshot being created if certain exceptions are thrown(all already implemented and working). Basically this is where I instantiate my driver:
System.setProperty("webdriver.chrome.driver", System.getProperty("user.home") + driversPath + "chromedriver");
ChromeOptions options = new ChromeOptions();
if(headless)
options.addArguments("--headless");
options.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
options.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
this.driver = new ChromeDriver(options);
this.driver.manage().window().setSize(new Dimension(1366, 768));
And this is how I create screenshots from it:
File srcFile = ((TakesScreenshot) this.driver).getScreenshotAs(OutputType.FILE);
try {
// creates file at System.getProperty("user.dir")
FileUtils.copyFile(srcFile, new File(path));
//catch and so on
With Boolean headless = false;
this works exactly as I wanted, and I can see my screenshots just fine. Now I would like to test in some VPS and needed to use it headless first locally.
With Boolean headless = true;
I noticed my automation worked as supposed by the log that was generated but when I intentionally got offline to throw some errors, I noticed all screenshots were blank images. I looked here but since with headless turned off I got screenshots, this answer doesn't matter to me.
I tried using setHeadless(boolean headless)
from ChromeOptions
that has this code:
public ChromeOptions setHeadless(boolean headless) {
args.remove("--headless");
if (headless) {
args.add("--headless");
args.add("--disable-gpu");
}
return this;
}
But it didn't work either. I've found this question but since I already got how to retrieve some screenshots from 'headfull' browser I think this isn't my case either.
I'm using ChromeDriver from this link(2.42)(the most recent at the time this question was made) and this dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.1</version>
</dependency>