appium targeting

Foreword

I believe we in the use of Appium, will encounter a problem, how to better in a page for a certain element for faster positioning mode. This article based on all new to Appium, the positioning of the elements is quite vague.

Appium targeting is dependent on the Selenium. So Selenium targeting methods, Appium are supported, plus targeting Android and iOS native. So to look, there are a dozen targeting methods, which use the selection, but also some luxurious.

1. Appium targeting species

Currently, Appium supported targeting methods, as follows:

cssSelector             # Selenium 最强大的定位方法,比 xpath 速度快,但比 xpath 难上手
linkText                # 链接元素的全部显示文字
partialLinkText         # 链接元素的部分显示文字
name                    # 元素的 name 属性,目前官方在移动端去掉这个定位方式,使用 AccessibilityId 替代
tagName                 # 元素的标签名 className # 元素的 class 属性 id # 元素的 id 属性 xpath # 比 css 定位方式稍弱一些的定位方法,但胜在容易上手,比较好使用,缺点就是速度慢一些。 AccessibilityId # Appium 中用于替代 name 定位方式 AndroidUIAutomator # Android 测试,最强大速度最快的定位方式 iOSNsPredicateString # iOS 谓词的定位方式,仅支持 XCTest 框架,需大于 iOS 9.3或以上 IosUIAutomation # iOS 谓词的定位方式,仅支持 UIAutomation 框架,需大于 iOS 9.3或以下 iOSClassChain # 国外大神 Mykola Mokhnach 开发类似 xpath 的定位方式,仅支持 XCTest 框架,,不如 xpath 和 iOSNsPredicateString 好 windowsAutomation # windows 应用自动化的定位方式 

Targeting methods shown above, namebecause the official reasons discarded, so is not repeated here. tagName, linkTextAnd partialLinkTextin my understanding, it is common to use a page in the web, mobile end rarely used.

Next, I think it used to talk about detailed usage targeting methods App UI automation.

1.1 className

Use the element classNameattribute positioning, support: Android and iOS, recommended.

MobileBy.className("XCUIElementTypeButton")

1.2 id

Use the element resource-idattribute positioning, support: Android, supports only Android 4.2 or above is recommended, in general, use id accurate positioning, it will use the id, location information is simple, is not prone to error. Anyway, I have not used in iOS, we have had the proper use of examples, you can share it.

MobileBy.id("package.name:id/android")

1.3 xpath

Support: Android and iOS. However, due to iOS 10 started using XCUITest framework soundtrack does not support, the positioning speed is very slow, so we do not recommend the use of the official, there are other alternative targeting methods can be used.

  1. Positioning using an absolute path, as shown xpath path screenshot

    MobileBy.xpath("className/className/className/className")
    
  2. Use positioned relative path

    MobileBy.xpath("//className")
    
  3. By positioning the element index

    MobileBy.xpath("//className[index]")
    
  4. By positioning element attributes

    MobileBy.xpath("//className[@label='更多信息']")    # 使用一种属性定位
    
    MobileBy.xpath("//className[@label='更多信息'][@isVisible='1']")    # 使用两种属性定位
    MobileBy.xpath("//className[contains(@label,'更多')]") # 使用部分属性定位(最强大) 

1.4 AccessibilityId

Replaces the previous namepositioning mode is recommended.
On Android, the main elements used content-desc属性, if the attribute is empty, can not use this targeting method.
On iOS, the main elements used labelor the name(value of the two properties are the same) property positioned, if the attribute is empty, if the attribute is empty, they can not use this property.

MobileBy.AccessibilityId("更多信息")

1.5 AndroidUIAutomator

Only supports Android 4.2 or more, support elements and a plurality of single property attributes positioned recommended.

Supports the positioning element the following attributes:

index(int index)
text(String text)
resourceId(String id)
className(String className)
packageName(String packageName)
description(String desc)
checked(boolean val)
clickable(boolean val)
enabled(boolean val)
longClickable(boolean val)
selected(boolean val)
instance(int val)
# 其他一些详细方法(包括正则表达式匹配),请查看 Android 源码中,UiSelector 类定义的方法

example:

MobileBy.AndroidUIAutomator("new UiSelector().text(\"发送\")") # 使用一种属性定位 MobileBy.AndroidUIAutomator("new UiSelector().text(\"发送\").clickable(true)") # 使用两种属性定位 

All properties are used as positioning elements, very powerful, and fast.

1.6 iOSNsPredicate

Only supports iOS 10 or more, support elements and a plurality of single property attributes positioned recommended. For details, see
iOSNsPredicate positioning .

MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton'")    # 使用一种属性定位
MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton' AND label == '更多信息'")    # 使用两种属性定位

IOSNsPredicate specific grammatical structure can check out the official documentation, or see another one of my posts.

1.7 iOSClassChain

Only supports iOS 10 or more, which is great God Mykola Mokhnach development github, and only in the framework of WebDriverAgent use for alternative xpath, but after using for a while, feeling no xpath flexibility and iOSNsPredicate good, not perfect it should. Specific use, see: iOSClassChain .

MobileBy.iOSClassChain('XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeNavigationBar[1]/XCUIElementTypeOther[1]/XCUIElementTypeButton[2]')

1.8 IosUIAutomation

Supports only iOS 9.3 or less, and the old framework are UIAutomation iOS targeting, the targeting of this type can likewise be used for positioning iOS predicate, a detailed reference: iOSNsPredicate

MobileBy.IosUIAutomation("type == 'UIAButton'")     # 使用一种属性定位

MobileBy.IosUIAutomation("type == 'UIAButton' AND label == '更多信息'")    # 使用两种属性定位

2. complex positioning element

In the version of Android parents Tony chat login page, enter the phone number and password input box information box two elements as shown below.

 
figure 1
 
figure 2

Enter the phone number and password input box box most of the properties are consistent, and alone className id has not feasible. Some students might ask, not a default input box copy (text attribute) do? Use text positioning attributes can be friends.
Upon initial login, using the text property positioned indeed, but not if the initial sign, the phone number input box remaining last login phone number, text property becomes the last login phone number, as shown in FIG.

 
image 3

In the input box like this, text attributes of the element will continue to change. During the positioning element, we will avoid the use of the property value can change as much as possible to locate.

Thus, phone number, password input box basically two single property can not be used to locate, if this is the case, we can use the multi-element positioning attributes. Most viewed in FIG. 1 and a consistent, two attributes element 2, but there are three different attributes: focused, password, instance. Only two identical combined input box attribute value, such a positioning, can be found by the elements.

Use AndroidUIAutomator positioning. UiSelector does not support password attribute positioning.

# 手机号码输入框
MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.babychat:id/edit_content\").focused(true)")
MobileBy.AndroidUIAutomator("new UiSelector().className(\"android.widget.EditText\").instance(0)")

# 密码输入框
MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.babychat:id/edit_content\").focused(false)") MobileBy.AndroidUIAutomator("new UiSelector().className(\"android.widget.EditText\").instance(1)") 

Using xpath location, does not support the use of instance attribute positioning

# 手机号码输入框
MobileBy.xpath("//android.widget.EditText[@focused='true']")
MobileBy.xpath("//android.widget.EditText[@password='false']")

# 密码输入框
MobileBy.xpath("//android.widget.EditText[@focused='false']") MobileBy.xpath("//android.widget.EditText[@password='true']") 

3. Summary

Some targeting Appium elements, generally on top as with everyone talking. As long as you want to be able to locate the elements, in particular the use of which, you have to look at personal habits. But if you want to locate elements of its properties with other elements like more, then you need to use two or even three properties are positioned, which attributes the specific use, you have one comparisons with other properties, find different properties The targeting properties of the type used, so that some of the more reliable.

This article is my learning Appium obtained in sentiment, if you have a better way, you can say it, we can discuss together and progress together!



Author: DC_ing
link: https: //www.jianshu.com/p/9f842862f883
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin www.cnblogs.com/dreamhighqiu/p/10990112.html