I am running Selenium testing overnight (using Java & WebDriver) and would like to take a screenshot when one of the testcases fails. If I use the Robot's createScreenCapture() method it only works if the monitor is on, and if I use the Selenium's getScreenshotAs() method it doesn't capture any popups or anything that are on top of the window (and often contain the cause of the problem). The getScreenshotAs() method does work when the pc is locked so that is what I am currently using. My machines run Windows 7 if that matters.
I adapted this code (found on this site) and am using this currently:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
This is what I had used previously but it only captures a grey screen with the task bar at the bottom if the pc is locked, but if I'm logged in it works great and captures everything. The problem is I need to have my computer locked overnight and that is when this code needs to run:
Robot robot = new Robot();
// Get size of screen
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
// Capture the screen
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
// Save the screen to to disk
ImageIO.write(screenFullImage, "jpg", new File(fileName));
I found some other posts on here asking similar questions but they were all pretty old and didn't quite match my question so I was wondering if any solution given these circumstances was possible?
EDIT: When I use the Robot code these are the results. I tried using both Chrome as the browser (ChromeDriver) and Firefox (FirefoxDriver).
If I run the Robot version of the screen capture in a class all by itself, just a loop that captures the screen every 5 seconds (Run As > Java Application), it works just fine even with the computer locked, but when I have Selenium run the same code (its Run As > JUnit Test) when a test fails it gives the screenshots above if the pc is locked. I even tried having the Selenium code spawn a new thread to take the screenshot while it did a Thread.sleep() but got the same results once the pc was locked.

