Python + Appium simple 5-step environment

This article mainly introduces how to build Python + Appium Android automatic test environment for Xiaobai. The steps are very simple~

1. python

Step 1 install Python

Forget it, everyone will skip this step~

Then install the required libraries. Because the automation program acts as a client and sends HTTP requests to Appium server, you need to install the corresponding libraries:

pip install appium-python-client

2. Install Appium server

appium server is run by nodejs and developed based on js.

Appium Desktop includes the running environment of nodejs, Appium server and some tools.

Therefore, you can install Appium Server, click me Jump to the download page!

3. JDK

The automation of Android APP requires Android SDK, which requires JDK environment

click me to download !

Install to D:\Java\jdk \, and add the environment variable JAVA_HOME, the installation directory with JDK value D:\Java\jdk

4. Android SDK

APP automation requires some tools, such as executing commands, setting up mobile phones, file transfer, application installation, etc

The general installation steps are to download android sdk after downloading Android Studio

For convenience, it can also be downloaded android sdk (passwd: kgwb), unzip it to D: \ Android SDK

Add environment variable ANDROID_HOME. The value is skd. Extract the directory, such as D: \ Android SDK

In addition, add the directory where the adb is located to the PATH, and the value is D: \ Android SDK \ platform tools

6. Connect mobile phone

Open the developer mode, enable USB debugging, and click allow USB debugging after connecting

Use adb devices to check the device on the command line, and the mobile phone can be detected. The adb command can be referred to click me

Double click to run Appium server and click start

Run the following code to realize the automatic test of Android bilibili:

from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey

desired_caps = {
  'platformName': 'Android', # The tested mobile phone is Android
  'platformVersion': '8', # Mobile Android version
  'deviceName': 'xxx', # Device name and Android phone can be filled in at will
  'appPackage': 'tv.danmaku.bili', # Start APP Package name
  'appActivity': '.ui.splash.SplashActivity', # Start Activity name
  'unicodeKeyboard': True, # Use the built-in input method, and fill in True when entering Chinese
  'resetKeyboard': True, # After executing the program, restore the original input method
  'noReset': True,       # Do not reset the App
  'newCommandTimeout': 6000,
  'automationName' : 'UiAutomator2'
  # 'app': r'd:\apk\bili.apk',
}

# Connect Appium Server and initialize the automation environment
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

# Set default wait time
driver.implicitly_wait(5)

# Youth protection
iknow = driver.find_elements_by_id("text3")
if iknow:
    iknow.click()

driver.find_element_by_id("expand_search").click()
sbox = driver.find_element_by_id('search_src_text')
sbox.send_keys('Doyoudo')
driver.press_keycode(AndroidKey.ENTER)

eles = driver.find_elements_by_id("title")
for ele in eles:
    print(ele.text)
ele.click()

q = input('input q to quit')
if q == 'q':
    driver.quit()

If the installation and connection process is normal, you can see that the mobile App automatically searches doyoudo

Inspector Session

The Inspector Session can project the mobile phone to the computer, and the agent APP on the mobile phone can upload the element node information of the tested APP to the Appium server and finally display it on the computer screen, so it can be used as easily as Selenium~

usage method:
1. Use the data cable to establish a connection with the mobile phone (use the adb command: adb devices to check whether it is connected and get the serial number of the mobile phone)
2. Run appium and click the "search" symbol in the upper right corner
3. Edit the information of the program to be located and save it
4. Select the Automatic Server column and click start session to enter the interface to locate

The specific steps are as follows. First, configure the following information:

Create a table under Automatic Server and fill in the information to generate the following JSON file:

{
  "platformName": "Android",
  "platformVersion": "8",
  "appPackage": "tv.danmaku.bili",
  "appActivity": ".ui.splash.SplashActivity",
  "unicodeKeyboard": "True",
  "resetKeyboard": "True",
  "noReset": "True",
  "newCommandTimeout": "6000",
  "automationName": "UiAutomator2"
}

Then you can save it and name it mi6. Finally, click Start session to see the APP window and DOM tree~

REFERENCES:

  1. https://www.bilibili.com/video/BV1tE411n7rV?p=3
  2. http://www.python3.vip/tut/auto/appium/01/

Keywords: Android Testing Appium

Added by Tim Silva on Mon, 07 Mar 2022 22:36:49 +0200