appium---uiautomator定位方法

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

uiautomator是什么

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

uiautomator定位方法

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

通过Text方法

1、text(“text文本”)

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

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

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

3、textStartsWith("以text文本开头")

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

运行结果:

由于用的是模拟器可能有点卡顿。抱歉哈

代码结果:

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属性进行定位

# 通过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等,这样的定位更加准确,具体的继续往下看吧

组合定位

1、id和text方法组合

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

2、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的键盘所有导致的是乱码的。

 元素定位方面方法非常的多,喜欢那种我们用哪种,哪种简单我们就用那种

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

猜你喜欢

转载自www.cnblogs.com/qican/p/11544421.html