gpt4 book ai didi

Java-Selenium : TakesScreenshot size issue

转载 作者:行者123 更新时间:2023-12-02 13:12:06 26 4
gpt4 key购买 nike

我创建了“takescreenshot”可重用方法来捕获屏幕截图,并在需要的地方调用它。

但是我在这里面临一个奇怪的问题。每次调用此函数时,捕获的图像尺寸都会不断增加例如 252K -> 278K -> 310K -> 400K ...

我在 ExtentReport 中使用这些捕获的图像。另外,除了 Selenium session 图像之外,我确实看到捕获的黑色背景图像不确定它来自哪里。

方法代码如下:

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){

}
}

关于Java-Selenium : TakesScreenshot size issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43908169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com