Appium is a mobile automation framework for testing native applications, mobile web applications and hybrid applications, and is cross-platform. Available for ios and Android operating systems.

Appium+Python

  • A, the premise
    • 1.1 JDK installation and configuration environment variables
    • 1.2 Android SDK installation
    • 1.3 the Node. Js installed
    • 1.4 Appium Client Installation
    • 1.5 Installing Python and configuring Environment Variables
    • 1.6 pycharm installation
    • 1.7 Installing the Python library appium-python-client
  • Second, the actual combat
    • 2.1 Click an event
    • 2.2 Click an event for a group of elements
    • 2.3 A set of positioning methods supplement
    • 2.4 A positioning method supplement

A, the premise

1.1 JDK installation and configuration environment variables

Download, install and configure the basic (should be all)

To test whether the configuration is complete, enter CMD (same below)

java -version
Copy the code

1.2 Android SDK installation

This is available directly in Android Studio

Test whether the configuration is correct

adb --version
Copy the code

1.3 the Node. Js installed

Download from the Node. js official website: nodejs.org/en/ Test, go to the node.js installation path, and enter

node --version
Copy the code

1.4 Appium Client Installation

In addition to the ABOVE JDK and Android-SDK environment, we use Appium and Python for automated testing. We also need to install two things, one is the Appium client and the other is the Appium-Python library. These two things need to be installed in addition to the phone for automated testing, the relationship between them is: Python code >Appium- Python library >Appium-> phone.

Appium-desktop download address: github.com/appium/appi… (this software is a little big, I uploaded a network location to baidu inside links: pan.baidu.com/s/1WlaYoife… Extraction code: WP4L)

After downloading it, right-click the administrator and open it. After opening it, select Install as anyone using this computer (all users). The default path after installation is C: Program Files\Appium

Remember to configure the environment variables as follows

By default Host and Port, click Start Server v1.18.0 before writing code in Python to run

1.5 Installing Python and configuring Environment Variables

Test whether the configuration is correct

python
Copy the code

1.6 pycharm installation

Website: www.jetbrains.com/pycharm/dow… Just download a community version hahaha

1.7 Installing the Python library appium-python-client

Open CMD and type

pip install Appium-Python-Client
Copy the code

Second, the actual combat

  1. Start the emulator (mine is a direct Android Studio emulator, so I need to start Android Studio before starting the emulator)
  2. Open the Appium server
  3. Open the pycharm
  4. Open the CMD
  5. Open uiAutomatorViewer (in SDK directory)

2.1 Click an event

Open PyCharm, create a new py file, and enter the code

from appium import webdriver
import time
desired_caps=dict()
desired_caps['platformName'] ='Android'# Platform name, case insensitive, "Android"; "Ios"
desired_caps['platformVersion'] ='5.0'# platform version, can not write later version number
desired_caps['deviceName'] ='emulator-5554'The device name cannot be empty
desired_caps['appPackage'] ='com.google.android.apps.messaging' The name of the application package to open
desired_caps['appActivity'] ='.ui.ConversationListActivity'The interface name of the application to open
desired_caps['udid'] ='emulator-5554'# Unique identifier of the connected device
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)# Connect to appium server

driver.start_activity("com.google.android.apps.messaging".".ui.ConversationListActivity") # package name = package name
driver.find_element_by_id("com.google.android.apps.messaging:id/start_new_conversation_button").click() # Click event
time.sleep(3)
driver.quit()
Copy the code

Note: (Before running, your Emulator in Android Studio is enabled and the Appium server is also enabled.

adb devices
Copy the code



Lines 7, 8: The name of the application package to open, the interface name of the application to open

Find the program you want to test and open it in the simulator beforehand

CMD to input

adb shell dumpsys window | findstr mCurrentFocus
Copy the code

Press Enter to see the package name and interface name of the application you want to test (the interface name can omit the package name, but. The dot cannot be omitted.



How do I find the specific ID of the click event? (Line 13)

CMD open uiautomatorviewer

If you don’t add it to the environment variable, go to the SDK directory and find uiAutomatorViewer.bat

Find File — >Settings on the first line in Android Studio



Click on the emulator and find the Resource-ID in the UIAutomatorViewer that is unique to each click event.



Click on event 1, click on event 2 that you want to act on, and you’ll see the ID

The emulator will start the program automatically after running python code.

2.2 Click an event for a group of elements

This is actually indexed

See the code

from appium import webdriver
import time
desired_caps=dict()
desired_caps['platformName'] ='Android'# Platform name, case insensitive, "Android"; "Ios"
desired_caps['platformVersion'] ='5.0'# platform version, can not write later version number
desired_caps['deviceName'] ='emulator-5554'The device name cannot be empty
# desired_caps [' appPackage] = 'com. Google. Android. Apps. Messaging' # to open the application package name
# desired_caps [' appActivity] = '. UI. ConversationListActivity '# to open application interface name
desired_caps['udid'] ='emulator-5554'# Unique identifier of the connected device
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)# Connect to appium server


# driver. Start_activity (" com. Google. Android. Apps. Messaging ", "the UI. ConversationListActivity") # package name (front is a package name, behind is the interface name)
# driver. Find_element_by_id (". Com. Google. Android apps. Messaging: id/start_new_conversation_button "). Click click event (#)
# time.sleep(3)
# driver.quit()

How do I position a set of elements, say I want to turn on the third option of Settings
driver.start_activity("com.android.settings".".Settings") # package name = package name
A=driver.find_elements_by_class_name("android.widget.LinearLayout")  # class name (remember to use the same class name, try it yourself)
A[6].click()
time.sleep(5)
driver.quit()
Copy the code

The class_name is also viewed via uiAutomatorViewer, for example, if I open Connected Devices, its index is 6 (line 21), it will open automatically

Blog.csdn.net/hanhanwangh… A super invincible lovely human duck welcomes your attention! Welcome to wechat public account: Treasure girl’s growing diary if reproduced, please indicate the source (if not indicated, the thief will investigate)

2.3 A set of positioning methods supplement

Locate a set of elements by class_name (class name)

find_elements_by_class_name("class_name")
Copy the code

Locate a set of elements by ID

driver.find_elements_by_id("id_name")
Copy the code

Locate a set of elements by xpath

driver.find_elements_by_xpath("xpath_name")
Copy the code

2.4 A positioning method supplement

So this is just a group of elements minus sCopy the code

Locate an element by class_name (class name)

find_element_by_class_name("class_name")
Copy the code

Locate an element by ID

driver.find_element_by_id("id_name")
Copy the code

Locate an element by xpath

driver.find_element_by_xpath("xpath_name")
Copy the code



This is an automated test for my mobile terminal. I remember sending an automated test for my PC terminalClick here to

Blog.csdn.net/hanhanwangh… Welcome to pay attention to this super invincible lovely person duck, have any question message private message all can, see will return! Creation is not easy, if there is a reprint, please indicate the source