繁体   English   中英

如何在 Appium 上使用带有 Junit 的 Jbehave 截屏失败?

[英]How can I take screenshot on failure using Jbehave with Junit on Appium?

我在 Appium 上使用 Jbehave 和 Junit。 我需要截取失败的屏幕截图并将它们添加到报告中。 那么在 Appium 上使用 Jbehave 在 Junit 上截屏的方法是什么?

谢谢:)

screenShot()添加到失败的断言、异常或您想要截屏的任何其他语句中。

import java.io.File;
import org.apache.commons.io.FileUtils;

public void screenShot() {
    String picSavedPath = "screenshot";
    if (!(new File(picSavedPath).isDirectory())) {
        new File(picSavedPath).mkdirs();
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String time = sdf.format(new Date());

    //---- Below are essential statements
    WebDriver augmentedDriver = new Augmenter().augment(driver);
    File sourceFile = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
    //----

    try {
        FileUtils.copyFile(sourceFile, new File(picSavedPath + File.separator + time + ".png"));
    } catch (IOException e) {
        LogUtil.error(e);
    }
}

您可以使用 getScreenshotAs() 方法。 例如:

public void takescreenshot(String message, WebDriver driver) { try {

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        
        File file = new File("GivethePathto savethescreenshot/screenshot"+message+".png");
        FileUtils.copyFile(scrFile,file );
        test.addScreenCaptureFromPath(file.getAbsolutePath(), message);

    }
    catch(Exception e) {

        e.printStackTrace();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM