appium---uiautomator定位方法

  前面总结了7种定位方法,今天在介绍一种uiautomator方法,其实appium就是基于uiautomator框架实现的,让咱们一块儿看下uiautomator有哪些定位方法可使用android

uiautomator是什么

UIAutomator是android的自动化测试框架,也是Android-Sdk中一个查看页面组件元素工具web

uiautomator定位方法

前面介绍了常规的定位方法,这里uiautomator又提供了3种经常使用的定位方法,此次咱们主要经过uiautomator方法进行介绍,定位的话依旧拿淘宝来作实战app

经过Text方法

一、text(“text文本”)框架

text = 'text("注册/登陆")' driver.find_element_by_android_uiautomator(text).click()

二、文本比较长,可使用textContains模糊查询定位 textContains('text文本')工具

text1 = 'textContains("请输入手机号码")' driver.find_element_by_android_uiautomator(text1).send_keys("123456")

三、textStartsWith("以text文本开头")测试

text2 = 'textStartsWith("请输入验证码")' driver.find_element_by_android_uiautomator(text2).send_keys("12234")

运行结果:ui

因为用的是模拟器可能有点卡顿。抱歉哈编码

代码结果:spa

from appium import webdriver import time desired_caps = { 'platformName': 'Android',  # 测试版本
                 'deviceName': 'emulator-5554',   # 设备名
                 'platformVersion': '5.1.1', # 系统版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 启动launch Activity
                "noReset": True,  # 不清空数据
                "unicodeKeyboard": True,    # 使用Unicode编码方式发送字符串
              "resetKeyboard": True,      # 键盘隐藏起来
 } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(4) text = 'text("注册/登陆")' driver.find_element_by_android_uiautomator(text).click() time.sleep(5) text1 = 'textContains("请输入手机号码")' driver.find_element_by_android_uiautomator(text1).send_keys("123456") time.sleep(5) text2 = 'textStartsWith("请输入验证码")' driver.find_element_by_android_uiautomator(text2).send_keys("12234")

Class Name方法

这个方法和appium定位方法同样都是经过Class属性进行定位code

# 经过class定位登陆按钮
className = 'className("android.widget.Button")' driver.find_element_by_android_uiautomator(className).click()

经过uiautomator工具找到注册/登陆按钮的Class属性

resource-id方法

resourceld 和appium中的id同样。

# 经过resourceId定位输入框
id = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et")' driver.find_element_by_android_uiautomator(id).send_keys("123456")

赞成的方法经过uiautomator找到搜索框的id

 id和class定位执行结果:

代码结果:

# coding:utf-8
from appium import webdriver import time desired_caps = { 'platformName': 'Android',  # 测试版本
                 'deviceName': 'emulator-5554',   # 设备名
                 'platformVersion': '5.1.1', # 系统版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 启动launch Activity
                "noReset": True,  # 不清空数据
                "unicodeKeyboard": True,    # 使用Unicode编码方式发送字符串
              "resetKeyboard": True,      # 键盘隐藏起来
 } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(6) # 经过class定位登陆按钮
className = 'className("android.widget.Button")' driver.find_element_by_android_uiautomator(className).click() time.sleep(5) # 经过resourceId定位输入框
id = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et")' driver.find_element_by_android_uiautomator(id).send_keys("123456")

上面写了一些单独的定位方法,其实uiautomator还支持组合定位元素,意思就是支持id和text或者text和className等,这样的定位更加准确,具体的继续往下看吧

组合定位

一、id和text方法组合

# 经过text+ClassName组合 (resourceId(属性).text(属性))
classText = 'className("android.widget.Button").text("注册/登陆")' driver.find_element_by_android_uiautomator(classText).click()

二、class和text方法组合

# 经过text+ID组合 (resourceId(属性).text(属性))
IdText = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et").text("请输入手机号码")' driver.find_element_by_android_uiautomator(IdText).send_keys("123456")

执行结果:

# coding:utf-8
from appium import webdriver import time desired_caps = { 'platformName': 'Android',  # 测试版本
                 'deviceName': 'emulator-5554',   # 设备名
                 'platformVersion': '5.1.1', # 系统版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 启动launch Activity
                "noReset": True,  # 不清空数据
                "unicodeKeyboard": True,    # 使用Unicode编码方式发送字符串
              "resetKeyboard": True,      # 键盘隐藏起来
 } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(6) classText = 'className("android.widget.Button").text("注册/登陆")' driver.find_element_by_android_uiautomator(classText).click() time.sleep(5) # 经过text+ID组合
IdText = 'resourceId("com.taobao.taobao:id/aliuser_login_mobile_et").text("请输入手机号码")' driver.find_element_by_android_uiautomator(IdText).send_keys("123456")

父子定位childSelector

 定位的时候咱们也能够经过父级找到子级定位

 

 

格式:

# 经过父子定位
(父亲属性).childSelector(定位属性)
# 经过父子定位
fuzi = 'resourceId("com.taobao.taobao:id/home_searchbar").childSelector(className("android.widget.EditText"))' driver.find_element_by_android_uiautomator(fuzi).click()

兄弟定位fromParent

定位也能够经过兄弟之间的完成定位

格式:

# 经过兄弟定位
(兄弟属性).fromParent(定位属性)
# 经过兄弟元素定位
xiongdi = 'resourceId("com.taobao.taobao:id/photoBtn").fromParent(className("android.widget.EditText"))' driver.find_element_by_android_uiautomator(xiongdi).send_keys(u"牛仔裤")

 完成代码:

# coding:utf-8
from appium import webdriver import time desired_caps = { 'platformName': 'Android',  # 测试版本
                 'deviceName': 'emulator-5554',   # 设备名
                 'platformVersion': '5.1.1', # 系统版本
                "appPackage": "com.taobao.taobao",   # app包名
                "appActivity": "com.taobao.tao.welcome.Welcome",   # 启动launch Activity
                "noReset": True,  # 不清空数据
                "unicodeKeyboard": True,    # 使用Unicode编码方式发送字符串
                "resetKeyboard": True,      # 键盘隐藏起来
 } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) time.sleep(6) # 经过父子定位
fuzi = 'resourceId("com.taobao.taobao:id/home_searchbar").childSelector(className("android.widget.EditText"))' driver.find_element_by_android_uiautomator(fuzi).click() time.sleep(6) # 经过兄弟定位
xiongdi = 'resourceId("com.taobao.taobao:id/photoBtn").fromParent(className("android.widget.EditText"))' driver.find_element_by_android_uiautomator(xiongdi).send_keys(u"牛仔裤")

结果:

这个地方没有设置appium的键盘全部致使的是乱码的。

 元素定位方面方法很是的多,喜欢那种咱们用哪一种,哪一种简单咱们就用那种

 

感受安静写的对您有帮助的话,能够点歌关注,不迷路,有哪里写错的或者不懂的能够下方留言!

相关文章
相关标签/搜索