【问题标题】:Selenide and Firefox record faulty screenshotSelenide 和 Firefox 记录错误截图
【发布时间】:2017-09-08 08:39:15
【问题描述】:

我正在使用 Selenide 和 Phantomjs 来测试 Web 应用程序。 我截图如下:

byte[] bytes = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.BYTES);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
returnImage = ImageIO.read(bis);

但是,对于输入元素,屏幕截图如下所示:faulty screenshot

实际上是这样的(使用 chrome/firefox 时) how it's supposed to look

有趣的是,当我将 Selenide 设置为使用 phantomjs (Configuration.Browser = "phantomjs") 时,它会正确截取屏幕截图。 它也只发生在那种元素上。按钮等记录良好。有什么想法吗?

PS:本文所附的截图被裁剪,这里的代码截取了整个页面的截图。在我的代码中,我只将屏幕截图裁剪为所需的元素,但即使在显示整个屏幕的屏幕截图上,该元素也没有被正确记录。

【问题讨论】:

    标签: java selenium firefox selenium-webdriver selenide


    【解决方案1】:

    您可以使用以下代码截取元素:-

    WebElement ele = driver.findElement(By.id("hplogo"));
    
    // 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 = ele.getLocation();
    
    // Get width and height of the element
    int eleWidth = ele.getSize().getWidth();
    int eleHeight = ele.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("C:\\images\\GoogleLogo_screenshot.png");
    FileUtils.copyFile(screenshot, screenshotLocation);
    

    现在在上面的代码中,你得到了 getWidth() 和 getHeight()。您可以尝试通过增加或减少值来调整它。

    【讨论】:

    • 恐怕这不是我的问题的答案。我截屏没有问题,它的元素显示不正确。
    • 哦,所以你的驱动程序在调用时没有显示正确的元素?
    • 截图没有正确显示元素。对于某些人来说,它现在可以工作了,我不知道为什么。无论如何,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2012-02-07
    • 2013-08-29
    • 2019-07-13
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    相关资源
    最近更新 更多