跨应用使用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到服务器。

原文地址:https://www.cnblogs.com/alexkn/p/5958810.html