跨应用使用Spoon框架截图的方法

spoon框架是一个很棒的用例驱动跟测试结果生成加工的框架。但在使用spoon-client时,传入参数需要被测应用的activity实例,跨应用测试会很受限(当然也可能是因为我对android不熟导致的,在使用uiautomator2时,我只能拿到被测应用的activity名,但没办法拿到实例)。这里提供一种解决办法,就是直接修改spoon框架的源码,仅供参考。

 

通过阅读spoon框架源码可以发现,spoon-client提供screenshot的api来截图,截图后会保存在包名路径下,而spoon-runner则会在报告生成时,去读对应路径下的图片文件。

  public static File screenshot(Activity activity, String tag, String testClassName,
      String testMethodName) {
    if (!TAG_VALIDATION.matcher(tag).matches()) {
      throw new IllegalArgumentException("Tag must match " + TAG_VALIDATION.pattern() + ".");
    }
    try {
      File screenshotDirectory =
          obtainScreenshotDirectory(activity.getApplicationContext(), testClassName,
              testMethodName);
      String screenshotName = System.currentTimeMillis() + NAME_SEPARATOR + tag + EXTENSION;
      File screenshotFile = new File(screenshotDirectory, screenshotName);
      takeScreenshot(screenshotFile, activity);
      Log.d(TAG, "Captured screenshot '" + tag + "'.");
      return screenshotFile;
    } catch (Exception e) {
      throw new RuntimeException("Unable to capture screenshot.", e);
    }
  }

因此可以把takeScreenshot(screenshotFile, activity)直接用Runtime. getRuntime().exec("screencap -p " +screenshotFile);替换后记得用Chmod修改file的可读权限,不然文件没办法pull到服务器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值