This article was originally published in: Walker AI
Appium is an open source automation tool that supports the automation of native, mobile Web and hybrid applications on iOS, Android and Windows desktop platforms. Appium is cross-platform: it allows you to write tests for multiple platforms (iOS, Android, Windows) using the same API. Reuse code between iOS, Android, and Windows test suites.
1. Download the installation and environment configuration
1.1 Installing Python Install the Appium-python-client library
You can run the PIP install appium-python-client command to install appium-python-client. After the installation, run the code from Appium import webdriver in Python to check whether the installation is successful.
1.2 Installing the Android SDK
(1) Download Android Studio from the official website, which includes the Android SDK. Drop down the Command Line Tools only at the bottom of the web page, download the Windows version, decompress the download, and then CD into the bin directory in the CMD Command line. Enter sdkmanager “platfrom-tools” “platforms; android-28” “build-tools; 28.0.3” During component installation, Accept? Is displayed. (y/N), type y and press Enter.
(2) After the component is installed, perform the following steps to add the Android SDK to the environment variables.
- Create the ANDROID_HOME variable in the system environment variable with the value of the SDK installation root directory path, for example: E:\android_sdk
- Add %ANDROID_HOME%\platform-tools to the environment variable Path
- Verify that the Settings are successful. If no, enter CMD
adb version
Press Enter to view the current ADB version information, indicating that the Android SDK has been installed and configured successfully
1.3 to install the JDK
(1) Download and install the JDK from the Oracle official website, and install JDK8 or later versions.
(2) After the installation, set JDK environment variables.
- Create a JAVA_HOME variable in the system environment variable with the value of the JDK installation directory path, for example, C:\Program Files\Java\jdk1.8.0_231
- Create the CLASSPATH variable in the system environment variable with a value of. %JAVA_HOME%\lib; %JAVA_HOME%\lib\tools.jar
- Add %JAVA_HOME%\bin; %JAVA_HOME%\jre\bin
- To verify whether the JDK environment configuration is successful, enter CMD
java -version
Press Enter. The current JDK version information is displayed, indicating that the JDK has been installed and configured successfully
1.4 Installing the Appium Server
(1) Download appium Desktop from the official website of Appium to install, and find the version corresponding to the system you use. All operations this time are carried out on Windows.
(2) After the installation is complete, start appium with the default host and port. In Edit Configuration, add the Android SDK and Java JDK to the environment variables.
(3) After setting, save and restart Appium, and then start the service. The following screen appears, indicating that your Appium Server has been started normally, and you can start the real machine test.
2. Real machine test
2.1 Real machine Settings
Connect the mobile phone with USB, open the mobile developer mode, and open USB debugging and USB installation in the developer options. Xiaomi mobile phone also needs to open USB debugging (security Settings), and then enter ADB Devices in the CMD command line. If the device number of the mobile phone appears, the connection is successful.
2.2 set Appium
(1) Click Start Inspector Session to configure Desired Capabilities. Enter the following JSON data in JSON Representation and click save for quick configuration. You can also add items one by one on the left. PlatformName, platformVersion, deviceName, appPackage, and appActivity are mandatory.
{
"platformName": "Android".Ios or Android
"platformVersion": "8.1.0".# Android kernel version number
"deviceName": "MI_5X".The name of the connected device
"appPackage": "com.tencent.qqmusic".# apk package name
"appActivity": ".activity.AppStarterActivity".# apk的launcherActivity
"resetKeyboard": True."noReset": True Do not reset the application state before starting the session
}
Copy the code
The above data can be obtained by adb command. To obtain appPackage and appActivity, start the app. The command is as follows:
(2) After Desired Capabilities is configured, click Start Session. After the app is started and run, you can click and select to view the detailed information of all elements and controls on the current page. There are three buttons below the Selected Element on the right.
- Tap: Performs a Tap on the selected element
- Send Keys: Sends values to input objects such as text boxes
- Clear: Clears the text in the input box
2.3 Writing automatic scripts
Once the session has started and the app has run successfully, click on the element you want to operate on. You can see the information for that element on the right. Here we can operate on the element by id, use the find_element_by_id() method in Python to locate the element, and there are many other ways to locate the element.
Such as: Find_element_by_xpath (), driver.find_element_by_name(), driver.find_element_by_partial_link_text(), etc. Swipe the screen using the SWIP () method.
The following is the source code display of python implementation operations. After running, you can see that the mobile phone automatically completes the operation of starting app, clicking, sliding and exiting, indicating that the Python script is running successfully. It should be noted that the response speed of APP varies with the performance of mobile phones after the last operation. It is recommended to wait a few seconds before proceeding to the next operation.
import time
from appium import webdriver
caps = {
"platformName": "Android".Ios or Android
"platformVersion": "8.1.0".# Android kernel version number
"deviceName": "MI_5X".The name of the connected device
"appPackage": "com.tencent.qqmusic".# apk package name
"appActivity": ".activity.AppStarterActivity".# apk的launcherActivity
"resetKeyboard": True."noReset": True Do not reset the application state before starting the session
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) # start the app
time.sleep(15)
el1 = driver.find_element_by_id("com.tencent.qqmusic:id/clt") # Locate the < category playlist > element
el1.click() # click
time.sleep(5)
driver.swipe(500.1550.500.800) # slide from (500, 1500) to (500, 800)
driver.quit() # exit
Copy the code
3. Summary
Appium is a relatively mature automated testing tool at present, using the automation framework of the system, there is no need to compile Appium specific or third-party code into your application, so you can use the officially released package for testing, there is no need to worry about the difference between the test package and the formal package. At the same time, you can use python’s existing testing frameworks, such as PyTest and UnitTest, to write test cases and perform automated tests. There are a lot of interesting features of Appium that have not been introduced, welcome to discuss with us if you have any questions or suggestions.
PS: more dry technology, pay attention to the public, | xingzhe_ai 】, and walker to discuss together!