环境搭建
- 安装appium-server (淘宝镜像安装)
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g appium
使用文档参考
- https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
#查询本地的模拟器系统
localhost:~ seveniruby$ $(which emulator) -list-avds
Nexus_5X_API_19
Nexus_5X_API_19_ARM
Nexus_5X_API_25
Nexus_5X_API_26
Nexus_5X_API_26_TesterHome
Nexus_5X_API_27
Pixel_2_API_23
Pixel_2_API_27
Pixel_XL_API_23
Pixel_XL_API_25
#开启模拟器
localhost:~ seveniruby$ $(which emulator) @Pixel_2_API_27
注意:
- 某些模拟器录制时是横屏显示,需要改下分辨率
- appium 1.14版本 android 8.0系统下无法获取“class”属性
Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn’t exist!
Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn’t exist!
- appium session 需要关闭,才可以使用 uiautomatorviewer,换句话说 uiautomatorview 不会和 appium 同时使用
appium 定位
- 假如元素有 content-desc 这个属性 那可以对应的使用find_element_by_Accessability_id来定位
相对定位
- //双斜杠开头,//* 表示条件 ,后面跟中括号,什么条件呢 //[@resource-id="…"]。假如找到两个元素,那么接着找 //[@resource-id="…" and @class="…"]
-
- //*[@resource-id=‘com.xueqiu.android:id/user_profile_icon’ and @class=‘android.widget.ImageView’]
-
- //*[contains(@resource-id, ‘user_profile_icon’) and contains(@class, ‘Image’)]
-
- //*[@text=‘基金’ and contains(@resource-id, ‘button’)]
-
- //*[@text=‘基金’ and @index=‘1’]
-
- (//*[@text=‘基金’])[1]
-
- //[contains(@resource-id, ‘buttons_container’)]//[@text=‘基金’]
Touch Action 的属性
需导入Touch Action的包
tap: 点一个元素
press 和 release:先按下再释放
long_press:长按
preform:整个串式的动作全部执行出来
move to:按下的同时,移动
滚动
找到一个可以被滚动的列表,取出它的第一个可滚动的元素
new UiSelector().scrollable(true).instance(0)
然后包裹在一个可滚动的对象里
new UiScrollable(new UiSelector().scrollable(true).instance(0))
然后就有了getChildByText属性
new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className(“android.widget.TextView”), “Tabs”)
scrollIntoView属性:滚动的视图是一个包含 WebView的 instance(0) 的一个元素
new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(“WebView”).instance(0))