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

Appium+python mobile terminal practice -- teach you how to locate XPath automated test

編輯:Python

Today's question
I : Simple tests can be operated manually , Why automated testing
teacher : Mainly regression testing . Changed bug after , Test again . In this way, regression testing is more cost-effective than manual testing

Appium+Python The actual combat of mobile terminal

  • One 、 Premise
  • Two 、 On the actual combat

One 、 Premise

  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

https://blog.csdn.net/hanhanwanghaha Treasure girl 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 )

Two 、 On the actual combat

Turn on the simulator
stay cmd Enter the code :

adb shell dumpsys window | findstr mCurrentFocus

The package name and interface name can be queried ( The package name can be omitted from the interface name , But I'm afraid I missed that point , Generally do not omit )
For example, my package name :

com.google.android.apps.messaging

Interface name

.conversation.screen.ConversationActivity

or

com.google.android.apps.messaging.conversation.screen.ConversationActivity

open appium, Enter server , Click on the search box , Enter interface

Click on

Enter the editing interface , Enter the code in the box to connect to the simulator
The code is as follows

 The following code comments :
Article 1 with a : The name of the platform , Case insensitive ,“Android”;“ios”
Second : Version of the platform , No subsequent version number is required
Article 3 the : Name of equipment , Can't be empty (cmd after adb devices You can see )
Article 4. : Name of the application package to open ( above cmd Your orders can be found out )
Article 5. : The interface name of the application to be opened ( above cmd Your orders can be found out )
Article 6. : Name of equipment
{

"platformName": "Android",
"platformVersion": "5.0",
"deviceName": "emulator-5554",
"appPackage": "com.google.android.apps.messaging",
"appActivity": "com.google.android.apps.messaging.ui.ConversationListActivity",
"udid": "emulator-5554"
}


Remember to save it after filling it out

Click on Start Session, You can go in appium Automation interface , The simulator will also run automatically

Next, let's look at the key we want to press xpath route , Click the key I want to run automatically , stay Select Element Can be seen in xpath route .

Keep the bracketed portion of the path , For example, mine is

//android.widget.Button[@content-desc="Start chat"]

What is needed is

[@content-desc="Start chat"]

But in the pycharm There will be errors in the , So you need to change the quotation marks ( This self adjustment is )
So put it on pycahrm Medium python The code should be

driver.find_element_by_xpath("//*[@content-desc='Start chat']").click() # Click event 

Upper actual combat 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", "com.google.android.apps.messaging.ui.ConversationListActivity") # Package name ( The bag name is in front of , Followed by the interface name )
time.sleep(3)
driver.find_element_by_xpath("//*[@content-desc='Conversation list']").click() # Click event 
time.sleep(3)
driver.find_element_by_xpath("//*[@content-desc='Start chat']").click() # Click event 
time.sleep(5)
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")
# A=driver.find_elements_by_class_name("android.widget.LinearLayout")
# A[6].click()
# time.sleep(5)
# driver.quit()

And then , my app It can run automatically
as follows

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