我创建了一个可以重复使用的方法来截取屏幕截图,并在需要的地方调用它。
然而,我在这里面临着一个奇怪的问题。每次调用此函数时,捕获的图像大小都会不断增加,例如252K -> 278K -> 310K -> 400K ...
这些捕获的图像是我在ExtentReport中使用的。除了selenium会话图像之外,我确实看到一个黑色背景图像被捕获,但不确定它来自哪里。
方法代码如下:
public static void takescreenshot(ExtentTest Test,String Status){
Date d=new Date();
String CurrentTimeStamp=d.toString().replace(":", "_").replace(" ", "_");
File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(CurrentTimeStamp+".png"));
if(Status.equals("pass")){
Test.log(LogStatus.PASS, "snapshot below:-"+CurrentTimeStamp+".png"));
}else if(Status.equals("fail")){
Test.log(LogStatus.FAIL, "snapshot below:-"+CurrentTimeStamp+".png"));
}
}如果我在extentreport代码中硬编码一些现有的图像,那么一切都很好。
有没有人遇到过这个问题。
发布于 2017-05-11 15:11:01
我已经写了一个代码来捕获元素的屏幕截图,效果很好。也许这会对你有所帮助。我不知道Extendreport是什么,所以在这方面我帮不了你。
public static void takeElementScreenshot(WebDriver driver, WebElement element){
try{
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = element.getLocation();
// Get width and height of the element
int eleWidth = element.getSize().getWidth();
int eleHeight = element.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("D:\\Screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
}
catch(Exception e){
}
}https://stackoverflow.com/questions/43908169
复制相似问题