Appium元素定位(二):UiAutomator定位

UiAutomator定位用于Android APP的元素定位,使用UI Automator API(UISelector类)来搜索特定元素。 Appium将Java代码作为字符串发送到服务器实现对应用程序的交互。

UiAutomator是android的工作引擎,所以定位速度快。缺点是语法表达式复杂,容易写错。

官方文档:https://developer.android.com/reference/android/support/test/uiautomator/UiSelector

下面将介绍UiSelector类一些常用接口使用方法。

uiautomator定位方式

resource-id定位

  • new UiSelector().resourceld("id")

classname定位

  • new UiSelector().className("className")

content-desc定位

  • new UiSelector().description("contenet-desc属性")

通过文本定位

  • text文本定位
    • new UiSelector().text("text文本")
  • textContains 模糊匹配
    • new UiSelector().textContains("包含text文本")
  • textStartsWith,以某个文本开头来匹配
    • new UiSelector().textStartsWith("以text文本开头")
  • 正则表达式 textMatches 匹配
    • new UiSelector().textMatches("正则表达式")

组合定位

  • id与text属性组合
    id_text = 'resourceld("com.baidu.yuedu:id/webbooktitle").text("textname")'
    driver.find_element_by_android_uiautomator(id_text).click()
    
  • class与text属性组合
    class_text='className("android.widget.TextView").text("textname")'
    driver.find_element_by_android_uiautomator(class_text).click()
    

父子元素、兄弟元素定位

父子关系定位 childSelector

通过父元素找儿子元素

son = 'resourceld("com.baidu.yuedu:id/rl_tabs").childSelector(text("股票"))'

兄弟定位 fromParent

通过相邻的兄弟元素定位,通过兄弟元素,找到同一父级元素下的子元素

brother = 'resourceld("com.baidu.yuedu:id/lefttitle").fromParent(text("用户"))'

滚动查找元素

'new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(newUiSelector().text("查找的文本").instance(0));'

uiautomator定位实例

跟 App自动化测试 | Appium元素定位(一)的测试过程一样,判断股票价格
测试过程:

  1. 打开雪球app
  2. 点击搜索框
  3. 输入"招商银行"
  4. 选择
  5. 获取股价,并判断
import pytest
from appium import webdriver

class TestUiautomator:
    def setup(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '6.0.1'
        desired_caps['deviceName'] = '127.0.0.1:7555'
        desired_caps['appPackage'] = 'com.xueqiu.android'
        desired_caps['automationName'] = 'Uiautomator2'
        desired_caps['appActivity'] = 'com.xueqiu.android.common.MainActivity'
        desired_caps['newCommandTimeout'] = 3000
        desired_caps['noReset'] = True
        desired_caps['dontStopAppOnReset'] = True
        desired_caps['skipDeviceInitialization'] = True
        desired_caps['unicodeKeyboard'] = True
        desired_caps['resetKeybBoard'] = True
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(15)

    def teardown_method(self):
        self.driver.quit()

    def test_search(self, searchkey, type, price):
        self.driver.find_element_by_android_uiautomator(
            'new UiSelector().resourceId("com.xueqiu.android:id/tv_search")').click()
        self.driver.find_element_by_android_uiautomator(
            'new UiSelector().resourceId("com.xueqiu.android:id/search_input_text")').send_keys("招商银行")
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("SH600036")').click()
        self.driver.find_element_by_android_uiautomator('new UiSelector().text("SH600036")').click()
        current_price = self.driver.find_element_by_android_uiautomator(
            'new UiSelector().resourceId("com.xueqiu.android:id/stock_current_price")').text
        print(current_price)
        assert float(current_price) > 40

执行结果:

--THE END--

系列文章

1、Appium 介绍及环境安装
2、selenium/appium 等待方式介绍
3、App控件定位:Android 控件介绍及元素定位方法
4、Appium元素定位(一)
5、Appium元素定位(二):UiAutomator定位
6、Appium控件交互
7、Android WebView测试
8、AppCrawler自动遍历测试
9、自动遍历测试之Monkey工具
10、App自动化测试工具Uiautomator2
11、App自动化测试工具Airtest
12、Android手机管理平台搭建:STF和atxserver2
13、Windows上实现iOS APP自动化测试:tidevice + WDA + facebook-wda / appium
14、iOS APP自动化:predicate定位
15、iOS APP自动化:class chain定位方法
16、使用facebook-wda进行iOS APP自动化测试


欢迎关注公众号:「测试开发小记」及时接收最新技术文章!

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值