-Xpathポジショニングの概要を共有するAppiumアプリの自動テスト経験
私の意見では、自動テスト要素は、XPathやCSSよりも天剣とTulongを位置付けますが、CSSのみをWeb用に使用されている(以前オーバー共有)、今回は、XPathの下に位置決め方法を共有します。この問題は、アプリケーションを使用するように配置されたXpathを伝えます。
A)のXpathポジショニング
XPathは、それがXML文書の一部の位置を決定するために使用される言語で、XMLパス短いです。
XMLデータを記憶し、転送するためのマークアップ言語。サフィックスの最後の.xml
ヒント:XPathは、それは非常に柔軟なポジショニング戦略を持っているので、それは、強力な言語です。
二つ)のXpath位置決め戦闘
次の使用例はすべてのアプリがある使用神シミュレータ、実際の操作:[設定]をクリックします-クリックして詳細を参照。
1.要素は、属性名と値を検索します
出典:要素は、属性名と値
)要素を探し一意にすることができフォーマット:(仮説を
// * [リソース-上記ID = "XXXX" @]
// * [@テキスト= "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 = ドライバ。find_element_by_xpath('// * [コンテンツ-DESC = "搜索" @]')
#A1 = driver.find_element_by_xpath( "// * [@テキスト= '更多']")
#A2 = driver.find_element_by_xpath( "// * [リソース-ID = @ 'com.android.settings:ID /タイトル'] ")
#点击
ELE。クリック()
位置決め要素のタグ+プロパティ名と値の組み合わせ
基づく:クラス+エレメント属性名と値
のフォーマットを:(仮定は、一意の要素を配置することができる)
//クラス[リソース-ID =「XXXX」@]
[テキスト=「XXXX」@] //クラス
の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
要素を配置複数の要素の同じ属性値は、索引付け及び位置決めすることができる場合:. Driver.find_element_by_xpath(「(// android.view.View [コンテンツDESC @ = 『詳細』])[1]」)(クリック)
3.階層+インデックスを検索するためによると
主な階層は、祖父の孫の見つけ、親ロケータです
// * [@属性=「:形式を XXXX」] /クラス
インデックス:類似したクラスのソート、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.位置決め兄弟
主副位置決め親に兄弟、
フォーマット:
// * [リソース@ -上記のIDを述べ=「上記のid属性を言及したのリソース値」] / ... / class1に
// * [リソース@ -上記のIDを述べた=「リソース・上記のid属性値を述べた」] /親::クラス/ 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()
- 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
論理演算の組み合わせかポジショニング
フォーマット:// * [@ attribute1と= 'XXXX' と@ 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
ファジーが含まれている位置は、開始さ-で
フォーマット:
// * [含まれています(@属性'XXXX')]
// * [開始-と(@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
これらは、すべてのテストが一般的にはあまり楽これらのXPathを記述する必要がありますを通じて、自分自身ソートされ、階層的な関係は非常に面倒周りに行くことを周りにラップするために本当にハードがされています。株式のXpathポジショニングは明日のWebに適用されます。