Appium自动化测试—定位方法
·
前言
定位元素是 Appium 自动化测试中最核心和基础的技能。无法定位元素,后续的所有操作(点击、输入、滑动等)都无从谈起。
一、定位原理:基于 WebDriver 协议?
Appium 遵循 W3C WebDriver 协议,它将移动应用(原生、混合)的界面元素映射为抽象的“元素”,然后通过不同的“定位策略”来查找这些元素。这与你使用 Selenium 定位网页元素的概念非常相似。
二、核心定位策略 (Location Strategies)
1.id / resource-id (首选)
这是最高效、最稳定的定位方式。Android 元素的 id 对应其 resource-id 属性,iOS 元素的 id 对应其 name 或 accessibility id 属性:
Android: driver.find_element(By.ID, "com.example.app:id/login_button")
iOS: driver.find_element(By.ID, “myLoginButton”)
2.accessibility id (移动端首选)
XPath 是一种在 XML 文档中定位节点的语言,非常强大和灵活,可以定位到几乎任何元素:
driver.find_element(By.ACCESSIBILITY_ID, “login_button”)
3.xpath (强大但谨慎使用)
这是一个专门为辅助功能(如读屏软件)设计的标识符,也非常适合自动化测试。在 Android 上对应 content-desc,在 iOS 上对应 accessibility identifier:
定位所有 TextView://android.widget.TextView
定位文本为“登录”的按钮://*[@text='登录'] (Android) 或 //*[@label='登录'] (iOS)
定位父元素下的某个子元素://android.widget.LinearLayout[@resource-id='container']/android.widget.Button[1]
4.class name (通常用于查找元素类型)
通常用于查找同一类型的多个元素,例如所有文本框或所有按钮:
获取所有输入框:driver.find_elements(By.CLASS_NAME, “android.widget.EditText”)
5.android uiautomator (Android 专属,强大)
仅适用于 Android。它使用 Android UiAutomator API 提供的强大选择器进行定位:
获取所有输入框:driver.find_elements(By.CLASS_NAME, “android.widget.EditText”)
6.ios predicate string (iOS 专属,推荐)
仅适用于 iOS。这是一种基于元素属性进行匹配的强大语言,语法简洁,效率高于 XPath:
通过 label 精确匹配:driver.find_element(By.IOS_PREDICATE, “label == ‘登录‘”)
通过 value 模糊匹配:driver.find_element(By.IOS_PREDICATE, “value BEGINSWITH ‘Log’”)
组合条件:driver.find_element(By.IOS_PREDICATE, “type == ‘XCUIElementTypeButton’ AND label == ‘Submit’”)
7.ios class chain (iOS 专属)
是 ios predicate string 的扩展,结合了 Predicate 和 XPath 的优点,可以进行灵活的层级定位,性能比 XPath 好:
driver.find_element(By.IOS_CLASS_CHAIN, ‘**/XCUIElementTypeButton[label == “登录“]’)
三、具体XPATH定位
# 1、text 属性定位://android.widget.TextView[@text='阿里拍卖']
# 2、contain属性值部分匹配://android.widget.TextView[contains(@text,'阿里拍卖')]
# 3、class 属性定位://android.view.View[@class='android.view.View']
# 4、content-desc 属性定位://android.view.View[@content-desc='婴儿车床两用']
# 5、contain属性值部分匹配://android.view.View[contains(@content-desc,'婴儿车床两用')]
# 6、text (文本定位):driver.findElement(By.xpath("//*[@text='登录']")); 对应Inspector 参数: text
# 通过坐标点定位:drver.
四、点击类操作
# 1、driver.find_element(AppiumBy.ID,'').click()
# 2、tap(self, positions, duration=None): 模拟手指点击(最多五个手指)
# positions:list类型,里面对象是元组,最多五个。如:[(100, 20), (100, 60)]
# duration:设置按住时间长度,持续时间,单位毫秒,如:500
# driver.tap( [(700,1450)],100)
五、输入
driver.find_element(AppiumBy.ID,'com.google.android.apps.nexuslauncher:id/input').send_keys()
sleep(1)
六、滑动
6.1 方式1:
si = driver.get_window_size() # 获取屏幕宽度和高度
driver.swipe(si['width'] * 0.2, si['height'] * 0.5, si['width'] * 0.8, si['height'] * 0.5, 800)#滑动到前一页
driver.swipe(si['width'] * 0.8, si['height'] * 0.5, si['width'] * 0.2, si['height'] * 0.5, 800)#滑动到后一页
# 上滑
# driver.swipe(si['width'] * 0.5, si['height'] * 0.2, si['width'] * 0.5, si['height'] * 0.8, 800)
# 下滑
# driver.swipe(si['width'] * 0.5, si['height'] * 0.8, si['width'] * 0.5, si['height'] * 0.2, 800)
#Swipe(start_x,start_y,end_x,end_y,duration)
6.2 方式2:drag_and_drop:
#将元素e1拖拽到元素e2
driver.drag_and_drop(e1,e2)
6.3 方式3:TouchAction
from appium.webdriver.common.touch_action import TouchAction
#press:点击坐标,move_to:移动到某个坐标,release:释放鼠标,perform:执行以上所有操作
TouchAction(driver).press(x=650,y=640).wait(1500).move_to(x=100,y=640).release().perform()
总结
推动为重要的可交互元素添加唯一的 resource-id 或 accessibility id,这是提升自动化测试稳定性和效率的最有效途径。
更多推荐
所有评论(0)