Appium installation and simple introduction

This article first appeared in: Walker AI

Appium is an automated open source 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 on multiple platforms (iOS, Android, Windows) with the same API. Reuse code between iOS, Android and Windows test suites.

1. Download installation and environment configuration

1.1 Install python install Appium-Python-Client library

You can use the pip command to install pip install Appium-Python-Client. After the installation is complete, run this code in python to from appium import webdriververify whether the installation is successful.

1.2 Install Android SDK

(1) Download Android Studio on the official website , which contains Android SDK, drop down to Command line tools only at the bottom of the webpage, download the windows version in it, unzip it after downloading, cd to the bin directory in the CMD command line, enter to sdkmanager "platfrom-tools" "platforms;android-28" "build-tools;28.0.3"execute the installation Component, Accept? (y/N) pops up during the process, enter y and press Enter.

Appium installation and simple introduction

(2) After the components are installed, perform the following steps to add the Android SDK to the environment variables.

  • Create the ANDROID_HOME variable in the system environment variable, the value is the root directory path of the SDK installation, for example: E:\android_sdk
  • Add %ANDROID_HOME%\platform-tools to the environment variable Path
  • Verify the setting is successful or not: Enter adb versionenter under the CMD command line , you can see the current adb version information, indicating that the Android SDK has been installed and configured successfully

1.3 Install JDK

(1) Download and install JDK on the oracle official website , and install JDK8 and above.

(2) After the installation is complete, set the environment variables of the JDK.

  • Create the JAVA_HOME variable in the system environment variable, the value is the directory path of the JDK installation, for example: C:\Program Files\Java\jdk1.8.0_231
  • Create a 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 the Path variable
  • Verify that the JDK environment configuration is successful, and press java -versionEnter under the CMD command line , you can see the current JDK version information, indicating that the JDK has been installed and configured successfully

1.4 Install appium server

(1) Download Appium Desktop on the official website of appium and install it, and find the version corresponding to the system you are using. All operations this time are performed on windows.

Appium installation and simple introduction

(2) After the installation is complete, start appium, host and port can be defaulted, and add Android SDK and Java JDK to the environment variables in Edit Configuration.

Appium installation and simple introduction

(3) Save and restart Appium after setting up, 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.

Appium installation and simple introduction

2. Real machine test

2.1 Real device settings

Connect the mobile phone with USB, turn on the mobile phone developer mode, turn on the USB debugging and USB installation in the developer options, the Xiaomi mobile phone also needs to turn on the USB debugging (security settings), and then enter the adb devicesEnter key in the CMD command line , if the device number of the mobile phone appears , Indicating that the connection is successful.

Appium installation and simple introduction

2.2 Setting up Appium

(1) Click Start Inspector Session to configure Desired Capabilities, enter the following json data in JSON Representation and click Save for quick configuration, or add item by item on the left. The five parameters platformName, platformVersion, deviceName, appPackage, and appActivity are required .

Appium installation and simple introduction

Appium installation and simple introduction

{
"platformName": "Android", # 声明是ios还是Android系统
"platformVersion": "8.1.0", # Android内核版本号
"deviceName": "MI_5X", # 连接的设备名称
"appPackage": "com.tencent.qqmusic", # apk的包名
"appActivity": ".activity.AppStarterActivity", # apk的launcherActivity
"resetKeyboard": True,
"noReset": True # 在开始会话之前不要重置应用程序状态
}

The above data is obtained through the adb command. When obtaining appPackage and appActivity, the app needs to be started . The obtaining command is as follows:

Appium installation and simple introduction

(2) After configuring the Desired Capabilities, click Start Session. After the app starts running, you can click to select on the interface to view the detailed information of all element controls on the current page. There are three buttons under Selected Element on the right.

  • Tap : Perform a click operation on the selected element
  • Send Keys : Pass values ​​for input objects such as text boxes
  • Clear : Clear the text in the input box

Appium installation and simple introduction

2.3 Writing automation scripts

After starting the session and running the app successfully, click on the element you want to operate, and you can see the relevant information of this 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 ways to locate elements.

Such as: find_element_by_xpath(), driver.find_element_by_name(), driver.find_element_by_partial_link_text(), etc., use the click() method to click the element, and use the swip() method to slide the screen.

Appium installation and simple introduction

The following is the source code display of python implementation. After running, you can see that the phone automatically completes the operations of launching the app, clicking, sliding and exiting, indicating that the python script has run successfully. It should be noted that the response speed of the app is different due to the difference in mobile phone performance after the previous operation is completed. It is recommended to wait a few seconds before proceeding to the next operation.

import time
from appium import webdriver

caps = {
"platformName": "Android", # 声明是ios还是Android系统
"platformVersion": "8.1.0", # Android内核版本号
"deviceName": "MI_5X", # 连接的设备名称
"appPackage": "com.tencent.qqmusic", # apk的包名
"appActivity": ".activity.AppStarterActivity", # apk的launcherActivity
"resetKeyboard": True,
"noReset": True # 在开始会话之前不要重置应用程序状态
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) # 启动app
time.sleep(15)
el1 = driver.find_element_by_id("com.tencent.qqmusic:id/clt") # 定位<分类歌单>元素
el1.click() # 点击
time.sleep(5)
driver.swipe(500, 1550, 500, 800) # 从(500, 1500)滑动到(500, 800)
driver.quit() # 退出

3. Summary

Appium is currently a relatively mature automated testing tool. It uses the automated framework that comes with the system and does not need to compile Appium-specific or third-party code into your application, so that you can use the officially released package for testing without worrying about testing. There is a difference between a package and a formal package. At the same time, it can be combined with python's existing test frameworks, such as pytest, unittest, to write test cases and perform automated tests. Appium still has many interesting features that have not been introduced, and friends who have questions or suggestions are welcome to discuss with us.


PS: For more technical dry goods, please pay attention to [public account| xingzhe_ai], and discuss with Xingzhe!

Guess you like

Origin blog.51cto.com/15063587/2585347