程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Appium+python mobile terminal (Android) automated test environment was not so difficult to build+ Take you to practice

編輯:Python

Appium It's a mobile automation framework , Can be used to test native applications , Mobile web applications and hybrid applications use , And it's cross platform . Can be used for ios and Android operating system .

Appium+Python

  • One 、 Premise
    • 1.1 JDK Installation and its configuration environment variables
    • 1.2 Android-SDK install
    • 1.3 Node.js install
    • 1.4 Appium Client installation
    • 1.5 python Installation and its configuration environment variables
    • 1.6 pycharm install
    • 1.7 install python library Appium-Python-Client
  • Two 、 actual combat
    • 2.1 Click on an event
    • 2.2 Click on an event of a group of elements
    • 2.3 A set of positioning mode supplement
    • 2.4 A positioning mode complements

One 、 Premise

1.1 JDK Installation and its configuration environment variables

Download and install the configuration, which is the basic ( It should all be )

Test if it's configured , stay cmd The input ( The same below )

java -version

1.2 Android-SDK install

This is directly in Android Studio There is

Test if it's configured

adb --version

1.3 Node.js install

node.js Download from the official website :https://nodejs.org/en/
test , Get into node.js Installation path for , Input

node --version

1.4 Appium Client installation

In addition to the above jdk and Android-sdk Environmental Science , We use Appium and python For automated testing , also Two things need to be installed , One is Appium The client of , One is Appium-python library . These two things that need to be installed can be tested automatically with mobile phones , 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 little big , I uploaded one to Baidu online disk
link :https://pan.baidu.com/s/1WlaYoifeRGIF1Yc02deScQ
Extraction code :wp4l )

After downloading, right-click the administrator to open , Open and select 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, stay python Click... Before writing the code to run Start Server v1.18.0

1.5 python Installation and its configuration environment variables

Test if it's configured

python

1.6 pycharm install

Official website :https://www.jetbrains.com/pycharm/download/#section=windows
Just download a community version

1.7 install python library Appium-Python-Client

open cmd, Input

pip install Appium-Python-Client

Two 、 actual combat

  1. Turn on the simulator ( Mine is for direct use Android Studio Simulator , So before I start the simulator, I have to start Android Studio)
  2. open appium The server
  3. open pycharm
  4. open cmd
  5. open uiautomatorviewer( stay SDK Under the table of contents )

2.1 Click on an event

open pycharm, Create a new one py file , Enter the code

from appium import webdriver
import time
desired_caps=dict()
desired_caps['platformName']='Android'# The name of the platform , Case insensitive ,“Android”;“ios”
desired_caps['platformVersion']='5.0'# Version of the platform , No subsequent version number is required 
desired_caps['deviceName']='emulator-5554'# Name of equipment , Can't be empty 
desired_caps['appPackage']='com.google.android.apps.messaging' # Name of the application package to open 
desired_caps['appActivity']='.ui.ConversationListActivity'# The interface name of the application to be opened 
desired_caps['udid']='emulator-5554'# Unique identification of the connected device 
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)# Connect appium The server 
driver.start_activity("com.google.android.apps.messaging", ".ui.ConversationListActivity") # Package name ( The bag name is in front of , Followed by 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()

Be careful :( The premise of operation is your Android Studio The simulator in is turned on 、appium The server is also turned on )
The sixth line is the equipment name query :cmd After input

adb devices


The first 7 That's ok 、 The first 8 That's ok : Name of the application package to open 、 The interface name of the application to be opened

Find the program you want to test , Open this program in the simulator beforehand

cmd The input

adb shell dumpsys window | findstr mCurrentFocus

Press enter to see the package name and interface name of the program you want to test ( The package name can be omitted from the interface name , however . Points cannot be omitted )

How to find a specific click event id???( The first 13 That's ok )

cmd open uiautomatorviewer

If it is not added to the environment variable, go to sdk In the directory uiautomatorviewer.bat
stay Android Studio Found in the first line of file——>Settings

Click Simulator , stay uiautomatorviewer find resource-id, This id Is unique to each click event .

Click on 1, Click on the event you want to operate 2, You can see that id 了

function python The simulator will automatically start the program after the code !

2.2 Click on an event of a group of elements

This is actually from the index

See the code

from appium import webdriver
import time
desired_caps=dict()
desired_caps['platformName']='Android'# The name of the platform , Case insensitive ,“Android”;“ios”
desired_caps['platformVersion']='5.0'# Version of the platform , No subsequent version number is required 
desired_caps['deviceName']='emulator-5554'# Name of equipment , Can't be empty 
# desired_caps['appPackage']='com.google.android.apps.messaging' # Name of the application package to open 
# desired_caps['appActivity']='.ui.ConversationListActivity'# The interface name of the application to be opened 
desired_caps['udid']='emulator-5554'# Unique identification of the connected device 
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)# Connect appium The server 
# driver.start_activity("com.google.android.apps.messaging", ".ui.ConversationListActivity") # Package name ( The bag name is in front of , Followed by 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 to locate a set of elements , For example, I want to open the third option of the setting 
driver.start_activity("com.android.settings",".Settings") # Package name ( The bag name is in front of , Followed by the interface name )
A=driver.find_elements_by_class_name("android.widget.LinearLayout") # Class name ( Remember that the class names should be the same , Try it yourself )
A[6].click()
time.sleep(5)
driver.quit()

Inside class_name through uiautomatorviewer To see , For example, I want to open Connected devices This option , Its index is 6( The first 21 Line code ), It will open automatically

https://blog.csdn.net/hanhanwanghaha A super invincible and cute duck Your attention is welcome !
Welcome to WeChat official account. : Treasure girl's Growth Diary
If reproduced , Please indicate the source ( If not indicated , Whoever steals will be prosecuted )

2.3 A set of positioning mode supplement

adopt class_name( Class name ) Locate a set of elements

find_elements_by_class_name("class_name")

adopt id Locate a set of elements

driver.find_elements_by_id("id_name")

adopt xpath Locate a set of elements

driver.find_elements_by_xpath("xpath_name")

2.4 A positioning mode complements

 In fact, this is one of the group methods elements Less s

adopt class_name( Class name ) Locate an element

find_element_by_class_name("class_name")

adopt id Locate an element

driver.find_element_by_id("id_name")

adopt xpath Locate an element

driver.find_element_by_xpath("xpath_name")


This is the automated test of my mobile terminal , I remember sending one before PC End of the automation test Click here to

https://blog.csdn.net/hanhanwanghaha Welcome to pay attention to this super invincible and lovely duck , If you have any questions, please leave a message , When you see it, you will return !
It's not easy to create , If reproduced , Please indicate the source


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved