Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java+失败的屏幕截图;黄瓜_Java_Cucumber_Screenshot - Fatal编程技术网

使用Java+失败的屏幕截图;黄瓜

使用Java+失败的屏幕截图;黄瓜,java,cucumber,screenshot,Java,Cucumber,Screenshot,我发现了一种广泛使用的方法,可以在使用Java+Cucumber时捕获故障屏幕截图,这很好,也很简单: @After public void embedScreenshot(Scenario scenario) throws Exception { if (scenario.isFailed()) { try { byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(Outpu

我发现了一种广泛使用的方法,可以在使用Java+Cucumber时捕获故障屏幕截图,这很好,也很简单:

@After
public void embedScreenshot(Scenario scenario) throws Exception {
    if (scenario.isFailed()) {
        try {
            byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
            String testName = scenario.getName();
            scenario.embed(screenshot, "image/png");
            scenario.write(testName);
        } catch (WebDriverException wde) {
            System.err.println(wde.getMessage());
        } catch (ClassCastException cce) {
            cce.printStackTrace();}
        }
    }
}  

例如,我如何将图像文件写入桌面上的文件夹,而不是默认情况下的项目目录,并给它一个自定义名称,例如我的Cucumber测试方案的名称?

您可以从
方案中获得方案名称,如下所示:

scenario.getName()
然后您只需创建两个
文件
对象,一个用于屏幕截图,另一个用于目标,并使用
org.apache.commons.io.FileUtils将屏幕截图文件复制到目标文件夹:

FileUtils.copyFile(file, destFile);

@gppanter嵌入方法将只接受字节作为参数。那么您是如何更改的呢。我正试图为我的代码截图

@After
public void embedScreenshotOnFail(Scenario s) {
    if (s.isFailed()) try {
        File  screenshot = 
          ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
     s.embed(screenshot , "image/png");
    } catch (ClassCastException cce) {
        cce.printStackTrace();
    }
}

谢谢我将输出类型修改为“FILE”而不是“Bytes”,它工作起来很有魅力。我已经编辑了我的原始帖子,以显示我目前使用的代码,这对我来说很有用。(我刚刚了解到SOF不允许在回复评论时使用代码)。我确实使用了“OutputType.BYTES”。