Macaca Macaca-ios-java 真机踏平的那些坑

Gavin · 2017年01月09日 · 最后由 达峰的夏天 回复于 2018年03月21日 · 1534 次阅读

坑一:Desired Capabilities 参数设置

  • 下面这两个参数同时设置,"reuse"参数才会生效
  • "app"参数直接从 xcode 拷贝出 “xxx.app” 的路径即可
  • 只有设置了 “app” 这个参数批量运行脚本的时候,每个脚本开始前才会重新卸载安装 app
porps.put("reuse", 0);
porps.put("app", /Users/xxx/xxxx.app);

坑二:真机运行一定要给 Webdriveragent 签名,否则脚本执行到 install app 的时候就会报错

  • 这一点可以参考链接来设置

坑三:动态获取界面元素的坐标

  • macaca 虽然支持界面元素的滑动长按等操作,但是 api 输入的参数全部是坐标,所以就需要能动态获取到元素的坐标(x,y)宽,高 (width,heigth)
  • 这个功能要特别感谢阿里的君禾美女对 java api 的及时更新,macacaclient 的版本要在 1.0.48 以上
<dependency>
        <groupId>macaca.webdriver.client</groupId>
        <artifactId>macacaclient</artifactId>
        <version>1.0.48</version>
</dependency>
private Double[] getElementLocation(Element element) {
    Double[] location = new Double[2];
    try {
        JSONObject elementRect = (JSONObject) element.getRect();
        location[0] = Double.valueOf(elementRect.get("x").toString()) + Double.valueOf(elementRect.get("width").toString()) / 2;
        location[1] = Double.valueOf(elementRect.get("y").toString()) + Double.valueOf(elementRect.get("height").toString()) / 2;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

坑四:滑动操作没效果

  • 按照 api 的描述参数 “duration”,一定是 0.05D,否则滑动是没有效果的
  • 滑动操作由于 macaca.java.biz 对原生的 macaca api 进行了封装,所以就直接引用了
<dependency>
          <groupId>macaca.java</groupId>
          <artifactId>biz</artifactId>
          <version>0.0.7</version>
          <type>jar</type>
 </dependency>
private void scrollByLocation(double xPoint, double yPoint, int direction, int step) {
       JSONObject obj = new JSONObject();
       try {
           switch (direction) {
               case RIGHT: {
                   driver.drag(Double.valueOf(xPoint), Double.valueOf(yPoint), Double.valueOf(xPoint + step), Double.valueOf(yPoint), 0.05D, step);
                   break;
               }
               case LEFT: {
                   if (step > xPoint) {
                       driver.drag(Double.valueOf(step), Double.valueOf(yPoint), Double.valueOf(0), Double.valueOf(yPoint), 0.05D, step);
                   } else {
                       driver.drag(Double.valueOf(xPoint), Double.valueOf(yPoint), Double.valueOf(xPoint - step), Double.valueOf(yPoint), 0.05D, step);
                   }
                   break;
               }
               case UP: {
                   if (step > yPoint) {
                       driver.drag(Double.valueOf(xPoint), Double.valueOf(step), Double.valueOf(xPoint), Double.valueOf(yPoint), 0.05D, step);
                   } else {
                       driver.drag(Double.valueOf(xPoint), Double.valueOf(yPoint), Double.valueOf(xPoint), Double.valueOf(yPoint - step), 0.05D, step);
                   }
                   break;
               }
               case DOWN: {
                   driver.drag(Double.valueOf(xPoint), Double.valueOf(yPoint), Double.valueOf(xPoint), Double.valueOf(yPoint + step), 0.05D, step);
                   break;
               }
           }
       } catch (Exception e) {
           handleFailure(e.getMessage());
       }
   }

坑五:截图找不到图片

  • macaca 虽然支持运行失败截图的功能,但是却不能创建图片的路径,所以在调用 api 的时候要创建图片路径
  • macaca.java.biz 对截图功能进行了封装,我就直接拿过来用例
  • 图片路径是支持相对路径的
public static String screenShot(BaseMacacaClient driver, String ScreenshotPath) {
      String screenShotPath=null;
      try {
          // 判断是否存在对应目录,不存在的话则创建
          File file = new File(ScreenshotPath);
          if (!file.exists() || !file.isDirectory()) {
              // 没有目录 创建截屏目录
              System.out.println("没有screenshot目录,创建目录");
              boolean isMkdirSucc = file.mkdir();
              if (isMkdirSucc) {
                  System.out.println("创建screenshot目录成功");
              } else {
                  System.out.println("创建screenshot目录失败");
              }
          } else {
              System.out.println("存在screenshot目录");
          }
          String time = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
          String screenShotName = time + ".png";
          screenShotPath = ScreenshotPath + "/" + screenShotName;
          driver.saveScreenshot(screenShotPath);
      } catch (Exception e) {
          // TODO: handle exception
          logger.info("Can't save screenshot");
          logger.error(e.getMessage());
      }
      return screenShotPath;
  }

总结个人在使用过程中遇到的一些问题,希望能帮助到广大的 macaca 使用者,非常感谢 macaca 的开发者团队能开源这么好的东西给我们使用

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 3 条回复 时间 点赞
恒温 将本帖设为了精华贴 01月10日 13:13
恒温 取消了精华贴 01月10日 13:14

还略微差了一点点。。。

精华!

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