"Turn" appium the practice of the control localization method based on the various findelement of Android

Source: Internet
Author: User
Tags appium

Original address: http://blog.csdn.net/zhubaitian/article/details/39754041#t11

Attempts to appiumdriver various findelement methods, the target application of which is the SDK comes with the Notepad application.

1. findElementByName1.1 Example

[Java]View Plaincopy
    1. el = Driver.findelementbyname ("Add note");
    2. Assertthat (El.gettext (), Equalto ("Add note");
1.2 How to Get Name

Android device did not find a suitable method, try to use Appium Inspector, but using the current latest "AppiumForWindows-1.2.3.1" did not see this property, and Inspector under Windows is very unstable, It's easy to crash. Sincerely expect the Appium team to solve this problem as soon as possible

iOS devices can be obtained with Appium inspector (on tablets from the Internet)

1.3 Recommendations

Personal suggestions can try to use the text of the view display as the name to see if you can get the control, according to my personal experience is generally successful, so I am very suspicious of Android above the name of the control is equal to text. If it does not work, you have to give up the name. Or wait for Appium's later stable inspector to be released to see if the control's name can be obtained.

This method after Appium1.0 in fact is outdated and to be replaced by Findelementbyaccessibilityid, do not know why can still call, guess is Appium team want to retain a certain compatibility to smooth over it. Please view: https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/migrating-to-1-0.md

2. findElementByAndroidUIAutomator2.1 Example

[Java]View Plaincopy
    1. el = Driver.findelementbyandroiduiautomator ("New Uiselector (). Text (\" Add note\ ")");
    2. Assertthat (El.gettext (), Equalto ("Add note");

2.2 How to get uiautomator parameters

Uiautomator get controls in a variety of ways, all through the Uiselector object to find, such as using the text of the view of the current window to find the control, not to do this, and then another article to describe the way uiautomator get control, You can apply it directly to it.

3. findElementByClassName3.1 Example

[Java]View Plaincopy
    1. el = Driver.findelementbyclassname ("Android.widget.TextView");
    2. Assertthat (El.gettext (), Equalto ("Add note");

3.2 How to get the classname of a control

You can use the Uiautomatorviewer tool to view directly

3.3 Recommendations

There is more than one view used with classname, so it should be necessary to iterate through the resulting views and then abbreviate the search criteria to get the target control. In the example, you do not need to traverse because there is only one TextView control above the window.

4. findElementById4.1 Example

[Java]View Plaincopy
    1. el = Driver.findelementbyid ("Android:id/title");
    2. Assertthat (El.gettext (), Equalto ("Add note");

4.2 How to get resource Id

can be obtained by uiautomatorviewer

4.3 recommendations

If the target device's API level is below 18, Uiautomatorviewer cannot get the corresponding resource ID, only if it is greater than 18.

5. findElementByAccessibilityId5.1 Example [Java]View Plaincopy
    1. el = driver.findelementbyaccessibilityid ( "menu_add_note_description")   
    2. Assertthat (El.gettext (), Equalto ( "node");   
5.2 How to get Accessibilityid can get 5.3 comments via Uiautomatorviewer or Appium Inspector accessibility ID on Android is equivalent to ContentDescription, this property is convenient for some of the physiological functions of the people who have a flaw in the use of the application. For example, we have a imageview inside to put a color complex picture, perhaps some blindness colour-blind people, can not tell the picture of what is painted. If a user installs a secondary Browse tool such as talkback,talkback, it will read aloud what the user is currently browsing. TextView control talkback can read the contents directly, but ImageView talkback can only read the value of ContentDescription, tell the user what this picture is. Since this is a hidden property, the various properties on Android that are used to find controls may be missing or duplicated (such as ID duplicates, for example, all items under a list may be called "id/text1"), So the best way to communicate with the development team is to give each view a unique contentdescription. 6. findelementbycssselectortbd, this method should be targeted at the WebView container under the control, because now for the native app is not yet used, so the first mark, in the future when needed to add. 7. findelementbylinktexttbd, this method should be targeted at the WebView container under the control, because now this is very right is native app is not yet used, so the first mark, in the future need to add. 8. findelementbypartiallinktexttbd, this method should be targeted at the WebView container under the control, because now this is very right is native app is not yet used, so the first mark, in the future need to add. 9.findElementByTagName This method Appium1.0 later has been obsolete and replaced by the above findelementbyclassname, please see https://github.com/appium/appium/ blob/master/docs/en/advanced-concepts/migrating-to-1-0.md10.findeelementbyxpath10.1 Example [Java]View Plaincopy
    1. el = Driver.findelementbyxpath ("//android.widget.textview[contains (@text, ' Add note ')]");
    2. el = Driver.findelement (By.xpath ("//android.widget.textview"));
    3. Assertthat (El.gettext (), Equalto ("Add note");
10.2 XPath format changes

After upgrading from the old version of appium0.18.x to the current appium1.x, note the change in class name and XPath policy: You now need to use FQCN to describe your control. In other words, the original:

Findelementbyxpath (""//textview[contains (@text, ' Add note ')] ")

Need to change into

Findelementbyxpath ("//android.widget.textview[contains (@text, ' Add note ')]"

For detailed changes please see "appium0.18.x Migration to appium1.x"

10.3 References are said to be not very stable: Https://github.com/appium/appium/issues/3371bootstraponline commented on

Sometimes uiautomator fails to create the dump.xml. A Client side retry may help. I Don ' t think there ' s much we can do about the problem until Google fixes uiautomator.

11. Ultimate Method: Appiumdriver Getpagesource finally introduces Appiumdriver's Getpagesource function, which can print the elements of the current page in XML. It is recommended that you call this function when you cannot find the corresponding property of the view to slowly find the ^_^

"Turn" appium the practice of the control localization method based on the various findelement of Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.