1. 程式人生 > >1109Appium app自動化測試經驗分享-Xpath定位總結

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
歡迎關注 微信公眾號:紫雲小站