Article directory
- 1. Introduction to appium
- 2. Appium development environment deployment (python environment)
- 3.Appium automated code writing steps
- 4. Use appium for APP testing:
- 5. Element positioning and element event operations
1. Introduction to appium
1.What is appium
-
Appium is a cross-platform mobile terminal automation testing tool that can create automated test cases for IOS and Android platforms. It can simulate various operations inside the App, such as clicking, sliding, text input, etc. Appium can complete all the actions we perform manually.
-
appium supports multiple platforms and multiple languages
-
Appium uses standard automation APIs across different platforms, so there is no need to recompile or modify your own applications when crossing platforms.
-
appium supports all languages supported by Selenium WebDriver,
如java、Object-C、JavaScript、Php、Python、Ruby、C#、Clojure,或者Perl语言
, and you can also use the API ofSelenium WebDriver
. -
appium supports multiple platforms, including
MAC 和 Windows
. It was developed for these two major platformsappium-Server
. -
It also supports
Android 和 iOS
two operating systems at the same time. This makes appium very flexible.
-
2. How appium works
-
The automation script sends registration parameters (URL, platformName, platformVersion, deviceName, appPackage, appActivity to
AppiumServer
) -
AppiumServerGeneral
bootstrap.jar包
release手机端
medium -
Mobile terminal usebootstrapStart
手机端口(4724)
and return the port toAppiumServer
(equivalent to established通信连接
) -
Automation scriptSends a request (operation or command or action) to
AppiumServer
-
AppiumServerwill
请求解析
and send it tobootstrap
-
bootstrapSend request analysis to
UIAutomator
-
UIAutomator performs the corresponding operation and returns the result to
bootstrap
-
bootstrap returns the response results to
AppiumServer
-
AppiumServerReturn response results to script
To sum up, AppiumServer is like 请求者
; bootstrap plays the role of 桥梁
and is responsible for PC与电脑之间的通信
; UIAutomator
is the real executor .
3.APP type
4.APP page layout
Similar to HTML, pages are composed of individual tags
2. Appium development environment deployment (python environment)
1. Download and install the environment
1.1. Download and install the required environment
Since appium runs based on the JDK+SDK environment, if you want to use python+appium for script development normally, you need to deploy the following environment
- Android SDK:Installation and configuration of Android SDK
- JDK environment:JDK installation and configuration
- Python3:Python3 installation and configuration
- Appium-desktop (also called Appium-Server-GUI):Installation Tutorial
- Appium-Inspector (Appium developer tool):Download address
- If you use Uiautomator2 that comes with Android SDK, then this tool does not need to be installed.
作用等同于 Uiautomator2
- If you use Uiautomator2 that comes with Android SDK, then this tool does not need to be installed.
- pip install Appium-Python-Client module: command:
pip install Appium-Python-Client
- pip installs the pytest module:
pip install pytest
- Yakami Simulator:Download address
- Use a real device for testing,Ignore the Yeshen emulator installation:ADB connection emulator or Android Cell phone
Through the above settings, we have the requirements for using appium for python automated testing.
1.2. Appium-desktop ( Appium-Server-GUI ) configuration
- It can be installed by default, no special settings are required, and the appium icon on the desktop will be launched after installation.
- Appium-desktop configures the Android SDK installation directory and Java JD installation directory
startServer starts Appium-desktop: listens to the local 4723 port to initiate a request
1.3.Appium-Inspector configuration
- Remote Path:
/wd/hub
- Advanced Settings: checked
Allow Unauthorized Certificates
, unchecked Use Proxy
startSession starts Appium-Inspector
1.4. Element detection tools
- is used to locate APP elements. For example, if you need to click a button, you need to get the element of the button to operate it (click, double-click, etc.) , recommended here Use
UIAutomatorViewer
.
1.5.Appium function introduction
Simple: simple mode
- Edit Configurations: You can configure the environment addresses of android sdk and Java sdk. If the environment has been configured, the default address will be used.
Advanced: Advanced mode
- A certain person has id,
元素id值是唯一的
, therefore可用id去进行定位发送秘钥
:对文本框进行输入
2.The relationship between appnium-python-client and selenium
-
There is a certain version dependency between the Appium Python Client module and Selenium.
-
appium is like a service, appium-python-client is
让python连接appium服务的一个驱动
, which ispython语言封装和appium api通讯的一个库
. -
appium has inherited
selenium的webdriver
,因此appium-python-client安装会更新selenium。
- appium-python-client版本https://pypi.org/project/Appium-Python-Client/#history
查看selenium和appium-python-client的版本 pip show selenium pip show appium-python-client 卸载当前的selenium和appium-python-client pip uninstall selenium pip uninstall appium-python-client 安装指定版本的appium-python-clien #安装appium-python-client的同时,系统会安装与之相匹配的selenium版本 pip install appium-python-client==2.11.0
-
3.Appium automated code writing steps
- Python3
- Java8
- Android SDK v2.4.1
- Appium v2.2.1
1. Mobile phone operation (taking Redmi mobile phone as an example)
-
Enter [Developer Mode]
- [Settings] - [My Device] - [All Parameters] - [MIUI Version] Click 5 times to turn on the developer mode
- [Settings] - [My Device] - [All Parameters] - [MIUI Version] Click 5 times to turn on the developer mode
-
In the developer options, turn on [usb debugging] and [usb installation]
2. Computer operation
android-sdk-tools environment construction
-
Installation
installer_r24.4.1-windows.exe
, be sure to see [java路径
] before proceeding to the next step
-
Extract
build-tools.zip 、tools.zip 、platform-tools.zip
to the installation directory【不要文件夹套着文件夹
】
-
Open the installation directory
SDK Manager.exe
and viewplatform-tools和build-tools是否已安装
-
Check the android version of your phone, and install the corresponding android version in
SDK Manager
4. Use appium for APP testing:
1. Mobile phone operation (taking Redmi mobile phone as an example)
-
Enter [Developer Mode]
Please see step 1 and will not repeat them again -
Turn on USB debugging and installation
-
Mobile phone connected to computer via data cable
2. Computer operation (take the calculator that comes with the mobile phone as an example)
-
Open appium, click Edit Configuration, and change the Android SDK/JDK paths as shown in the picture to your own paths.
- Save changes and click to start the server
-
Open Appium Inspector to configure the parameters required to connect the app
- The items shown in the figure are:
{ "platformName": "Android", # 机操作系统,如 iOS, Android, 或者 FirefoxOS (必填) "platformVersion": "9", #手机版本 (必填) "deviceName": "FJH7N19xxx00457", #设备编码,不会进行校验,但是没有会报错 (必填) "appPackage": "com.android.calculator2", #运行的 Android 应用的包名 (必填) "appActivity": "com.huawei.calculator.Calculator", #程序入口类名 (必填) "noReset": true #在当前 session 下不会重置应用的状态。默认值为 false(非必填) }
- Get [device name] command line input [
adb devices
],
- Take [
程序包名、程序入口类名
]关闭手机所有后台程序
- . Open the calculator-application on your phone
- Enter【
adb shell "dumpsys window | grep mCurrent
】 in the command window
如果上面命令正确启动报错,说明当前的appActivity设置的不是app首次进入的appActivity,可以解析apk文件
- aapt dump badging apk package path |findstr package
- aapt dump badging F:\my_tools\DigiFinex_V2023.10.12_debugT.apk |findstr package
- aapt dump badging apk package path |findstr activity
- aapt dump badging F:\my_tools\DigiFinex_V2023.10.12_debugT.apk |findstr activity
- The items shown in the figure are:
-
After configuring the required functions, save the configuration and click to start the session
-
You will find that the mobile phone has automatically jumped to the calculator interface and is displayed on the left side of the appium window on the computer.
-
Note:
如果手机是第一次连接appium,会提示下载一个软件【
Appium Settings】,正常下载安装即可
, the installed APP is shown in the red box in the picture
There is no need to download it when you use it again. [Appium Settings
] will be displayed in the notification bar of the phone when running, as shown in the red box in the picture
-
Use the mouse to click on the mobile calculator interface on the computer appium to record the operations that need to be tested (take testing the multiplication table as an example)
-
First enter [
adb shell pm clear 包名
] in the DOS window and press enter, which means clearing the cache to prevent the cache from affecting the upcoming test. The return result is [< a i=2>】The cache is cleared successfullySuccess
-
Click the eye icon in the red frame to start recording
-
-
Start recording specific operations, taking operation 7*8 as an example
-
Copy the recorded code to pycharm
from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy from appium.webdriver.common.touch_action import TouchAction # Appium服务器地址 appium_server = 'http://127.0.0.1:4723/wd/hub' # Appium测试设备配置 desired_caps = { "platformName": "Android", "appium:platformVersion": "9", "appium:deviceName": "FJH7N19131000457", "appium:appPackage": "com.android.calculator2", "appium:appActivity": "com.huawei.calculator.Calculator", 'unicodeKeyboard': True, # 使用自带输入法,输入中文时填True 'resetKeyboard': True, # 执行完程序恢复原来输入法 'noReset': True, # 不要重置App,如果为False的话,执行完脚本后,app的数据会清空,比如你原本登录了,执行完脚本后就退出登录了 'newCommandTimeout': 6000 } # 连接Appium服务器 driver = webdriver.Remote(appium_server, desired_caps) driver.implicitly_wait(10) # TODO 干掉弹框 TouchAction(driver).tap(x=777, y=2192).perform() # 清空计算器 driver.find_element(AppiumBy.ID, "com.android.calculator2:id/op_clr").click() for i in range(1, 10): for j in range(i, 10): # TODO 按下 9 el1 = driver.find_element(AppiumBy.ID, f'com.android.calculator2:id/digit_{ i}') el1.click() # TODO 按下 乘X el2 = driver.find_element(AppiumBy.ACCESSIBILITY_ID, "乘") el2.click() # TODO 按下 8 el3 = driver.find_element(AppiumBy.ID, f'com.android.calculator2:id/digit_{ j}') el3.click() # TODO 获取 result 结果 el4 = driver.find_element(AppiumBy.ID, "com.android.calculator2:id/result") print(f"{ i} X { j} = { el4.text}", end=", ") # 清除 el6 = driver.find_element(AppiumBy.ID, "com.android.calculator2:id/op_clr") el6.click() print(f"\n-----{ i}乘法已完毕-----------") driver.quit()
- operation result:
- operation result: