51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 3098|回复: 4
打印 上一主题 下一主题

[转贴] Macaca App Inspector 原理解析

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-6-20 10:50:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Macaca 提供元素查看器(app-inspector),能够查看 iOS 和 Android 应用的元素属性值,如 name, id, XPath 等。

app-inspector 有如上几个组成部分,分别介绍主要用途:
  • UIAutomatorWD: UIAutomatorWD 是对 UI Automator 的封装,app-inspector 需要使用 UI Automator 提供的接口获取 Android 系统的 window Hierarchy。这也是 macaca-android 的下层功能模块,可见 app-inspector 使用的驱动层与 macaca-android 是一致的。
  • macaca-adb: ADB 在 app-inspector 启动过程中会操作设备和用来回传截图。
  • XCTestWD: XCTestWD 是对 XCTest 的封装,app-inspector 需要使用 UI Automator 提供的接口获取 iOS 系统的 lastSnapshot。这也是 macaca-ios 的下层功能模块,可见 app-inspector 使用的驱动层与 macaca-ios 是一致的。
  • ios-simulator: ios-simulator 用来操作 iOS 模拟器。
  • ios-utils: 此模块提供 iOS SDK 环境检查,获取真机日志输入等功能。
设计原则
  • 跨多端:app-inspector 同时支持两个平台,避免了维护两套环境的困窘。
  • 完全WEB化:app-inspector 的实体是一个 WEB 服务,方便各处部署,只要有标准浏览器即可使用。
开放和标准化是贯穿 Macaca 整体的理念,可以看到能够 web 化的工具都是使用 web 实现的。
实现原理
  • 初始化时启动手机设备上的 UIAutomatorWD 或 XCTestWD 服务
  • 向 UIAutomatorWD 或 XCTestWD 先后发送 /source 和 /screenshot 请求
  • 将每次时序获取的截图和 Hierarchy 做映射,渲染到用户浏览器
处理渲染
app-inspector 用户端分三部分,中间的部分用来将界面 Hierarchy 渲染为树形组件。当操作中间的树控件时,将节点的信息同时展示在右侧,同时将节点携带的 width 和 height 坐标等渲染在左侧,高亮显示。
处理事件
  1. export function boundsSize(bounds) {
  2.   const [
  3.     x,
  4.     y,
  5.     width,
  6.     height
  7.   ] = bounds;
  8.   return width * height;
  9. };
  10. export function compareBoundsSize(rectA, rectB) {
  11.   return boundsSize(rectA) > boundsSize(rectB);
  12. };
  13. export function isInRect(x, y, bounds) {
  14.   const [
  15.     _x,
  16.     _y,
  17.     width,
  18.     height
  19.   ] = bounds;
  20.   return x >= _x
  21.     && x <= _x + width
  22.     && y >= _y
  23.     && y <= _y + height;
  24. };
复制代码
当操作左侧屏幕展示区时,通过后序遍历的方式查找到用户端操作到的区域,然后将后面的遍历操作都终结掉,同时高亮当前区域。
  1. export function getNodePathByXY(tree, isIOS, x, y) {
  2.   let bestBounds = null;
  3.   let bestPath = null;
  4.   function walk(node, path) {
  5.     let bounds = node.bounds;
  6.     let inRect = isInRect(x, y, bounds);
  7.     if (inRect) {
  8.       if (!bestBounds || compareBoundsSize(bestBounds, bounds)) {
  9.         bestBounds = bounds;
  10.         bestPath = path;
  11.       }
  12.       if (node.nodes) {
  13.         node.nodes.forEach((child, index) => {
  14.           walk(child, path.concat([index]));
  15.         });
  16.       }
  17.     }
  18.   }
  19.   walk(tree, []);
  20.   return bestPath;
  21. };
复制代码
处理 XPath
  • 当前节点若有唯一ID,则返回/*[@resource-id="${ID}"]
  • 当前节点的父节点有没有ID,若有则采用相对定位
  • 当前节点若有唯一文本值,则返回/*[@text="${ID}"],其他属性类似
  • 当前节点若没有命中任何规则,直接返回最长路径
如何应对 WebviewMacaca app-inspector 弥补了社区 Native 元素查看器的空白,Webview 又如何处理?
不建议将 Webview 透出到 Native 元素上来当做 Native 元素处理,虽然 Android 能够这样实现,因为毕竟是已经标准化运行时的 View,暂时没法与 Naitve 等同看待。
由于历史渊源,Android 还是 iOS 中提供的 Webview 底层都支持 WebKit 远程调试协议的,协议的统一是 Web 调试工作的福音,借助现有的 devtools 工具就能够完成 inspector 的功能。
$ ios_webkit_debug_proxy -f chrome-devtools://devtools/bundled/inspector.html或打开 chrome://inspect/#devices 浏览器调试界面就可以找到 Webview 的页面了。


  • https://github.com/macacajs/app-inspector
  • https://doing-data-science.github.io/objc_inheritance/tree.html
  • https://xudafeng.github.io/pillow/examples/mouse/

欢迎讨论,互相学习。

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1
回复

使用道具 举报

  • TA的每日心情
    无聊
    2024-7-12 13:16
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    2#
    发表于 2017-6-20 10:54:16 | 只看该作者
    分析的很透彻,通俗易懂!
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    3#
     楼主| 发表于 2017-6-20 10:55:03 | 只看该作者
    乐哈哈yoyo 发表于 2017-6-20 10:54
    分析的很透彻,通俗易懂!

    谢谢支持
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2017-6-21 15:15:46 | 只看该作者
    先进来占个坑。
    晚点学习……
    谢谢版主的资料了……
    分享的可以哦……
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-21 05:37 , Processed in 0.070792 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表