How to do Appium Android automatic element positioning

Get into the habit of writing together! This is the second day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

No matter what operation we want to perform on the app page, we must first find the element to operate on. For example, if you want to click a button directly, then you have to find the button first. If you want to enter something in the input box, then you must find the input box first.

This process of finding the element that needs to be manipulated is called element positioning.

element positioning function

In most of the APP operation process, you must first find the element that needs to be operated, this process is called element positioning.

The function for element positioning in Appium is called find_element, which passes two parameters. The first parameter is the way to locate the element, and the second parameter is the value of this way. For example, to locate an element by ID, the first parameter is filled with id, and the second parameter is the value of ID.

el = driver.find_element('id', 'value')
复制代码

The obtained el is an element object, and getting the attributes of the element is very simple:

el.text  #获取元素文本
el.rect  #获取元素坐标
el.get_attribute('clickable')  #获取元素clickable属性
复制代码

Element positioning tool: uiautomatorviewer

So, where to get the value of id? There is a tools directory in the SDK, this directory has a tool called uiautomatorviewer, which can view the attributes of each element, including the id attribute of course. Find the corresponding file directly and click it to see its interface.

To view the properties of an element, simply move the mouse over the element and the properties of each element will be displayed on the right. Every time you click on the uiautomatorviewer tool, it will be more troublesome, right? Create a shortcut access method and put it on our desktop. When you need it, just click this shortcut on the desktop to open it.

https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/33103996fae54e9e9622ca251aad8baa~tplv-k3u1fbpfcp-zoom-1.image

element positioning

There are several ways to locate the main elements of app automation testing:

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

id and xpath are the two most commonly used and common syntax methods. Try to use id to locate. If the element does not have an id attribute or the id is not unique, use the xpath element positioning method.

Xpath is a general path positioning language, which can be used both in web pages and app pages. It can make positioning expressions more precise by combining various attribute conditions. For specific Xpath usage, you can refer to XPath elements positioning method .

Secondly, Android Uiautomator is suitable for use in Java language, and other languages ​​are not very convenient to use. If necessary, you can refer to the official website for usage.

You can locate the element by looking at the page properties and placing the copied properties into the function. For example, if the id of an element is viewed com.keda.say:id/username, the corresponding code is:

driver.find_element('id', 'com.keda.say:id/username')
复制代码

It can also be transcribed as an Xpath expression:

driver.find_element('xpath', '//*[@id="com.keda.say:id/username"]')
复制代码

get element attribute

After the find_element method locates the element, it will get an element object, which can be used to obtain the attributes of the element, or call the methods and operation events of the attributes.

  • element.location_in_view
  • element.location
  • element.rect
  • element.text
  • element.get_attribute(‘clickable’)

Summarize

No matter what the situation is, as long as you use the find_element method for ui automation, you can obtain the element through element positioning, and then you can perform operations on the element later.

I am Jiushan, thank you for your patient reading, see you next time.

Guess you like

Origin juejin.im/post/7081881256807366670