Introduction to uiautomatorviewer
Element positioning is mainly used to obtain element information. After obtaining element information, you can use the relevant API s provided by appium to identify and operate elements. In the Android SDK, Google provides the element location tool uiautomatorviewer, which can be found in the Android SDK installation path:
<android-sdk>\tools\bin\uiautomatorviewer.bat
Bloggers are in this path
D:\Android\android-sdk-windows\tools
Operation uiautomatorviewer
After opening, it's like this
Locate app interface
Use steps:
1. Open the app to be recognized on the virtual machine or real machine
2. In the command window, enter the uiautomatorviewer command
3. Then click the DeviceScreenshot button to connect the phone
It's not a big problem. Let me have a little operation
Step 1:
# Search for app uix adb shell uiautomator dump /sdcard/app.uix # Path stored locally adb pull /sdcard/app.uix D:\Android
Step 2:
# Search for pictures adb shell screencap -p /sdcard/app.png # Save picture to local adb pull /sdcard/app.png D:\Android\app.png
Local folder path to store files
After completing the above operations, we import these two files
After importing, you can identify the page elements of the app, but few people use it. One is that the software is not very good, which is incompatible with some models, and the other is that the operation is not very friendly, so let's introduce Appnium below
Appnium
In fact, we have introduced the operating principle of appnium before. Let's review it later
Appium supports multiple platforms, including MAC and Windows. It develops appium server for these two platforms
appium also supports Android and iOS operating systems, which can be applied to different mobile devices, covering most mobile device models in the market.
So we don't talk much and then we'll operate
Appnium action
- Get the device id first
adb services
- Get package name
adb shell dumpsys window | findstr mCurrentFocus
Package name
mCurrentFocus=Window{c854f2d u0 com.android.calculator2/com.android.calculator2.Calculator}
- Open Appnium
- Open pychar new demo
# -*- coding: utf-8 -*- # @Time : 2022/1/14 10:46 # @Author : Limusen # @File : demo_connect_01 from appium import webdriver des = { "platformName": "Android", "platformVersion": "9.0", # System version "deviceName": "Samsung Galaxy S9", "appPackage": "com.android.calculator2", # Package name "appActivity": "com.android.calculator2.Calculator", # Active package name "udid": "192.168.0.101:5555", # Equipment number "noReset": "True", "unicodeKeyboard": "True", "resetKeyboard": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)
After running the code, our calculator app will automatically open
- Code illustration
Appium element identification
First, we go to this page, and then click the small search button
- Fill in the parameters and click start session
Then we enter the interface of identifying elements
Appium element positioning
The positioning method of app is similar to the UI we learned earlier. The following will describe the relevant positioning methods
Xpath
Absolute path
The full path of the element, including all nodes. This method is very long and inefficient; On the other hand, because too many layers are involved, once any one of the middle layers changes, the element cannot be located
Assuming that the element is in position 8, all paths from position 1 to position 7 must be written, and there are many elements in position 7, which can be represented by subscript [2]
- Code illustration
It should be noted here that why use a double slash at the beginning?
In fact, it is because it is not a top-level directory. There is a directory on it. If you don't believe it, you can try to remove the double slash and see what you click
- Sample code
# -*- coding: utf-8 -*- # @Time : 2022/1/14 10:46 # @Author : Limusen # @File : demo_connect_01 from appium import webdriver des = { "platformName": "Android", "platformVersion": "9.0", "deviceName": "Samsung Galaxy S9", "appPackage": "com.android.calculator2", "appActivity": "com.android.calculator2.Calculator", "udid": "192.168.0.101:5555", "noReset": "True", "unicodeKeyboard": "True", "resetKeyboard": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # Absolute positioning driver.find_element_by_xpath( '//android.widget.FrameLayout/android.widget.FrameLayout' '/android.widget.FrameLayout/android.view.ViewGroup' '/android.widget.LinearLayout/android.widget.LinearLayout[2]' '/android.view.ViewGroup[1]/android.widget.Button[2]').click()
- Code illustration
Attribute positioning
give an example:
Textproperties:
driver.find_element_by_xpath('/ / class name [@ text=text value]')
Resource id attribute: equivalent to id
driver.find_element_by_xpath('/ / class name [@ resource id = attribute value]')
Content desc attribute:
driver.find_element_by_xpath('/ / class name [@ content desc = attribute value]')
class attribute:
driver.find_element_by_xpath('/ / class name')
Multi attribute: and indicates and or indicates or
driver.find_element_by_xpath('/ / class name [@ attribute name = attribute value and/or @ attribute name = attribute value]')
- Sample code
# -*- coding: utf-8 -*- # @Time : 2022/1/14 10:46 # @Author : Limusen # @File : demo_connect_01 from appium import webdriver des = { "platformName": "Android", "platformVersion": "9.0", "deviceName": "Samsung Galaxy S9", "appPackage": "com.android.calculator2", "appActivity": "com.android.calculator2.Calculator", "udid": "192.168.0.101:5555", "noReset": "True", "unicodeKeyboard": "True", "resetKeyboard": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) # Absolute positioning driver.find_element_by_xpath( '//android.widget.FrameLayout/android.widget.FrameLayout' '/android.widget.FrameLayout/android.view.ViewGroup' '/android.widget.LinearLayout/android.widget.LinearLayout[2]' '/android.view.ViewGroup[1]/android.widget.Button[2]').click() # Use the text attribute to locate '/ / attribute value [@ text = ""]' driver.find_element_by_xpath('//android.widget.Button[@text="+"]').click() # click the + sign # Use resource ID '/ / attribute value [@ resource id = ""]' driver.find_element_by_xpath('//android.widget.Button[@resource-id="com.android.calculator2:id/digit_6"]').click() # click 6 # Using bounds driver.find_element_by_xpath('//android.widget.Button[@bounds="[1184,2482][1412,2764]"]').click() # Click=
- Code illustration
Partial attribute positioning
Use partial attribute content positioning: this method can be used when the attribute value of an element is too long or there are dynamic changes in the element attribute value content.
Start position match:
starts-with()driver.find_element_by_xpath('/ / class name [starts with (@ attribute name, some attribute values)]')
Include matches:
contains()driver.find_element_by_xpath('/ / class name [contains (@ attribute name, some attribute values)]')
End position match:
ends-with()driver.find_element_by_xpath('/ / class name [ends with (@ attribute name, some attribute values)]')
- Sample code
# -*- coding: utf-8 -*- # @Time : 2022/1/14 10:46 # @Author : Limusen # @File : demo_connect_01 from appium import webdriver des = { "platformName": "Android", "platformVersion": "9.0", "deviceName": "Samsung Galaxy S9", "appPackage": "com.android.calculator2", "appActivity": "com.android.calculator2.Calculator", "udid": "192.168.0.101:5555", "noReset": "True", "unicodeKeyboard": "True", "resetKeyboard": "True" } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) driver.find_element_by_xpath( '//android.widget.FrameLayout/android.widget.FrameLayout' '/android.widget.FrameLayout/android.view.ViewGroup' '/android.widget.LinearLayout/android.widget.LinearLayout[2]' '/android.view.ViewGroup[1]/android.widget.Button[2]').click() # # What element starts' / / class name [contains(@ element name, "content")] 'there is no element to locate here # driver.find_element_by_xpath('//android.widget.Button[contains(@ element, "xxxxx")]') click() # Element contains a certain content '/ / class name [contains(@ element name, "content")]' driver.find_element_by_xpath('//android.widget.Button[contains(@resource-id,"digit_6")]').click() # Start matching '/ / class name [ends with (@ element name, "content")]' driver.find_element_by_xpath('//android.widget.Button[ends-with(@resource-id,"eq")]').click()
- Code illustration
summary
-
Summary of this chapter
This chapter mainly talks about how appnium locates elements and two tools for locating elements
Blog Park Address
https://www.cnblogs.com/yushengaqingzhijiao/category/2024559.html