Using TakesScreenshot interface API getScreenshotAs(OutputType<T>) can take screenshot in Selenium WebDriver.
public interface TakesScreenshot
Indicates a driver that can capture a screenshot and store it in different ways.
Example usage:
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
/**
* Taking the entire browser screenshot
* @param screenName
* @throws IOException
*/
private static void captureScreenshot(String screenName) throws IOException {
//take screenshot
File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//create Object for Date class
Date d = new Date();
screenName=screenName+"-"+d.toString().replace(":", "-").replace(" ", "-")+".jpg";
//copy the file name under project directory
FileUtils.copyFile(src, new File(System.getProperty("user.dir")+"\\src\\screenshots\\"+screenName));
}
/**
* taking the element screenshot
* @param element
* @param screenName
* @throws IOException
*/
private static void captureScreenshot(WebElement element,String screenName) throws IOException {
//take screenshot
File src=((TakesScreenshot)element).getScreenshotAs(OutputType.FILE);
//create Object for Date class
Date d = new Date();
screenName=screenName+"-"+d.toString().replace(":", "-").replace(" ", "-")+".jpg";
//copy the file name under project directory
FileUtils.copyFile(src, new File(System.getProperty("user.dir")+"\\src\\screenshots\\"+screenName));
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.