Appium app自动化测试经验分享-find_element_by_android_uiautomator ()【二】

本文为博主原创,未经许可严禁转载。
本文链接:https://blog.csdn.net/zyooooxie/article/details/113868447

之前分享过 find_element_by_android_uiautomator () 【一】,最近做了些新学习,做个新分享。

个人博客:https://blog.csdn.net/zyooooxie

用法

Appium app自动化测试经验分享-find_element_by_android_uiautomator ()【二】

find_element_by_android_uiautomator(uia_string)

作用:通过底层Android UiAutomator自动化测试框架定位控件。
参数:参数是UiAutomator的 UiSelector对象来定位。

官方文档 UiSelector

总结如下:

resource-id属性:
resourceId\resourceIdMatches

text属性:
text\textContains\textStartsWith\textMatches

className属性:
className\classNameMatches

description属性:
description\descriptionContains\descriptionMatches\descriptionStartsWith

fromParent:兄弟定位

Adds a child UiSelector criteria to this selector which is used to start search from the parent widget. Use this selector to narrow the search scope to sibling widgets as well all child widgets under a parent.

childSelector:父子定位

Adds a child UiSelector criteria to this selector. Use this selector to narrow the search scope to child widgets under a specific parent widget.

package属性:
packageName\packageNameMatches

index:序列【其父元素的第几个节点】

Set the search criteria to match the widget by its node index in the layout hierarchy. The index value must be 0 or greater. Using the index can be unreliable and should only be used as a last resort for matching. Instead, consider using the instance(int) method.

instance:实例【所有结果 里面的第几个元素】

Set the search criteria to match the widget by its instance number. The instance value must be 0 or greater, where the first instance is 0. For example, to simulate a user click on the third image that is enabled in a UI screen, you could specify a a search criteria where the instance is 2, the className(String) matches the image widget class, and enabled(boolean) is true. The code would look like this: new UiSelector().className(“android.widget.ImageView”) .enabled(true).instance(2);

多说一句:正则,xxxMatches,要做到所有内容 全匹配;【在UISlector使用所有的正则表达式相关的方法中都有效

代码

还是某Q极速版来举例;

操作流程:打开app-我-(右上角的)二维码-下方页面;

Appium app自动化测试经验分享-find_element_by_android_uiautomator ()【二】

ANDROID_UIAUTOMATOR = ‘-android uiautomator’

File:page_wo.py


class PageWo(BaseMethod):
    # 我
    tab_wo = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.csdn.zyooooxie:id/tab4")')

    # 右上角的二维码
    scan = (MobileBy.ANDROID_UIAUTOMATOR, 'resourceId("{}")'.format('com.csdn.zyooooxie:id/ivTitleScan'))

    # 我-断言的元素
    zyooooxie = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("zyooooxie")')

    # 二维码-断言的元素
    payment_text = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("付款")')

    # 我的二维码
    # text          我的二维码
    # resource-id   com.csdn.zyooooxie:id/ivTitleBtnLeft
    # class         android.widget.TextView【排序为0 故可用】
    # content-desc  向上导航
    
    # 父子节点  先定位父元素,通过父元素找儿子
    # 兄弟节点  通过兄弟元素,找到同一父级元素下的子元素

    # 上面是推荐的;下面是不太推荐的

    # package       com.csdn.zyooooxie 【包名感觉是 不单用,要组合用】
    # instance      int 【the first instance is 0】、【index_instance 推荐使用instance方法】
    # index         int 【be used as a last resort for matching】

    # text\textContains\textStartsWith\textMatches
    left_button_text0 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("我的二维码")')
    left_button_text1 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("我的二")')
    left_button_text2 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textStartsWith("我的二维")')
    left_button_text3 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textMatches(".的.+码$")')
    left_button_text4 = (MobileBy.ANDROID_UIAUTOMATOR, 'textMatches(".的.+码$")')

    # resourceId\resourceIdMatches
    left_button_id0 = (MobileBy.ANDROID_UIAUTOMATOR, 'resourceId("com.csdn.zyooooxie:id/ivTitleBtnLeft")')
    left_button_id1 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.csdn.zyooooxie:id/ivTitleBtnLeft")')
    left_button_id2 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceIdMatches("^com.+zyooooxie:id/ivTitleBtnLeft$")')

    # className\classNameMatches
    left_button_class0 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().className("android.widget.TextView")')
    left_button_class3 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().classNameMatches("^android.+TextView$")')
    left_button_class5 = (MobileBy.ANDROID_UIAUTOMATOR, 'className("android.widget.TextView")')

    # description\descriptionContains\descriptionMatches\descriptionStartsWith
    left_button_desc0 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().description("向上导航")')
    left_button_desc1 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().descriptionContains("上导航")')
    left_button_desc2 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().descriptionMatches("^向.+航$")')
    left_button_desc3 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().descriptionStartsWith("向上导")')
    left_button_desc5 = (MobileBy.ANDROID_UIAUTOMATOR, 'description("向上导航")')

    # fromParent:兄弟定位
    brother0 = ('-android uiautomator', 'resourceId("com.csdn.zyooooxie:id/qrcode_scanner_imageview").fromParent(resourceId("com.csdn.zyooooxie:id/qrcode_scanner_text"))')
    brother1 = ('-android uiautomator', 'new UiSelector().resourceId("com.csdn.zyooooxie:id/qrcode_group_charge_layout").fromParent(new UiSelector().resourceId("com.csdn.zyooooxie:id/qrcode_group_payment_layout"))')

    # childSelector:父子定位
    child0 = (MobileBy.ANDROID_UIAUTOMATOR, 'resourceId("com.csdn.zyooooxie:id/rlCommenTitle").childSelector(className("android.widget.TextView"))')
    child1 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.csdn.zyooooxie:id/group_qrcode_charge_paymen_layout").childSelector(description("扫一扫"))')

    # instance 和 index 都是从0开始计数,

    # index: 后续定位方式是其本身 + index是其父元素的第几个节点
    index0 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().index(1).resourceId("com.csdn.zyooooxie:id/qrcode_group_charge_layout")')
    index1 = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().index(3).resourceId("com.csdn.zyooooxie:id/qrcode_group_payment_layout")')
    index2 = (MobileBy.ANDROID_UIAUTOMATOR, 'index(5).resourceId("com.csdn.zyooooxie:id/qrcode_scanner_layout")')
    index3 = (MobileBy.ANDROID_UIAUTOMATOR, 'index(1).text("扫一扫")')
    index4 = (MobileBy.ANDROID_UIAUTOMATOR, 'index(1).description("zyooooxie,扫一扫二维码,加我QQ。")')

    # instance:匹配的所有结果 里面的第几个元素
    instance0 = ('-android uiautomator', 'new UiSelector().instance(0).className("android.widget.TextView")')
    instance1 = ('-android uiautomator', 'new UiSelector().instance(1).classNameMatches(".+TextView$")')
    instance2 = ('-android uiautomator', 'instance(2).className("android.widget.TextView")')
    instance3 = ('-android uiautomator', 'instance(3).classNameMatches(".+TextView$")')

    instance10 = ('-android uiautomator', 'instance(2).className("android.widget.ImageView")')
    instance11 = ('-android uiautomator', 'new UiSelector().instance(0).className("android.widget.ImageView")')
    instance12 = ('-android uiautomator', 'new UiSelector().instance(3).classNameMatches("^android.+geView$")')
    instance13 = ('-android uiautomator', 'instance(1).classNameMatches("^android.+geView$")')

    # packageName\packageNameMatches
    left_button_package0 = ('-android uiautomator', 'new UiSelector().packageName("com.csdn.zyooooxie").text("我的二维码")')
    left_button_package1 = ('-android uiautomator', 'packageName("com.csdn.zyooooxie").resourceId("com.csdn.zyooooxie:id/ivTitleBtnLeft")')
    left_button_package2 = ('-android uiautomator', 'new UiSelector().packageName("com.csdn.zyooooxie").description("向上导航")')
    left_button_package10 = ('-android uiautomator', 'new UiSelector().packageNameMatches("com.+zyooooxie").textContains("的二维")')
    left_button_package11 = ('-android uiautomator', 'packageNameMatches("^com.+zyooooxie").className("android.widget.TextView")')
    left_button_package12 = ('-android uiautomator', 'new UiSelector().packageNameMatches("com.+zyooooxie$").descriptionStartsWith("向上")')
    fail_package20 = ('-android uiautomator', 'new UiSelector().packageName("000.11111111.222222").descriptionStartsWith("向上")')
    fail_package21 = ('-android uiautomator', 'new UiSelector().packageNameMatches("111.+22222").textContains("的二维")')

    def QR_code(self, driver):
        self.element_click(driver, *self.tab_wo)
        self.element_click(driver, *self.scan)


pytest.ini 文件:

Appium app自动化测试经验分享-find_element_by_android_uiautomator ()【二】

@File:test_wo.py

class TestWo(PageWo):

    @pytest.mark.mark_text
    def test_text_0(self, driver):
        self.QR_code(driver)
        print(self.left_button_text0)
        self.element_click(driver, *self.left_button_text0)

        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_text
    def test_text_1(self, driver):
        self.QR_code(driver)
        print(self.left_button_text1)
        self.element_click(driver, *self.left_button_text1)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_text
    def test_text_2(self, driver):
        self.QR_code(driver)
        print(self.left_button_text2)
        self.element_click(driver, *self.left_button_text2)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_text
    def test_text_3(self, driver):
        self.QR_code(driver)
        print(self.left_button_text3)
        self.element_click(driver, *self.left_button_text3)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_text
    def test_text_4(self, driver):
        self.QR_code(driver)
        print(self.left_button_text4)
        self.element_click(driver, *self.left_button_text4)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_id
    def test_id_0(self, driver):
        self.QR_code(driver)
        print(self.left_button_id0)
        self.element_click(driver, *self.left_button_id0)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_id
    def test_id_1(self, driver):
        self.QR_code(driver)
        print(self.left_button_id1)
        self.element_click(driver, *self.left_button_id1)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_id
    def test_id_2(self, driver):
        self.QR_code(driver)
        print(self.left_button_id2)
        self.element_click(driver, *self.left_button_id2)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_class
    def test_class_0(self, driver):
        self.QR_code(driver)
        print(self.left_button_class0)
        self.element_click(driver, *self.left_button_class0)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_class
    def test_class_3(self, driver):
        self.QR_code(driver)
        print(self.left_button_class3)
        self.element_click(driver, *self.left_button_class3)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_class
    def test_class_5(self, driver):
        self.QR_code(driver)
        print(self.left_button_class5)
        self.element_click(driver, *self.left_button_class5)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_desc
    def test_desc_0(self, driver):
        self.QR_code(driver)
        print(self.left_button_desc0)
        self.element_click(driver, *self.left_button_desc0)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_desc
    def test_desc_1(self, driver):
        self.QR_code(driver)
        print(self.left_button_desc1)
        self.element_click(driver, *self.left_button_desc1)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_desc
    def test_desc_2(self, driver):
        self.QR_code(driver)
        print(self.left_button_desc2)
        self.element_click(driver, *self.left_button_desc2)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_desc
    def test_desc_3(self, driver):
        self.QR_code(driver)
        print(self.left_button_desc3)
        self.element_click(driver, *self.left_button_desc3)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_desc
    def test_desc_5(self, driver):
        self.QR_code(driver)
        print(self.left_button_desc5)
        self.element_click(driver, *self.left_button_desc5)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_index_instance
    def test_index_0(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)
        print(self.index0)

        attr_name = self.driver_find_element_and_wait(driver, *self.index0).get_attribute("name")
        print(attr_name)

        name = '收款'
        assert attr_name == name

    @pytest.mark.mark_index_instance
    def test_index_1(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.index1)
        attr_name = self.driver_find_element_and_wait(driver, *self.index1).get_attribute("name")
        print(attr_name)

        name = '付款'
        assert attr_name == name

    @pytest.mark.mark_index_instance
    def test_index_2(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.index2)
        attr_name = self.driver_find_element_and_wait(driver, *self.index2).get_attribute("name")
        print(attr_name)

        name = '扫一扫'
        assert attr_name == name

    @pytest.mark.mark_index_instance
    def test_index_3(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.index3)
        attr_id = self.driver_find_element_and_wait(driver, *self.index3).get_attribute("resourceId")
        print(attr_id)

        assert_id = 'com.csdn.zyooooxie:id/qrcode_scanner_text'
        assert assert_id == attr_id

    @pytest.mark.mark_index_instance
    def test_index_4(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.index4)
        attr_class = self.driver_find_element_and_wait(driver, *self.index4).get_attribute("className")
        print(attr_class)
        assert 'android.widget.ImageView' == attr_class

    @pytest.mark.mark_index_instance
    def test_instance_0(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance0)
        attr_text = self.driver_find_element_and_wait(driver, *self.instance0).get_attribute("text")
        print(attr_text)

        text = '我的二维码'
        assert attr_text == text

    @pytest.mark.mark_index_instance
    def test_instance_1(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance1)
        attr_text = self.driver_find_element_and_wait(driver, *self.instance1).get_attribute("text")
        print(attr_text)

        text = '收款'
        assert attr_text == text

    @pytest.mark.mark_index_instance
    def test_instance_2(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance2)
        attr_text = self.driver_find_element_and_wait(driver, *self.instance2).get_attribute("text")
        print(attr_text)

        text = '付款'
        assert attr_text == text

    @pytest.mark.mark_index_instance
    def test_instance_3(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance3)
        attr_text = self.driver_find_element_and_wait(driver, *self.instance3).get_attribute("text")
        print(attr_text)

        text = '扫一扫'
        assert attr_text == text

    @pytest.mark.mark_index_instance
    def test_instance_10(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance10)
        attr_id = self.driver_find_element_and_wait(driver, *self.instance10).get_attribute("resourceId")
        print(attr_id)

        res_id = 'com.csdn.zyooooxie:id/qrcode_group_payment_imageview'
        assert attr_id == res_id

    @pytest.mark.mark_index_instance
    def test_instance_11(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance11)
        attr_id = self.driver_find_element_and_wait(driver, *self.instance11).get_attribute("resourceId")
        print(attr_id)

        res_id = 'com.csdn.zyooooxie:id/qrcode_card_body'
        assert attr_id == res_id

    @pytest.mark.mark_index_instance
    def test_instance_12(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance12)
        attr_id = self.driver_find_element_and_wait(driver, *self.instance12).get_attribute("resourceId")
        print(attr_id)

        res_id = 'com.csdn.zyooooxie:id/qrcode_scanner_imageview'
        assert attr_id == res_id

    @pytest.mark.mark_index_instance
    def test_instance_13(self, driver):
        self.QR_code(driver)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        print(self.instance13)
        attr_id = self.driver_find_element_and_wait(driver, *self.instance13).get_attribute("resourceId")
        print(attr_id)

        res_id = 'com.csdn.zyooooxie:id/qrcode_group_charge_imageview'
        assert attr_id == res_id

    @pytest.mark.mark_package
    def test_package_0(self, driver):
        self.QR_code(driver)
        print(self.left_button_package0)
        self.element_click(driver, *self.left_button_package0)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_package
    def test_package_1(self, driver):
        self.QR_code(driver)
        print(self.left_button_package1)
        self.element_click(driver, *self.left_button_package1)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_package
    def test_package_2(self, driver):
        self.QR_code(driver)
        print(self.left_button_package2)
        self.element_click(driver, *self.left_button_package2)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_package
    def test_package_3(self, driver):
        self.QR_code(driver)
        print(self.left_button_package10)
        self.element_click(driver, *self.left_button_package10)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_package
    def test_package_4(self, driver):
        self.QR_code(driver)
        print(self.left_button_package11)
        self.element_click(driver, *self.left_button_package11)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_package
    def test_package_5(self, driver):
        self.QR_code(driver)
        print(self.left_button_package12)
        self.element_click(driver, *self.left_button_package12)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_package
    def test_package_6(self, driver):
        self.QR_code(driver)
        print(self.fail_package20)
        self.element_click(driver, *self.fail_package20)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_package
    def test_package_7(self, driver):
        self.QR_code(driver)
        print(self.fail_package21)
        self.element_click(driver, *self.fail_package21)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_child_parent
    def test_parent0(self, driver):
        self.QR_code(driver)
        print(self.brother0)
        # text有值、content-desc无值
        print(self.driver_find_element_and_wait(driver, *self.brother0).get_attribute("text"))

        self.assert_FindElement(driver, *self.payment_text, the_time=5)

    @pytest.mark.mark_child_parent
    def test_parent1(self, driver):
        self.QR_code(driver)
        print(self.brother1)
        # content-desc有值、text无值
        print(self.driver_find_element_and_wait(driver, *self.brother1).get_attribute("name"))

        self.assert_FindElement(driver, *self.payment_text, the_time=5)

    @pytest.mark.mark_child_parent
    def test_child0(self, driver):
        self.QR_code(driver)
        print(self.child0)
        self.element_click(driver, *self.child0)
        self.assert_FindElement(driver, *self.zyooooxie, the_time=5)

    @pytest.mark.mark_child_parent
    def test_child1(self, driver):
        self.QR_code(driver)
        print(self.child1)
        self.assert_FindElement(driver, *self.payment_text, the_time=5)

        self.element_click(driver, *self.child1)

if __name__ == '__main__':
    # pytest.main(['-s', '-v', 'test_wo.py', '-m', 'mark_id or mark_text'])
    # pytest.main(['-s', '-v', 'test_wo.py', '-m', 'mark_index_instance'])
    # pytest.main(['-s', '-v', 'test_wo.py'])
    pytest.main(['-v', 'test_wo.py', '--reruns', '2', '--reruns-delay', '5'])


执行结果

Appium app自动化测试经验分享-find_element_by_android_uiautomator ()【二】

Appium app自动化测试经验分享-find_element_by_android_uiautomator ()【二】

交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie

上一篇:pytest实现参数化(@pytest.mark. parametrize)


下一篇:锁优化