Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
228 views
in Selenium by
recategorized by
I  need code in Java to take screenshot using selenium webdriver

2 Answers

0 like 0 dislike
by The go-to Tester (327 points)
reshown by

You can try using below function.

 

public static String captureScreenshot(WebDriver driver,String screenshotdir) throws IOException {
String randomUUID = UUID.randomUUID().toString();
String storeFileName = screenshotdir + "\\"
+ getFileNameFromURL(driver.getCurrentUrl()) + "_"
+ randomUUID + ".png";
String[] screenshotdirsplit = screenshotdir.split("\\\\");
String fileName = screenshotdirsplit[screenshotdirsplit.length - 1] + "\\"
+ getFileNameFromURL(driver.getCurrentUrl()) + "_"
+ randomUUID + ".png";
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(storeFileName));
return fileName;
}
 
0 like 0 dislike
by New User (11 points)

I am using the following method to take a screenshot.

public static void makeScreenshot(WebDriver driver, String screenshotName) {

WebDriver augmentedDriver = new Augmenter().augment(driver);

/* Take a screenshot */

File screenshot = ((TakesScreenshot) augmentedDriver) .getScreenshotAs(OutputType.FILE);

String nameWithExtention = screenshotName + ".png";

/* Copy screenshot to specific folder */

try {

String reportFolder = "test-output/Screenshots" + File.separator;

String screenshotsFolder = "screenshots";

File screenshotFolder = new File(reportFolder + screenshotsFolder);

if (!screenshotFolder.getAbsoluteFile().exists()) {

screenshotFolder.mkdir();

}

FileUtils.copyFile(screenshot, new File(screenshotFolder

+ File.separator + nameWithExtention).getAbsoluteFile());

} catch (IOException e) {

log("Failed to capture screenshot: " + e.getMessage());

}

log(getScreenshotLink(nameWithExtention, nameWithExtention));

}


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...