How to take the Screen shot in Selenium Grid

Share on Facebook2Share on LinkedIn0Share on Google+0Tweet about this on Twitter1Email this to someoneDigg this

Here is the code that you can use to take the screen shot when running the script using Selenium Grid or Remote machine

public void takeScreenshot() throws IOException {
Random rand = new Random();
int num = rand.nextInt(150);
File scrFile = null;
Integer numNoRange = rand.nextInt();
WebDriver underlyingDriver = driver.getWrappedDriver();
if (browser.contains("Remote")) {
try {
WebDriver augmentedDriver = new Augmenter().augment(underlyingDriver);
scrFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
String filename = numNoRange.toString();
copyFile(scrFile, new File("location were you want the file to be saved" + filename + ".png")); //Change location were you want the file to be saved
} catch (Exception n) {
LOGGER.warn("Failed to take screen shot of remote browser");
}
}
if (scrFile == null) {
try {
scrFile = ((TakesScreenshot) underlyingDriver).getScreenshotAs(OutputType.FILE);
String filename = numNoRange.toString();
copyFile(scrFile, new File("location were you want the file to be saved" + filename + ".png")); //Change location were you want the file to be saved
} catch (NullPointerException n) {
fail(n.getMessage());
}
}
}


Leave a Reply

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