微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UI 自动化 六 —— Appium 的定位方式

Appium 概念

手机App分为两大类,原生App(Native App)和混合APP(Hybrid App)

原生App(Native App)

原生App实际就是我们所常见的传统App开发模式,云端数据存储+App应用客户端。App应用客户端,包含了所有的UI元素、框架逻辑等。只有数据存储在云端。

混合App(Hybrid App)

混合App就是HTML5 App,混合App通常由两部分组成,HTML5云网站+App应用客户端,这里的App应用客户端实际只是个架子,里面的UI元素和逻辑,都是存储在云端的,每次在打开App时,去云端获取数据呈现给手机用户。说白了混合App就是将Web页面嵌套到了App应用客户端中。由于每次打开App都要向HTML云端服务请求数据,所以会产生不小的数据流量,并且如果没有网络,会导致无法看到HTML App。这就是常说的H5页面

Appium介绍

Appium是一个开源、跨平台的自动化测试框架,它适用于Native App、Hybrid App。

Appium有个比较好的设计哲学,简单的说就是不要为了移动端自动化测试而单独开发一套Api接口,所以Appium也是基于Selenium的Webdriver进行了扩展,扩展了一些操作移动端的Api接口,在我们学习完Selenium的Api接口后,我们在学习Appium只需要学习扩展的那些操作移动端的Api接口即可。

Appium最牛之处在于支持跨平台操作。实际上我们在测试时是启动了一个Appium Server,这个Server可以放到任意的机器上,供公司的自动化测试人员共同使用。

Appium原理

Appium既然是基于Selenium做的二次扩展,那么Appium也是一个经典的Client-Server的设计模式,我们的Code就是狭义上的客户端,Server端与Selenium不同,Selenium直接测试浏览器Web页面,将浏览器作为服务端。而Appium的服务端是我们自己启动的Appium-Server。数据与操作命令传递与Selenium Api接口相同,遵守REST设计风格的Api接口。(所谓的REST设计风格,只是一帮喜欢写论文的人装逼搞出来的名词,让大家看的云里雾里。简单的理解,就是一种接口设计方式,网上对于REST的解释层出不穷,翻译成三句话,看URL知道要什么、看HTTP Metod知道干什么、看HTTP Status Code知道结果。)

Appium工作流

 

Appium 定位的方式

ClassName

Android

Android的class属性对应ClassName定位方式,ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性

iOS

iOS的type属性对应CLassName定位方式,ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性

 

ID

Android

Android的resource-id对应ID定位方式,这个id也可能存在重复情况,可以通过index来获取需要的元素。(从0开始查找dom树中的同名resource-id属性

使用appium-desktop来获取元素时,如果提示有id的定位方式,则可以只接获取,代表唯一。

 

XPATH

Android

Android的Xpath定位与PC的XPATH定位大同小异,可以通过相对路径的定位方式定位,区别在于,这里相对路径定位的//后只可以接Android的class属性或*。(//android.widget.Button[@text="登 录"])

iOS

iOS10 以上使用XCUITest框架后,原生框架不支持XPATH,Appium进行了转换,速度很慢不建议使用。

 

AccessibilityId

Android

Android的content-desc属性对应AccessibilityId定位方式,这个content-desc属性专门为残障人士设置,如果这个属性不为空则推荐使用。

iOS

iOS的label和name属性都对应AccessibilityId定位方式,如果有则推荐使用。

 

AndroidUIAutomator

Android的源生测试框架的定位方式,定位速度快。推荐使用牢记常用的几种。

# 这个在运行时,调用的是Android自带的UI框架UiAutomator的Api
# 介绍几个简单常用的,text、className、resource-id
# text
# 匹配全部text文字
driver.find_element_by_android_uiautomator('new UiSelector().text("手机号")')
# 包含text文字
driver.find_element_by_android_uiautomator('new UiSelector().textContains("机")')
# 以text什么开始
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("手")')
# 正则匹配text
driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^手.*")')
 
# className
driver.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.TextView")')
# classNameMatches
driver.find_elements_by_android_uiautomator('new UiSelector().classNameMatches("^android.widget.*")')
 
# resource-id、resourceIdMatches
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.syqy.wecash:id/et_content")')
 
# description
driver.find_element_by_android_uiautomator('new UiSelector().description("S 日历")')
# descriptionStartsWith
driver.find_element_by_android_uiautomator('new UiSelector().descriptionStartsWith("日历")')
# descriptionMatches
driver.find_element_by_android_uiautomator('new UiSelector().descriptionMatches(".*历$")')

iOSPredicateString

支持iOS10以上,可以多个属性同时定位,推荐。(替代XPATH)

driver.find_elements_by_ios_predicate("label == '登录'")
 
driver.find_elements_by_ios_predicate("type='XCUIElementTypeOther' and name='联系人,标签, 第2个按钮,共3个'")

iOSUIAutomation

iOS9.3以下使用,现在已经废弃,iOSPredicateString代替了iOSUIAutomation

Appium 怎么用?

上一篇博客我们学会了如何搭建 Appium 的环境,那么如何才能连接呢?

  1. 打开移动设备
  2. 打开 appium-server,并且与移动设备连接
  3. 打开 appium-client,配置好与 appium - server 的连接信息

1、打开移动设备

首选的是移动设备也就是真机,其次才是模拟器

2、打开 appium-server,并且将其与移动设备连接

在 cmd 下输入 appium,启动一个 appjium 的 server 

appium
(node:12476) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
[Appium] Welcome to Appium v1.8.1
[Appium] Appium REST http interface listener started on 0.0.0.0:4723

 

并且将其与移动设备连接,真机的话建议用 usb ,一定要打开开发者模式以及允许 USB 调试

另外开一个 CMD ,输入:adb devices ,显示 List of devices attached 即为连接成功

adb devices
adb server version (31) doesn't match this client (41); killing...
* daemon started successfully
List of devices attached
emulator-5554   device

 

3、打开 appium - client ,配置好连接信息,让 client 端可以操作 server 端,server 端再去操作移动端

定位元素方式

青铜:appium 客户端

王者:安装的 SDK 下的 tool 下的 uiautomatorviewer.bat

appium 使用

1、打开 appium

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐