Appium 求大神帮忙,appium 运行失败自动截图,使用方法 Throwable.getCause()时,获取到信心为 null

YYGiel · 2015年04月18日 · 最后由 YYGiel 回复于 2015年04月20日 · 1279 次阅读

详细代码如下:

public class MyEventListener extends AbstractWebDriverEventListener  {
    @Override
    public void onException(Throwable throwable, WebDriver driver) {
        Throwable cause = throwable.getCause();
        if(cause instanceof ScreenshotException){
            String filePath = "F:/screenshot";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
            String daString = format.format(new Date());
            File of = new File(filePath+daString+"-exception.png");
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(of);
                out.write(new Base64Encoder().decode(((ScreenshotException) cause).getBase64EncodedScreenshot()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try {
                    out.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

public void setUp() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
         WebDriverEventListener eventListener = new MyEventListener();
          driver = new EventFiringWebDriver(new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities)).register(eventListener);
//      driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
         Thread.sleep(6000);

    }

不能成功截图保存在文件中,麻烦大神帮忙解答下

共收到 5 条回复 时间 点赞
6楼 已删除

if(cause instanceof ScreenshotException){ 这句过不了。

#2 楼 @lihuazhang ,目前就是困在这句了,不知道咋办,请问有好的方法吗

7楼 已删除

这种方式的要 driver 本身支持的。webdriver 里面也只有 firefox 支持。

#5 楼 @lihuazhang 那请问,目前有啥好的解决方案吗

http://8atmyhouse.com/blog/webdriver-zhi-ce-shi-shi-bai-zi-dong-jie-tu.html

可以用两种方式来截图:

  • RemoteWebDriver 监听器是在 RemoteWebDriver 抛出异常的时候截图。
  • TestRule 是在 assert 失败的时候截图。

第一种需要 driver 本身出异常的时候,返回的出错信息里面,带有 screen 字段的值。所以对 driver 的要求比较多。
第二种就采用主动截图的方式(不过用了 testRule,所以也不需要你每次都写代码截图)。这个就要看 driver 支持不支持截图了。

Appium Server 在出异常的时候,返回给客户端的 error 对象的 json 里面没有截图信息。所以你没办法,除非你自己修改代码。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册