APP测试实战
一、定位元素方法
#生成webdriver对象
awd = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
#定位元素的方法同selenium
1、 根据 id 定位
-
awd.find_element( “id” , resourceId的值 )
Android的resource-id对应ID定位方式
注意:这个id也可能存在重复情况,可以通过index来获取需要的元素。
#示例1: id 定位 元素
#(1)定位 报表 点击
awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_charts").click()
time.sleep(2)
#(2)定位 账本 点击
awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_account").click()
time.sleep(2)
#(3)定位 【发现】元素 点击
awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_discovery").click()
time.sleep(2)
#(4)定位 【设置】元素 点击
awd.find_element("id","com.mobivans.onestrokecharge:id/mybtn_setting").click()
time.sleep(2)
2、 根据 xpath定位
- awd.find_element( ‘xpath’, ‘/hierarchy/类名1/类名2’)
app绝对路径 :/hierarchy/类名1/类名2/类名2[1]
web绝对路径: /html/body/标签
#(3)定位 【发现】元素 点击
awd.find_element("xpath","
/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout[3]/android.widget.FrameLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.LinearLayout[3]/android.widget.LinearLayout").click()
time.sleep(2)
#点击记一笔的 xpath
'/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout[3]/android.widget.FrameLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.view.View'
2 相对路径 :
-
使用属性定位元素
-
awd.find_element( ‘xpath’, ‘//*[@属性=”值“]’)
-
常用属性有:
-
tex属性 定位元素: //*[@text=’内容’]
-
resource-id属性 定位元素 : //*[@resource-id=’xx’]
-
class属性 定位元素 : //*[@classs=’类名’] #android 的类名重复多,当类名唯一的时候使用
Android的class属性对应ClassName定位方式
ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性)
find_element(“xpath”,” //*[@classs=’类名’]”) #只有一个指定类名 唯一
find_elements(“xpath”,” //*[@classs=’类名’]”)[0] # 指定类名如果有多个
-
-
-
从某个元素向下查找待定位的元素
awd.find_element( 'xpath', '//*[@resource-id=”值“]/类名1/类名2/类名3[2]' )
- 常见的类名:
- android.widget.FrameLayout
- android.widget.LinearLayout
- android.support.v7.widget.RecyclerView
- android.widget.TextView
- android.widget.ImageView
- android.widget.EditText
- android.widget.Button
-
从已定位的元素继续向下查找待定位的元素:
元素对象1=awd.find_element( 'xpath', '//*[@属性=”值“]/类名1/类名2/类名3' )
元素对象2 = 元素对象1.find_element('xpath',"//*[@text='XX']")
'''
app绝对路径 : //类名[@属性='xx']/类名 //*[@resoucer-id='xx']/类名/类名[1]
web相对路径 ://标签[@属性="xx"]/标签
'''
#==============示例: 定位 【发现】元素 点击===================
#-----(1)相对路径 text属性定位
aWd.find_element('xpath',"//*[@text='发现']").click()
#-----(2)相对路径 从某个元素向下查找待定位的元素
awd.find_element('xpath',"//*[@resource-id='com.mobivans.onestrokecharge:id/bottomBar_ll_bottom']/android.widget.LinearLayout[3]").click()
'''
【发现】元素的类名: android.widget.LinearLayout
【发现】元素的上一级元素 resource-id=com.mobivans.onestrokecharge:id/bottomBar_ll_bottom
'''
- 示例 : 使用 xpath 相对路径 定位 发现元素 如下图所示:
3 、根据 class_name定位
- awd.find_elements(‘class name’, ‘//类名’)[索引号]
android 的类名重复多,所以使用elements 结合索引号 定位
类似于 web 页面中的 tag name
4、 使用位置定位元素
如果id,xpath, class_name 都不能定位到元素, 那么使用位置(x,y坐标)定位元素
- 点击元素 webdriver对象 . tap( [ (x,y) ] )
如何计算x,y的坐标值呢?
由于手机设备的屏幕大小不同,所以需要先获取屏幕实际尺寸,在屏幕范围内确定x,y的值
获取屏幕尺寸:
width= webdriver对象.get_window_size()['width']
height= webdriver对象.get_window_size()['height']
#在屏幕区域内 点击
awd.tap( [(width/2,heignt/2)])
#当某个元素 本身点击不起作用时,使用 awd.tap 在屏幕某个位置点击
el=awd.find_element("","")
x=el.location().get("x")
y=el.location().get("y")
awd.tap( [(x+10,y+10)] )
5 、定位 Toast消息提示框
Toast消息框 自动显示,过一会儿会自动消失,类似于web的alert提示框
-
awd.find_element(“xpath”,”//*[@class=’android.widget.Toast’]”)
6 、AccessibilityId属性定位
Android的content-desc属性对应AccessibilityId定位方式,这个content-desc属性专门为残障人士设置,如果这个属性不为空则推荐使用
find_element("accessibility id","21 三月 2022")
二、元素操作方法
-
点击 el.click()
-
清除 el.clear()
-
输入 el.sendkeys(‘text’)
-
获取元素的文本 el.text ——- android.widget.TextView
-
获取元素的属性 el.getAttribute(‘checked’)
-
获取元素左上角坐标 el.location el.location.get[“x”] el.location.get[“y”]
-
获取元素的宽、高 el.size
# 示例:"记一笔" 元素坐标位置和元素的高宽
#定位 记一笔按钮
jiyibi_button= self.awd.find_element("id","com.mobivans.onestrokecharge:id/main_write1")
# "记一笔" 元素坐标位置
print(jiyibi_button.location) #{'x': 320, 'y': 1174}
print(jiyibi_button.location.get("x"),jiyibi_button.location.get("y"))
# "记一笔" 元素高宽
print(jiyibi_button.size)#{'height': 106, 'width': 80}
print(jiyibi_button.size.get("height"),jiyibi_button.size.get("width"))