如何在Appium上使用Jithave和Junit来捕获失败的屏幕截图?

问题描述

我在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(字符串消息,WebDriver驱动程序){ 尝试{

        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();
    }
}