【问题标题】:How to take a screenshot with Selenium WebDriver [duplicate]如何使用 Selenium WebDriver 截屏 [重复]
【发布时间】:2014-04-28 10:29:45
【问题描述】:

我目前正在研究 Selenium WebDriver、JavaTestNG 框架。

我用 Java 编写的代码。

如果我的 Java 代码是布尔值,我如何截取屏幕截图?

例子:

public boolean test() throws Exception {
    // Some implementation
}

我尝试了这一步,但它显示错误:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

copyFile 的这一行中的错误显示为The method copyFile(File, File) is undefined for the type FileUtils

 FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

【问题讨论】:

标签: java selenium selenium-webdriver


【解决方案1】:
import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.apache.commons.io.FileUtils;


public  class Test {

    public void screenShot() {

        // 'driver' is your WebDriver
        File screenshot = ((TakesScreenshot) driver)
                                .getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File(fileName));

    }
}

【讨论】:

  • 一个解释应该是有序的,例如这个解决方案背后的意图/想法是什么?你可以edit your answer
【解决方案2】:

这是一个示例程序:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

【讨论】:

  • 一个解释是有序的,例如这个解决方案背后的意图/想法是什么?你可以edit your answer
【解决方案3】:

如果我们有一个 Firefox Driver 对象,那么:

    FirefoxDriver Driver = new FirefoxDriver();
    Driver.get("http://facebook.com/");
    File ScrFile = Driver.getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(ScrFile, new File("D://img.jpg"));

【讨论】:

  • 大写的变量名(DriverScrFile)是 Java 约定吗?
【解决方案4】:

here 下载Shutterbug JAR 文件。

public void takeSnapShot(String i) throws Exception {

    /* Function: This function is used to take a snapshot. It
                 will take a snapshot of the entire page.

       Function Call: takeSnapShot("Screen_name"); 
    */

    Shutterbug.shootPage(browser, ScrollStrategy.BOTH_DIRECTIONS).save(Path + i);
}

【讨论】:

    【解决方案5】:

    您可以做的另一件事,而不是屏幕截图,以确保页面看起来像您想要的那样:

    1. 使用 WebElement.getLocation() 获取元素的坐标 或页面部分。
    2. 使用 WebElement.getSize() 获取该元素的尺寸 或页面部分。
    3. 使用 WebElement.getText() 来验证该元素或部分的内容。

    【讨论】:

      【解决方案6】:
          TakesScreenshot scrShot = ((TakesScreenshot)webdriver);
      
          File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
      
          File DestFile = new File(fileWithPath);
      
          FileUtils.copyFile(SrcFile, DestFile);
      }
      

      【讨论】:

      • 一个解释应该是有序的,特别是当代码似乎不完整时(例如,它是一个片段还是它真的缺少什么)。最好是edit you answer.
      【解决方案7】:
      // Convert web driver object to the TakeScreenshot
      TakesScreenshot scrst = ((TakesScreenshot)webdriver);
      
      // Call getScreenshotAs method to create an image file
      File SrcFile = scrst.getScreenshotAs(OutputType.FILE);
      
      // Move image file to the new destination
      File DestFile = new File(fileWithPath);
      
      // Copy file at the destination
      FileUtils.copyFile(SrcFile, DestFile);
      

      【讨论】:

        猜你喜欢
        • 2011-03-26
        • 2019-03-11
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        • 2019-08-08
        • 1970-01-01
        相关资源
        最近更新 更多