Appium+Python mobile terminal combat-teach you how to automatically test xpath positioning

Today’s question
I: You can operate simple tests manually, why do you want to automate tests
Teacher: Mainly regression tests. After fixing the bug, test again. In this way, using regression testing will save costs compared to manual testing

Appium+Python mobile terminal combat

One, the premise

  1. 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)
  2. Open appium server
  3. Open pycharm
  4. Open cmd

Second, actual combat

Open the simulator
, enter the code in cmd:

adb shell dumpsys window | findstr mCurrentFocus

The package name and interface name can be queried (the package name can be omitted for the interface name, but I am afraid that I will miss that point, so I usually don’t omit it)
.

com.google.android.apps.messaging

Interface name

.conversation.screen.ConversationActivity

or

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

Insert picture description here

Open appium, enter the server, click the search box, enter the interface
Insert picture description here
click
Insert picture description here

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

下面的代码注释:
第一条:平台的名字,不区分大小写,“Android”;“ios”
第二条:平台的版本,可以不写后续版本号
第三条:设备的名字,不能为空 (cmd后 adb devices可以看)
第四条:要打开的应用程序包名(上面cmd的命令可查出来)
第五条:要打开的应用程序的界面名(上面cmd的命令可查出来)
第六条:设备的名字
{
    
    
  "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"
}

Insert picture description here
Remember to save after filling in. After
Insert picture description here
clicking Start Session, you can enter the automation interface of appium, and the simulator will run automatically.
Insert picture description here
Next, let's check the xpath path of the key we want to press, and click the key I want to run automatically. The xpath path can be seen in Select Element.
Insert picture description here
Keep the brackets of the path, for example mine is

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

What is needed is

[@content-desc="Start chat"]

But there will be errors in pycharm, so you need to change the quotation marks (this is self-adjustment),
so the python code in pycahrm should be

driver.find_element_by_xpath("//*[@content-desc='Start chat']").click()  #点击事件

On the actual 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", "com.google.android.apps.messaging.ui.ConversationListActivity") #包名(前面是包名,后面是界面名)
time.sleep(3)
driver.find_element_by_xpath("//*[@content-desc='Conversation list']").click()  #点击事件
time.sleep(3)
driver.find_element_by_xpath("//*[@content-desc='Start chat']").click()  #点击事件
time.sleep(5)
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()

Afterwards, my app can run automatically
as follows
Insert picture description 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
Insert picture description here

Guess you like

Origin blog.csdn.net/hanhanwanghaha/article/details/111316397