[英]Java-Selenium : TakesScreenshot size issue
我创建了“ takescreenshot”可重用方法来捕获屏幕截图,并在需要的地方进行了调用。
但是我在这里面临一个奇怪的问题。 每次调用此功能时,捕获的图像大小都会不断增加,例如252K-> 278K-> 310K-> 400K ...
我在ExtentReport中使用的这些捕获的图像。 除了硒会话图像外,我确实看到捕获的黑色背景图像不确定其来源。
方法代码如下:
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代码中对一些现有图像进行硬编码,则一切正常。
有没有人遇到过这个问题。
我已经编写了一个代码来捕获元素的屏幕截图,效果很好。 可能会对您有帮助。 我不知道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){
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.