1109Appium app自动化测试经验分享-Xpath定位总结

在我看来,自动化测试中元素定位的倚天剑和屠龙刀莫过于 Xpath和CSS,但CSS只用于Web(之前已经分享过),这次就分享下Xpath的定位方法。本期讲的是Xpath定位运用到App。

一)Xpath定位

XPath即为XML Path 的简称,它是一种用来确定XML文档中某部分位置的语言。

XML:一种标记语言,用于数据的存储和传递。 后缀.xml结尾

提示:Xpath为强大的语言,那是因为它有非常灵活定位策略;

二)Xpath定位实战

以下所有用例所用app是微信谷歌市场版,实际操作:点击通讯录-点击微信团队。

1.根据 元素属性名和属性值 来定位

依据:元素属性名和值
格式:(假设都可以唯一定位某元素)
//[@resource-id=“XXXX”]
//
[@text=“XXXX”]
//*[@content-desc=“XXXX”]

    def test_xpath_02(self):
        """xpath 根据元素属性名和属性值来定位"""
        self.xin_find_element(By.XPATH, '//*[@text="通讯录"]').click()     # text属性值是通讯录 可以唯一定位
        self.xin_find_element(By.XPATH, '//*[@resource-id="com.tencent.mm:id/ik"]').click()     # resource-id属性值是com.tencent.mm:id/ik 不唯一,但是此元素处于第一个
    def test_xpath_02b(self):
        """xpath 根据元素属性名和属性值来定位"""
        self.xin_find_element(By.XPATH, '//*[@text="通讯录"]').click()
        self.xin_find_element(By.XPATH, '//*[@text="微信团队"]').click()  # text属性值是微信团队 可以唯一定位
    def test_xpath_02c(self):
        """xpath 根据元素属性名和属性值来定位"""
        self.xpath_find_element('//*[@text="通讯录"]').click()
        self.xpath_find_element('//*[@content-desc="微信团队"]').click()     # desc属性值是微信团队 可以唯一定位

2.根据 标签 + 元素属性名和值 组合定位

依据:class + 元素属性名和值
格式:(假设都可以唯一定位某元素)
//class[@resource-id=“XXXX”]
//class[@text=“XXXX”]
//class[@content-desc=“XXXX”]

    def test_xpath_03b(self):
        """ 标签 + 元素属性名和值"""
        self.xpath_find_element('//android.widget.TextView[@text="通讯录"]').click()
        self.xpath_find_element('//android.view.View[@text="微信团队"]').click()
    def test_xpath_03c(self):
        """ 标签 + 元素属性名和值"""
        self.xpath_find_element('//android.widget.TextView[@text="通讯录"]').click()
        self.xpath_find_element('//android.view.View[@content-desc="微信团队"]').click()

3.根据 层级关系+索引 来定位

层级关系 主要是 父定位子,爷爷定位孙子
格式://*[@attribute=‘XXXX’]/class

索引:同类class排序,从1开始

    def test_xpath_04(self):
        """层级关系、索引"""
        self.xpath_find_element('//android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.LinearLayout[@resource-id="com.tencent.mm:id/bwj"]/android.widget.TextView').click()
        self.xpath_find_element('//android.widget.RelativeLayout[@resource-id="com.tencent.mm:id/ih"]/android.view.View').click()     # 父类的id定位并非唯一,但是父类位于第一个
    def test_xpath_04b(self):
        """层级关系、索引"""
        self.xpath_find_element('//android.widget.LinearLayout/android.widget.RelativeLayout[2]').click()
        self.xpath_find_element('//android.widget.ListView[@resource-id="com.tencent.mm:id/i2"]/android.widget.LinearLayout[2]/android.widget.LinearLayout').click()
    def test_xpath_04c(self):
        """层级关系、索引"""
        self.xpath_find_element('//android.widget.LinearLayout[@resource-id="com.tencent.mm:id/bwj"]/android.widget.TextView[@text="通讯录"]').click()
        self.xpath_find_element('//android.widget.LinearLayout/android.widget.RelativeLayout/android.view.View[@text="微信团队"]').click()

4.根据 兄弟节点 来定位

兄弟节点 主要在子定位父

格式:
// *[ @ resource - id = “resource-id属性值”] /…/ class1
// *[ @ resource - id = “resource-id属性值”] / parent::class/class1
可以无限 子定位父

    def test_xpath_05(self):
        """兄弟节点"""
        self.xpath_find_element('//android.widget.RelativeLayout/../android.widget.TextView[@text="通讯录"]').click()
        self.xpath_find_element('//android.widget.TextView[@text="W"]/parent::*/android.widget.LinearLayout').click()
    def test_xpath_05b(self):
        """兄弟节点"""
        self.xpath_find_element('//com.tencent.mm.ui.mogic.WxViewPager[@resource-id="com.tencent.mm:id/auh"]/parent::android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout[2]').click()
        self.xpath_find_element('//android.view.View[@resource-id="com.tencent.mm:id/i5"]/parent::android.widget.RelativeLayout/parent::android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout[2]/android.widget.LinearLayout').click()

5. 根据 逻辑运算 and or not 组合定位

格式: //*[@attribute1=‘XXXX’ and @attribute2=‘XXXXXX’]

    def test_xpath_06(self):
        """逻辑运算 and or not """
        self.xpath_find_element('//*[@text="通讯录" and @class="android.widget.TextView"]').click()
        self.xpath_find_element('//*[@text="微信团队" and @resource-id="com.tencent.mm:id/ik"]').click()
    def test_xpath_06b(self):
        """逻辑运算 and or not """
        self.xpath_find_element('//*[@text="通讯录" and @resource-id="com.tencent.mm:id/bwm"]').click()    # and 用于多个元素可以唯一定位的时候
        self.xpath_find_element('//*[@text="微信团队" or @content-desc="微信团队"]').click()    # or 用于多个元素都可以唯一定位 的时候

6.根据 模糊定位contains、starts-with

格式:
//*[contains(@attribute,‘XXXX’)]

//*[starts-with(@attribute,“XXXX”)]

    def test_xpath_07(self):
        """模糊匹配 contains"""
        self.xpath_find_element('//*[contains(@text,"讯")]').click()
        self.xpath_find_element('//*[contains(@text,"团队")]').click()
    def test_xpath_07b(self):
        """模糊匹配 contains"""
        self.xpath_find_element('//*[contains(@text,"录")]').click()
        self.xpath_find_element('//*[contains(@content-desc,"微信")]').click()
    def test_xpath_07c(self):
        """模糊匹配 contains"""
        self.xpath_find_element('//*[contains(@text,"通讯")]').click()
        self.xpath_find_element('//*[contains(@content-desc,"信团")]').click()
    def test_xpath_08(self):
        """模糊匹配 starts-with"""
        self.xpath_find_element('//*[starts-with(@text,"通讯")]').click()
        self.xpath_find_element('//*[starts-with(@text,"微信")]').click()
    def test_xpath_08b(self):
        """模糊匹配 starts-with"""
        self.xpath_find_element('//*[starts-with(@text,"通讯")]').click()
        self.xpath_find_element('//*[starts-with(@content-desc,"微信")]').click()

这些都是自己整理、全部测试通过得,写这些xpath一般不太费力气;就是层级关系那儿真的是辛苦,来回绕来绕去得很麻烦。明天分享Xpath定位运用到Web。

交流技术 欢迎+QQ 153132336 zy
欢迎关注 微信公众号:紫云小站

猜你喜欢

转载自blog.csdn.net/zyooooxie/article/details/83865511