ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-01-18 20:59:25  阅读:408  来源: 互联网

标签:定位 Appium UiSelector text UiAutomator caps desired new android


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--

文章标题:Appium元素定位(二):UiAutomator定位
本文作者:hiyo
本文链接:https://blog.csdn.net/u010698107/article/details/111433778
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!

标签:定位,Appium,UiSelector,text,UiAutomator,caps,desired,new,android
来源: https://blog.csdn.net/u010698107/article/details/111433778

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有