XP positioning (APP element positioning)

Appium app automated testing experience sharing -Xpath positioning summary

 

In my opinion, automated testing elements positioned Heaven Sword and Tulong than Xpath and CSS, but CSS is used only for Web (previously shared over), this time sharing the positioning method under Xpath. This issue tells of Xpath positioned to use App.

A) Xpath positioning

XPath is the XML Path short, it is a language used to determine the position of a part of an XML document.

XML: a markup language for storing and transferring data. The end of the suffix .xml

Tip: Xpath is a powerful language, it is because it has a very flexible positioning strategy;

Two) Xpath positioning combat

All of the following use cases used app is a god simulator , the actual operation: Click Settings - Click for more.

 

 


1. The elements to locate the attribute names and values

Source: element attribute names and values
format :( hypothesis can be uniquely locate an element)
// * [@ Resource-the above mentioned id = "XXXX"]
// *
[@ text = "XXXX"]
// * [@ Content-desc = "XXXX"]

   
        """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 不唯一,但是此元素处于第一个
ele= driver.find_element_by_xpath('//*[@content-desc="搜索"]')
# a1=driver.find_element_by_xpath("//*[@text='更多']")
# a2=driver.find_element_by_xpath("//*[@resource-id = 'com.android.settings:id/title']")
#点击
ele.click()


The tag + property name and value combinations of elements positioned

Based on: class + element attribute names and values
format :( assumptions can be uniquely positioned an element)
// 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()
  • 1
  • 2
  • 3
  • 4
    def test_xpath_03c(self):
        """ 标签 + 元素属性名和值"""
        self.xpath_find_element('//android.widget.TextView[@text="通讯录"]').click()
        self.xpath_find_element('//android.view.View[@content-desc="微信团队"]').click()
  • 1
  • 2
  • 3
  • 4

If the same attribute value of a plurality of elements positioned elements, can be indexed and positioned:. Driver.find_element_by_xpath ( '(// android.view.View [@ content-desc = "Details"]) [1]') click ( )

3. According to locate hierarchy + index

The main hierarchy is a parent locator, locate grandfather grandson
format: // * [@ attribute = ' XXXX'] / class

Index: Sort of similar class, starting at 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定位并非唯一,但是父类位于第一个
  • 1
  • 2
  • 3
  • 4
    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()
  • 1
  • 2
  • 3
  • 4
    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()
  • 1
  • 2
  • 3
  • 4

4. The locating sibling

Siblings, mainly in sub-positioned parent

Format:
// * [@ Resource - the above mentioned id = "Resource-value of the above mentioned id attribute"] / ... / class1
// * [@ Resource - the above mentioned id = "Resource-the above mentioned id attribute value"] / parent :: class / class1
unlimited sub targeting a parent

    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()
  • 1
  • 2
  • 3
  • 4
    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()
  • 1
  • 2
  • 3
  • 4

The combination of logic operations and or not positioning

Format: // * [@ 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()
  • 1
  • 2
  • 3
  • 4
    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 用于多个元素都可以唯一定位 的时候
  • 1
  • 2
  • 3
  • 4

The positioning fuzzy contains, starts-with

Format:
// * [the 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()
  • 1
  • 2
  • 3
  • 4
    def test_xpath_07b(self):
        """模糊匹配 contains"""
        self.xpath_find_element('//*[contains(@text,"录")]').click()
        self.xpath_find_element('//*[contains(@content-desc,"微信")]').click()
  • 1
  • 2
  • 3
  • 4
    def test_xpath_07c(self):
        """模糊匹配 contains"""
        self.xpath_find_element('//*[contains(@text,"通讯")]').click()
        self.xpath_find_element('//*[contains(@content-desc,"信团")]').click()
  • 1
  • 2
  • 3
  • 4
    def test_xpath_08(self):
        """模糊匹配 starts-with"""
        self.xpath_find_element('//*[starts-with(@text,"通讯")]').click()
        self.xpath_find_element('//*[starts-with(@text,"微信")]').click()
  • 1
  • 2
  • 3
  • 4
    def test_xpath_08b(self):
        """模糊匹配 starts-with"""
        self.xpath_find_element('//*[starts-with(@text,"通讯")]').click()
        self.xpath_find_element('//*[starts-with(@content-desc,"微信")]').click()
  • 1
  • 2
  • 3
  • 4

These are the sort themselves, through all the tests have to write these xpath generally less effortless; there is a hierarchical relationship is really hard to wrap around to go around very troublesome. Share Xpath positioning applied to the Web tomorrow.

 

Guess you like

Origin www.cnblogs.com/zenghongfei/p/11743971.html