Appium is a mobile automation framework that can be used to test native applications, mobile web applications and hybrid applications, and is cross-platform. Can be used for ios and Android operating systems.
Appium + Python
One, the premise
1.1 JDK installation and configuration environment variables
Download and install configuration is basic (should be all)
Test whether the configuration is good, enter under cmd (the same below)
java -version
1.2 Android-SDK installation
This is directly in Android Studio
Test whether it is configured
adb --version
1.3 Node.js installation
Node.js official website download: https://nodejs.org/en/
test, enter the installation path of node.js, enter
node --version
1.4 Appium client installation
In addition to the above jdk and Android-sdk environments, we use Appium and python for automated testing. Two things need to be installed, one is the Appium client and the other is the Appium-python library. These two things that need to be installed can be automatically tested by adding a mobile phone. The relationship between them is: python code>Appium-python library>Appium->mobile phone.
appium-desktop download address: https://github.com/appium/appium-desktop/releases
(This software is a bit big, I uploaded one to Baidu network disk.
Link: https://pan.baidu.com/s/ 1WlaYoifeRGIF1Yc02deScQ
extraction code: wp4l)
After downloading, right-click to open it as an administrator. After opening, choose to install for anyone who uses this computer (all users) . The default path after installation is C:\Program Files\Appium
Remember to configure environment variables as follows
Default Host and Port, click Start Server v1.18.0 before you write the code in python and run it
1.5 Python installation and configuration environment variables
Test whether it is configured
python
1.6 pycharm installation
Official website: https://www.jetbrains.com/pycharm/download/#section=windows just
download a community version hahaha
1.7 Install the python library Appium-Python-Client
Open cmd, enter
pip install Appium-Python-Client
Two, actual combat
- Turn on the emulator (mine is the Android Studio emulator that I use directly, so I have to open Android Studio before turning on the emulator)
- Open appium server
- Open pycharm
- Open cmd
- Open uiautomatorviewer (under the SDK directory)
2.1 Click on an event
Open pycharm, create a new py file, enter the code
from appium import webdriver
import time
desired_caps=dict()
desired_caps['platformName']='Android'#平台的名字,不区分大小写,“Android”;“ios”
desired_caps['platformVersion']='5.0'#平台的版本,可以不写后续版本号
desired_caps['deviceName']='emulator-5554'#设备的名字,不能为空
desired_caps['appPackage']='com.google.android.apps.messaging' #要打开的应用程序包名
desired_caps['appActivity']='.ui.ConversationListActivity'#要打开的应用程序的界面名
desired_caps['udid']='emulator-5554'#连接设备的唯一标识
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)#连接 appium 服务器
driver.start_activity("com.google.android.apps.messaging", ".ui.ConversationListActivity") #包名(前面是包名,后面是界面名)
driver.find_element_by_id("com.google.android.apps.messaging:id/start_new_conversation_button").click() #点击事件
time.sleep(3)
driver.quit()
Note : (The premise is that the emulator in your Android Studio is turned on and the appium server is turned on) The
sixth line of device name query: enter after cmd
adb devices
Line 7 and Line 8: The package name of the application to be opened, the interface name of the application to be opened
Find the program you want to test and open it in the simulator beforehand
Enter under cmd
adb shell dumpsys window | findstr mCurrentFocus
After you press Enter, you can see the package name and interface name of the program you want to test (the interface name can omit the package name, but the dot cannot be omitted)
How to find the specific id of the click event? ? ? (Line 13)
cmd open uiautomatorviewer
If it is not added to the environment variable, go to the sdk directory and find uiautomatorviewer.bat
. Find the file in the first line of Android Studio -> Settings and
click the emulator. Find the resource-id in uiautomatorviewer. This id is unique to each click event.
Click 1, click the event 2 you want to operate, you can see the id
After running the python code, the simulator will automatically start the program!
2.2 Click an event of a certain group of elements
This is actually through the index
See code
from appium import webdriver
import time
desired_caps=dict()
desired_caps['platformName']='Android'#平台的名字,不区分大小写,“Android”;“ios”
desired_caps['platformVersion']='5.0'#平台的版本,可以不写后续版本号
desired_caps['deviceName']='emulator-5554'#设备的名字,不能为空
# desired_caps['appPackage']='com.google.android.apps.messaging' #要打开的应用程序包名
# desired_caps['appActivity']='.ui.ConversationListActivity'#要打开的应用程序的界面名
desired_caps['udid']='emulator-5554'#连接设备的唯一标识
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)#连接 appium 服务器
# driver.start_activity("com.google.android.apps.messaging", ".ui.ConversationListActivity") #包名(前面是包名,后面是界面名)
# driver.find_element_by_id("com.google.android.apps.messaging:id/start_new_conversation_button").click() #点击事件
# time.sleep(3)
# driver.quit()
#如何定位一组元素,比如说我要打开设置的第三个选项
driver.start_activity("com.android.settings",".Settings") #包名(前面是包名,后面是界面名)
A=driver.find_elements_by_class_name("android.widget.LinearLayout") #类名(记住类名要一样,自己试试)
A[6].click()
time.sleep(5)
driver.quit()
The class_name inside is also viewed through uiautomatorviewer. For example, if I want to turn on the Connected devices option, its index is 6 (line 21 of code), and it will be automatically opened
2.3 A set of positioning methods supplement
Locate a group of elements by class_name (class name)
find_elements_by_class_name("class_name")
Locate a group of elements by id
driver.find_elements_by_id("id_name")
Locate a group of elements via xpath
driver.find_elements_by_xpath("xpath_name")
2.4 A positioning method supplement
其实这个也就是一组的方式中的elements少个s
Locate an element by class_name (class name)
find_element_by_class_name("class_name")
Locate an element by id
driver.find_element_by_id("id_name")
Locate an element via xpath
driver.find_element_by_xpath("xpath_name")
This is my end mobile automated testing, I remember also made a PC end automated test here
https://blog.csdn.net/hanhanwanghaha Welcome to follow this super invincible and cute duck. If you have any questions, you can leave a private message and you will reply if you see it!
Creation is not easy, if reprinted, please indicate the source