Practices and Suggestions on positioning methods of Appium based on various FindElement controls of Android

Source: Internet
Author: User
Tags appium

Practices and Suggestions on positioning methods of Appium based on various FindElement controls of Android

Try various findElement methods of AppiumDriver. The target application is the Notepad application that comes with the SDK.

1. findElementByName1.1 example

        el = driver.findElementByName("Add note");        assertThat(el.getText(),equalTo("Add note"));        
1.2 How to obtain Name

Android devices did not find a suitable method, tried to use Appium Inspector, but used the latest "AppiumForWindows-1.2.3.1" to not see this property, and Inspector is very unstable under Windows, crash is easy. We sincerely hope that the Appium team will solve this problem as soon as possible.

IOS devices can be obtained using Appium Inspector (from the Internet)


1.3 suggestions

I personally suggest you try to use the text displayed in the view as the name to check whether the control can be obtained. In my personal experience, the control is generally successful, therefore, I doubt whether the name of the control on Android is equal to text. If the problem persists, You have to discard the name. Or wait for the stable inspector release of Appium to check whether the control name can be obtained. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel + rbn9yrG2 + kernel/rysdBcHBpdW3NxbbTz + kernel + Mi4xIMq + wP08L2gyPgo8cD48L3A + kernel "brush: java;"> el = driver. findElementByAndroidUIAutomator ("new UiSelector (). text (\ "Add note \") "); assertThat (el. getText (), similar to ("Add note "));

2.2 how to obtain the UIAutomator Parameter

You can use the UiSelector object to search for controls in various ways. For example, you can use the text of the view to search for controls in the current window, in the future, we will create another article to describe how to obtain the control through UIAUtomator, and then apply it directly.

3. findElementByClassName3.1 example

        el = driver.findElementByClassName("android.widget.TextView");        assertThat(el.getText(),equalTo("Add note"));

3.2 How to obtain the control's ClassName

You can use the UIAutomatorViewer tool to directly view

3.3 suggestions

When ClassName is used, more than one view is obtained. Therefore, you must traverse the obtained views and use the search criteria to obtain the target control. In this example, there is only one textview control in the window, so no traversal is required.

4. findElementById4.1 example

        el = driver.findElementById("android:id/title");        assertThat(el.getText(),equalTo("Add note"));

4.2 How to obtain the Resource Id

You can use UIAutomatorViewer to obtain

4.3 If the API Level of the target device is lower than 18, UIAutomatorViewer cannot obtain the corresponding Resource ID, which can be used only when the API Level of the target device is greater than 18.

5. findElementByAccessibilityId5.1 example
    el = driver.findElementByAccessibilityId("menu_add_note_description");    assertThat(el.getText(),equalTo("node"));
5.2 how to obtain the AccessibilityId can be obtained through UIAutomatorViewer or Appium Inspector. The 5.3 annotation Accessibility ID is equivalent to contentDescription on Android. This attribute is convenient for some people with physiological functional defects to use the application. For example, if we have an ImageView with a complex color picture, some blind people may not be able to tell what the image is. If you have installed a secondary browser tool such as TalkBack, TalkBack will read the content you are currently browsing. The TextView control TalkBack can directly read the content, but the ImageView TalkBack can only read the value of contentDescription to tell the user what the image is.
As this is a hidden property, various properties used to search for controls on Android may be missing or duplicated (such as duplicate IDs, for example, all the items under a list may be called "id/text1"), so the best way is to communicate with the development team and assign each view a unique contentDescription. 6. findElementByCssSelectorTBD. This method should be used to locate controls under the WebView container. Because Native apps are currently not used, mark them first and add them as needed. 7. findElementByLinkTextTBD. This method should be targeted at the controls under the WebView container, because it is right that the Native App is not used yet, so Mark it first and add it as needed in the future.
8. findElementByPartialLinkTextTBD. This method should be used to locate the control under the WebView container, because it is right that the Native App is not used yet, so Mark it first and add it as needed in the future. 9. findElementByTagName this method Appium1.0 has expired and is replaced by the above findElementByClassName. Please refer to the https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/migrating-to-1-0.md10.findEelementByXPath10.1 example
        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 you upgrade Appium0.18.x of the old version to Appium1.x, pay attention to the changes in the class name and xpath policies: You need to use FQCN to describe your control. That is to say, the original:

FindElementByXpath ("" // TextView [contains (@ text, 'add note')] "")

Change

FindElementByXpath ("// android. widget. TextView [contains (@ text, 'add note')]")

For details about the changes, see instructions for migrating Appium0.18.x to Appium1.x.

10.3 reference is said to be not very stable: https://github.com/appium/appium/issues/3371bootstraponline commented on 13 Aug

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 the getPageSource function of AppiumDriver, which can print the elements of the current page in XML format, it is recommended that you call this function when you cannot find the corresponding attribute of the view to slowly find ^_^.

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.