Whatever we want to do in the app page, we have to find the element we want to do. For example, if you want to click a button, you have to find the button first. If you want to enter something in the input field, you must first find the input field.

This process of finding the elements to operate on is called element location.

Element location function

In most APP operations, the element to be operated on must be found first, which is called element positioning.

The function that locates elements in Appium is called find_element, and it passes two arguments. The first argument is the way the element is positioned, and the second argument is the value of that way. For example, to locate an element by ID, the first argument is the ID, and the second argument is the value of the ID.

el = driver.find_element('id', 'value')
Copy the code

The resulting EL is an element object, and getting the attributes of the element is simple:

El.get_attribute ('clickable') # Get the element clickable attributeCopy the code

Element location tool: UIAutomatorViewer

So, where do I get the value of id? There’s a Tools directory in the SDK, and that directory has a tool called UIAutomatorViewer that looks at the properties of each element, including the ID properties of course. Go directly to the corresponding file, click to see its interface.

To view the attributes of an element, simply move the mouse over the element and the attributes for each element are displayed to the right. It’s kind of a hassle to click on uiAutomatorViewer every time, to create a shortcut, put it on our desktop, and when you need it, just click on the shortcut on the desktop to open it.

Element positioning

There are several main elements positioning methods for app automated testing:

  • MobileBy.XPATH
  • MobileBy.ID
  • MobileBy.ACCESSIBILITY_ID
  • MobileBy.ANDROID_UIAUTOMATOR
  • MobileBy.CLASS_NAME

Id and xpath are the two most commonly used and syntactically common methods. Try to use id to locate the element, and use xpath elements if the element has no ID attribute or id is not unique.

Xpath is a universal path location language, which can be used in both Web pages and APP pages. It can combine a variety of attribute conditions to make the location expression more accurate. For specific Xpath usage, you can refer to the location method of Xpath elements.

Secondly, Android Uiautomator is suitable for Java language, which is not very convenient to use in other languages. If necessary, please refer to the instructions on the official website.

You can locate elements by looking at page properties and putting the copied properties into functions. Say :id/username = com.keda.say:id/username

driver.find_element('id', 'com.keda.say:id/username')
Copy the code

You can also write it as an Xpath expression:

driver.find_element('xpath', '//*[@id="com.keda.say:id/username"]')
Copy the code

Get element attributes

When the find_element method locates an element, it returns an element object that can be used to retrieve the element’s attributes, or to call its methods and action events.

  • element.location_in_view
  • element.location
  • element.rect
  • element.text
  • Element. Get_attribute (‘ clickable ‘)

conclusion

In either case, UI automation requires that the find_element method be used to retrieve an element from its location and then perform operations on that element.

I am nine handle, thank you for your patience to read, see you next time.