Appium control positioning for various findelement based on Android

Source: Internet
Author: User
Tags xpath appium

Transferred from: http://www.2cto.com/kf/201410/340345.html

1. findElementByName1.1 Example

?
12 el = driver.findElementByName("Add note");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. < yo? " http://www.2cto.com/kf/ware/vc/"target=" _blank "class=" Keylink ">vcd4kpha+ 1ek49re9t6ju2kfwcgl1bteumnauuvpg5mq10tg+rbn9yrg2+nkqsbtmaw5krwxlbwvudej5qwnjzxnzawjpbgl0eulkykg0+ rxdwcujrlk71qq1wm6qyrldtlu5xny199pdo6yyws/rysdbchbpdw3nxbbtz+ Uxo8h00ru2qlxevobi3ddu0ttgvbusuf22yldjoaph67lpv7sjumh0dhbzoi8vz2l0ahvilmnvbs9hchbpdw0vyxbwaxvtl2jsb2ivbwfzdgvyl2rvy3mvzw4 Vywr2yw5jzwqty29uy2vwdhmvbwlncmf0aw5nlxrvltetmc5tzdwvcd4kpggxpjiuigzpbmrfbgvtzw50qnlbbmryb2lkvulbdxrvbwf0b3i8l2gxpgo8adi +mi4ximq+wp08l2gypgo8cd48l3a+cjxwcmugy2xhc3m9 "Brush:java;" > el = Driver.findelementbyandroiduiautomator ("New Uiselector (). Text (\" Add note\ ")"); 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

?
12 el = driver.findElementByClassName("android.widget.TextView");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

?
12 el = driver.findElementById("android:id/title");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?
12 el = driver.findElementByAccessibilityId("menu_add_note_description");assertThat(el.getText(),equalTo("node"));
5.2 How to get Accessibilityid

can be obtained by uiautomatorviewer or Appium inspector

5.3 Notes

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. Findelementbycssselector

TBD, 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. Findelementbylinktext

TBD, 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. Findelementbypartiallinktext

TBD, 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.md

10.findeelementbyxpath10.1 example?
123 el = driver.findElementByXPath("//android.widget.TextView[contains(@text,‘Add note‘)]");//el = driver.findElement(By.xpath("//android.widget.TextView"));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

Supposedly not very stable: https://github.com/appium/appium/issues/3371 Bootstraponline 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 introduce the Getpagesource function of Appiumdriver, you can print the elements of the current page in XML, it is recommended that you do not find the corresponding properties of the view when the function is called to slowly find the ^_^

Appium control positioning for various findelement based on Android

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.