目录
前言:
在 Appium 中,app Inspector 是一个用来查看应用程序详细信息的工具。它可以用来查看应用程序的元数据、UI 元素、模拟器状态等信息。
演示环境
-
Appium.app 1.3.4
-
iOS 模拟器:iPhone 6 (8.3 Simulator)
-
Android 模拟器:genymotion 的 Google Nexus 4 - 4.4.4 - API 19 - 768x1280
虽然演示环境使用的是 mac ,但里面对于 Android 的参数设置是和 windows exe 通用的。
什么是 Inspector 工具
Inspector 工具的作用是通过截图查看元素对应节点,进而更加直观地获取到合适的元素定位器。在可以工作的情况下,它的界面如下 ( mac 上 appium.app 的 Inspector):
它主要由两部分组成。左侧是当前屏幕的节点树(类似于 driver.getSource()
返回的结果),右侧是当前屏幕的截图 ( driver.getScreenShot()
) 辅以能标明被选中节点所指的元素位置的红色方框。
它设计用途是通过获取当前屏幕的节点树及截图,让用户直观地看到节点树的节点对应屏幕的哪个元素,并辅以简单的录制脚本功能( mac 版录制功能较多,windows 版目前相对少很多)
Inspector 的使用前提
如上面所说,Inspector 依靠 driver 的 getSource()
和 getScreenShot()
方法获取截图及节点树,因此它的使用前提是你已经建立了 session 。
建立 session 有两种方法:
1、在你的脚本中使用合适的 caps 建立 WebDriver 实例。
2、在启动 appium server 时加入建立 session 所需参数及 --pre-launch
。
第一种相信能用 appium 跑通脚本的都知道如何使用。但每次都需要启动脚本才能用 Inspector 略嫌麻烦,因此我这里主要介绍第二种方法。
在图形化界面中建立 session
在 mac 版的 appium.app 的界面中,有两个设置各平台参数的菜单:
注意,虽然这里设置的值名称看起来和用例里的 caps 差不多,但这里设置的其实是 server arguments 。
对于建立 session ,我们必须设定的参数有:
-
Android: App path, 需要使用的模拟器或真机已经启动并能通过 adb devices 看到
-
iOS: App path, UDID(真机)或 force device(模拟器), platform version
记得要在你设定了的参数前打钩才是生效的哦。
然后我们在 General Settings 里面勾选 Pre-launch application:
最后点击 Launch 。然后 appium server 就会在启动时按照指定参数自动根据设定的参数开启 session 。这样你就能直接使用 Inspector 工具了。
附录
为了方便大家后面出错后查错,附上正常 Launch 后能直接打开 Inspector 查看元素时的所有 log(均使用模拟器)。
- iOS 模拟器:
Launching Appium with command: '/Applications/Appium.app/Contents/Resources/node/bin/node' lib/server/main.js --command-timeout "7200" --pre-launch --platform-version "8.3" --platform-name "iOS" --app "/Users/hengjiechen/Develop/IOSTutorial/ToDoList/build/Release-iphonesimulator/ToDoList.app" --device-name "iPhone 6" --native-instruments-lib
info: [debug] Starting Appium in pre-launch mode
info: Pre-launching app
info: [debug] Using local app from command line: /Users/hengjiechen/Develop/IOSTutorial/ToDoList/build/Release-iphonesimulator/ToDoList.app
info: [debug] Creating new appium session afd48ecc-4f20-4e42-a0c3-99f76d9a4548
info: [debug] Removing any remaining instruments sockets
info: [debug] Cleaned up instruments socket /tmp/instruments_sock
info: [debug] Setting Xcode folder
info: [debug] Setting Xcode version
info: [debug] Setting iOS SDK Version
info: [debug] Getting sdk version from xcrun with a timeout
info: [debug] iOS SDK Version set to 8.3
info: [debug] Checking whether instruments supports our device string
info: [debug] Getting list of devices instruments supports
info: [debug] Instruments is at: /Applications/Xcode.app/Contents/Developer/usr/bin/instruments
info: [debug] Getting device string from opts: {"forceIphone":false,"forceIpad":false,"xcodeVersion":"6.3","iOSSDKVersion":"8.3","deviceName":"iPhone 6","platformVersion":"8.3"}
info: [debug] fixDevice is on
info: [debug] Final device string is: 'iPhone 6 (8.3 Simulator)'
info: [debug] iOS sim UDID is E23DE92A-B6B1-4AEF-9632-4DA8DD7CE630
info: [debug] Detecting automation tracetemplate
info: [debug] Not auto-detecting udid, running on sim
info: [debug] Could not parse plist file (as binary) at /Users/hengjiechen/Develop/IOSTutorial/ToDoList/build/Release-iphonesimulator/ToDoList.app/en.lproj/Localizable.strings
info: Will try to parse the plist file as XML
`parseFileSync()` is deprecated. Use `parseStringSync()` instead.
info: [debug] Could not parse plist file (as XML) at /Users/hengjiechen/Develop/IOSTutorial/ToDoList/build/Release-iphonesimulator/ToDoList.app/en.lproj/Localizable.strings
warn: Could not parse app Localizable.strings; assuming it doesn't exist
info: [debug] Getting bundle ID from app
info: [debug] Parsed app Info.plist (as binary)
info: [debug] Creating instruments
info: [debug] Preparing uiauto bootstrap
info: [debug] Dynamic bootstrap dir: /Users/hengjiechen/Library/Application Support/appium/bootstrap
info: [debug] Dynamic env: {"nodePath":"/Applications/Appium.app/Contents/Resources/node/bin/node","commandProxyClientPath":"/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-uiauto/bin/command-proxy-client.js","instrumentsSock":"/tmp/instruments_sock","interKeyDelay":null,"justLoopInfinitely":false,"autoAcceptAlerts":false,"sendKeyStrategy":"oneByOne"}
info: [debug] Dynamic bootstrap code: // This file is automatically generated. Do not manually modify!
...
info: [debug] Dynamic bootstrap path: /Users/hengjiechen/Library/Application Support/appium/bootstrap/bootstrap-a4a902c9cce7b6cb.js
info: [debug] Reusing dynamic bootstrap: /Users/hengjiechen/Library/Application Support/appium/bootstrap/bootstrap-a4a902c9cce7b6cb.js
info: [debug] Getting device string from opts: {"forceIphone":false,"forceIpad":false,"xcodeVersion":"6.3","iOSSDKVersion":"8.3","deviceName":"iPhone 6","platformVersion":"8.3"}
info: [debug] fixDevice is on
info: [debug] Final device string is: 'iPhone 6 (8.3 Simulator)'
info: [debug] We're on iOS7.1+ so forcing defaultDevice on
info: [debug] Getting device string from opts: {"forceIphone":false,"forceIpad":false,"xcodeVersion":"6.3","iOSSDKVersion":"8.3","deviceName":"iPhone 6","platformVersion":"8.3"}
info: [debug] fixDevice is on
info: [debug] Final device string is: 'iPhone 6 (8.3 Simulator)'
info: [debug] Parsed app Info.plist (as binary)
info: [debug] Wrote new app Info.plist with device type
info: [debug] Checking whether we need to set app preferences
info: [debug] Running ios sim reset flow
info: [debug] Killing the simulator process
info: [