坑一:Desired Capabilities 参数设置

porps.put("reuse", 0);
porps.put("app", /Users/xxx/xxxx.app);

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

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

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

坑四:滑动操作没效果

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

坑五:截图找不到图片

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 的开发者团队能开源这么好的东西给我们使用


↙↙↙阅读原文可查看相关链接,并与作者交流