macaca icon indicating copy to clipboard operation
macaca copied to clipboard

macaca-android驱动封装规则

Open CodeToSurvive1 opened this issue 10 years ago • 2 comments

  • Macaca Version:
  • Macaca Doctor Logs:
  • Operate System:
  • Programming Language:

查看macaca-android源代码发现,有的操作是直接通过adb操作进行的,比如截图getScreenshot,而有的是通过uiautomator-bootstramp在手机上启动服务端发送指令来实现的,请问这两者使用的规则是什么呢?哪种操作会使用adb直连进行操作?哪种操作需要走uiautomator服务端进行操作

getScreenshot截图走adb

controllers.getScreenshot = function *() {
  const swapFilePath = temp.path({
    prefix: `${pkg.name}-screenshot`,
    suffix: '.png'
  });
  const remoteFile = `${this.adb.getTmpDir()}/screenshot.png`;
  const cmd = `/system/bin/rm ${remoteFile}; /system/bin/screencap -p ${remoteFile}`;
  yield this.adb.shell(cmd);

  yield this.adb.pull(remoteFile, swapFilePath);

  var base64 = null;

  try {
    let data = fs.readFileSync(swapFilePath);
    base64 = new Buffer(data).toString('base64');
  } catch (e) {
    throw new errors.NoSuchWindow();
  }

  _.rimraf(swapFilePath);
  return base64;
};

getSource获取dump先走uiautomator后走adb


controllers.getSource = function *() {
  yield this.send({
    cmd: 'getSource',
    args: {
    }
  });
  var tmpDir = this.adb.getTmpDir();
  var xml = yield this.adb.shell(`cat ${tmpDir}/macaca-dump.xml`);
  var xml_hack = yield this.adb.shell(`cat ${tmpDir}/local/tmp/macaca-dump.xml`);
  return xml.length > xml_hack.length ? xml : xml_hack;
};

CodeToSurvive1 avatar Sep 29 '16 08:09 CodeToSurvive1

因为有些操作只有 abd 支持,有些是 uiautomator 支持,所以只能混着用了。一般是优先 uiautomator。

ziczhu avatar Sep 29 '16 08:09 ziczhu

上层可以完成的就不会使用驱动层,简单原则,但是不稳定的话会考虑更下层的实现

xudafeng avatar Oct 01 '16 14:10 xudafeng