Macaca Macaca-NoSmoke 遍历调研过程记录

进击的程序茗 · 2018年07月03日 · 最后由 少年不识月 回复于 2020年10月29日 · 1932 次阅读

个人 blog

http://cming.site/

背景介绍

本文主要介绍对于 NoSmoke 遍历 Android App 使用过程中的一些记录,通过源码打断点的方式及查看截图效果简单确定一些 NoSmoke 的内部机制,其中若有理解错误或不当之处还请多多指教。本文未多介绍环境搭建,环境搭建有需要再补。

环境介绍

Macaca+NoSmoke+Android 手机 +Mac10.13.1

主要参考文档:

Macaca NoSmoke 官方文档

Macaca 自动遍历器 NoSmoke 发布公测

基于 macaca 的遍历 NoSmoke

NoSmoke 启动流程

NoSmoke 其实是提供了一套遍历流程,Macaca 作为服务端,NoSmoke 作为客户端 去对手机进行一个遍历操作。

主要分以下步骤:

  • 开启 Macaca 服务端
  • 更改 NoSmoke 配置
  • 启动 NoSmoke 客户端
  • 页面实时查看效果

NoSmoke 结果页展示

首先来简单介绍一下 NoSmoke 的遍历结果页面,在图上简单的进行了一下标注,对于标注相关说明会在下面详细介绍

NoSmoke结果页面.png

Digest对应的Actions列举

NoSmoke图片简排页面

NoSmoke 遍历流程

NoSmoke 在遍历过程中核心步骤主要如下:

  • 确定页面 Digest
  • 确定页面的 Action 集合
  • 确定页面的点击操作
  • 循环该过程直至遍历结束 我们先简单的说明一下这 4 个步骤。

NoSmoke-Digest 介绍

在上面的效果图中提到了 Digest,Digest 就相当于一个页面的唯一标识,即在自动化遍历页面时如何确定两个页面时相同的控件结构。

在 NoSmoke 的代码中NoSmoke/lib/crawler/models.js函数NSAppCrawlingTreeNode.prototype.checkDigest里提供了判断 Digest 的方法,核心代码如下:

this.digest = '' + (source.value.match(/node/g) || []).length +
            (source.value.match(/Android/g) || []).length +
            (source.value.match(/TextView/g) || []).length +
            (source.value.match(/EditText/g) || []).length +
            (source.value.match(/Layout/g) || []).length +
            (source.value.match(/Button/g) || []).length;

从代码中可以看到每个页面根据 Macaca 返回的控件树的 value 值,匹配一定节点,拼接而成的 Digest。因此我们看到的 Digest 是一串数字。图片上的 View 值就是唯一 ID。

Digest

对于这个 Digest,从页面效果来看它的区分程度准确率并不是特别的好,上面的图和下面的图区分来看是一样的页面,但是它们的 Digest 是不一样的。图片没有截全,下面的 Digest 是520120411,上面的图图像上标识是520120421

Digest

个人认为页面唯一 ID 的确定本身就是一个有无数种选择的问题,可以说未必能有一种唯一的算法可以确定出所有 APP 的页面 ID,因此 NoSmoke 也提供了 hook,用户可以自己编写自己所确定页面 ID 的代码。

NoSmoke-Action 介绍

当我们确定好这个页面的唯一 ID 时,页面中我们要去点击哪些控件呢?这就是 Action 集合所做的事情。

每一次请求页面 Source 时,都会返回页面的节点层级,在 NoSmoke 中通过不断的查询子节点,确定了每一个控件的执行路径。此处详细代码在NoSmoke/lib/crawler/crawler.js文件中的NSCrawler.prototype.recursiveFilter函数中 以下仅截取部分代码:主要是如果当前节点有子节点,进行递归循环,并生产Xpath路径

/** 1. filter Current Node Information */
if (source.hasOwnProperty('children')) {
  if (Array.isArray(source.children)) {
    for (let i = 0; i < source.children.length; i++) {
      this.eraseModelDifference(source.children);
      this.insertXPath(source, source.children[i]);
      let result = this.recursiveFilter(source.children[i], matches, exclusive);
      sourceArray = sourceArray.concat(result);
    }
  } else {
    this.eraseModelDifference(source.children);
    this.insertXPath(source, source.children);
    let result = this.recursiveFilter(source.children, matches, exclusive);
    sourceArray = sourceArray.concat(result);
  }
}

生产的路径如下:

页面Digest及对应Action路径

此处重点概括一下就是:

  • 一个 Digest 会生产多个 Action,这些 Action 集合为 Actions
  • NoSmoke 源码中设定 Actions 生产的最大数量为 16
  • 遍历路径达到的 Action/所有 Digest 生产的 Action 为页面展示的百分比
Action 具体生成

页面控件层级

上图是页面的控件层级图。我们如果转化成模块简单化路径按下图层级为:

a-b-d//这个结果是通过生产的Action和页面的层级路径相比对整理出来的,如有错误还请指出
a-b-e
a-f
a-g

灵魂画图

上图里的 c 和 h 路径并没有被录入,用户可以匹配某种控件类型被过滤,如果父元素为被过滤的类型,那么子元素同样不会被选择。

页面元素点击

页面元素点击在performAction函数里,这里的逻辑就比较简单,主要是对于当前即将点击的元素进行类型判断clickTypes,horizontalScrollTypes,editTypes这三种分别为点击、滑动、输入三种类型,那么我们怎么知道过来的一个元素应该点击滑动还是输入呢?

yml 文件配置

horizontalScrollTypes:
  - 'android.widget.ImageView'
  - 'android.widget.TextView'
  - 'android.widget.Button'
clickTypes:
  - 'android.widget.Button'
  - 'android.widget.TextView'
editTypes:
  - 'android.widget.EditText'

当前的元素是图片类型,它被设定为滑动类型,那么久会被滑动,当前图片是按钮,它既可以滑动也可以点击,按照下面代码中的 if else 逻辑会对其进行点击操作.

if (this.config.clickTypes.indexOf(action.source.type) >= 0) {
          /** 1. handle click actions */
          return new Promise((resolve, reject) => {
            root.wdclient.send('/wd/hub/session/' + this.sessionId + '/element/' + action.data.value.ELEMENT + '/click', 'post', {}, () => {
              this.refreshScreen();
              resolve();
            });
          });
        } else if (this.config.horizontalScrollTypes.indexOf(action.source.type) >= 0) {
          /** 2. handle horizontal scroll actions */
          return new Promise((resolve, reject) => {
            root.wdclient.send('/wd/hub/session/' + this.sessionId + '/actions', 'post', {'actions': [{'type': 'drag', 'fromX': 600, 'fromY': 200, 'toX': 10, 'toY': 200, 'duration': 2}]},//这里滑动的坐标被我更改了
              () => {
                this.refreshScreen();
                resolve();
              });
          });
        } else if (this.config.editTypes.indexOf(action.source.type) >= 0) {
          /** 3. handle edit actions */
          return new Promise((resolve, reject) => {
            root.wdclient
              .send('/wd/hub/session/' + this.sessionId + '/element/' + action.data.value.ELEMENT + '/value', 'post', {
                'value': [action.input]
              }, () => {
                this.refreshScreen();
                resolve();
              });
          });
}

循环结束

整个页面的遍历会在什么时候循环结束呢?

// Terminate under the following cases:
  // 1. the previous node has been finished for continuously count of 8, assume crawling finish
  // 2. the crawling process takes too long and hence expire
  if (this.repeatingCrawlingCount >= maxRepeatCrawlingCount || this.crawlingExpires) {
    this.terminate('terminate due to timeout');
    return;
  }
  • maxRepeatCrawlingCount 爬行的最大层数 默认为 8
  • repeatingCrawlingCount 当前爬行的层数
  • crawlingExpires 配置的时间 默认 30min
页面深度设定规则
  • 该页面为新的页面 repeatingCrawlingCount = 0
  • 这个页面 action 全部被浏览完毕 且这个页面的父节点类型不是需要被过滤的 repeatingCrawlingCount++ 这是根据代码里的逻辑得出的这样一个结论,没有进行具体验证。

NoSmoke 官方爬行原理图

以上是我在通过打断点和页面效果观察过程中的一点记录,下面是官方公布的原理图,在上面做了小标志。官方高清大图请走链接

Macaca 自动遍历器 NoSmoke 发布公测

从PPT上直接截取下来了

个人总结

以上就是在 NoSmoke 调研过程中的一点个人记录,主要从四个方面结合源码和执行效果进行了简单分析,如有错误或者疏漏之处还请指出。

NoSmoke 本身提供了很强的 hook 配置,及用户可以自己定义一系列的 Action,最后的页面展示页特别赞,本次调研未调研 iOS 相关,因为 WDA 好像……又出问题了……

共收到 8 条回复 时间 点赞

有遇到过半小时把内存跑满 CPU 使用率 100% 的问题么?15 年的 macpro

我去催饭 回复

我这边是一边测试一边打断点 跑过几次半小时的时长 但是没有遇到 CPU 使用率 100% 的问题~

一边打点一边测试,慢是非常常见的

这个怎么打断点啊

墨语 回复

是在 vscode 中对于 node 打的断点

/** 1.1.2 if finished browsing, and the current one is originates from a normal view, trigger back and then crawl again*/
this.repeatingCrawlingCount++;
if (this.currentNode.depth === 0) {
/** 1.1.2.1 if depth is 0 , then terminate crawling, avoid further navigate back /
this.repeatingCrawlingCount = maxRepeatCrawlingCount;
this.crawl();
} else {
/
* 1.1.2.2 if depth is not 0, then back and further explore */
console.log('=====this.curentNode.depth !== 0 back');
this.back();
}
}

请问大佬 这段代码的意思 为什么 this.currentNode.depth === 0 就终止爬行呢?

支持 iOS 真机吗?

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