首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java-Selenium : TakesScreenshot大小问题

Java-Selenium : TakesScreenshot大小问题
EN

Stack Overflow用户
提问于 2017-05-11 14:36:39
回答 1查看 401关注 0票数 0

我创建了一个可以重复使用的方法来截取屏幕截图,并在需要的地方调用它。

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

这些捕获的图像是我在ExtentReport中使用的。除了selenium会话图像之外,我确实看到一个黑色背景图像被捕获,但不确定它来自哪里。

方法代码如下:

代码语言:javascript
复制
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代码中硬编码一些现有的图像,那么一切都很好。

有没有人遇到过这个问题。

EN

回答 1

Stack Overflow用户

发布于 2017-05-11 15:11:01

我已经写了一个代码来捕获元素的屏幕截图,效果很好。也许这会对你有所帮助。我不知道Extendreport是什么,所以在这方面我帮不了你。

代码语言:javascript
复制
  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){

                } 
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43908169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档