https://blog.csdn.net/weixin_45912307/article/details/122392478
app自动化之启动参数获取、元素定位工具和元素定位方式
1. 启动参数获取
1.1 下载qq
官网:https://im.qq.com/download
1.2 获取启动参数
1. 获取操作系统版本
2. 获取设备名称
- cmd
- adb devices
3. 安装qq
4. 获取包名和启动页面名称 - 到build-tools的目录下选择任意一个版本,以32.0.0为例,进入
aapt.exe
目录下 - cmd回车打开终端
- 输入
aapt dump badging apk
安装路径(如C:\Users\用户名称\Downloads\Android_8.8.55.6900_537105254_32.apk
)
5. 设置appium server启动参数
1.3 整理启动参数
# 1. 从appium库里导入driver对象
from appium import webdriver
# 2. server启动参数
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = 'emulator-5554'
desired_caps['appPackage'] = 'com.tencent.mobileqq'
desired_caps['appActivity'] = 'com.tencent.mobileqq.activity.SplashActivity'
# 3. 声明driver对象
driver = webdriver.Remote('http://127.0.0.1:5554/wd/hub', desired_caps)
2元素定位工具和定位方式
2.1 第一种: android SDK自带工具:uiAutormatorViewer
2.2 第二种:python版uiAutomator2中的weditor
1. 安装工具
pip install -U uiautomator2 | 安装第三方库
python -m uiautomator2 init | 初始化uiautomator2
pip install -U weditor | 安装定位工具
weditor --help | 检查是否安装成功
weditor | 启动定位工具(网页版)
2. 工具使用
- 启动命令:weditor
2.3 第三种:appium自身定位工具appium inspector
1. 点击appium搜索按钮
2. 填写连接设备参数
注:点击“start session”前必须打开需要定位元素所在页面
{
"platformName": "Android",
"platformVersion": "5.1.1",
"deviceName": "emulator-5554",
"appPackage": "com.tencent.mobileqq",
"appActivity": "com.tencent.mobileqq.activity.LoginActivity",
"noRest": false
}
3. 工具使用及解读
- id:id唯一则有值不为空
- xpath:采用绝对定位方式
2.4 app自动化常用的元素定位方式
id | driver.find_element_by_id(id_value)
ANDROID_UIAUTOMATO | Rnew UiSelector().方法名称(值).方法名称(值).方法名称(值)
xpath | driver.find_element_by_xpath(xpath_value)
description content-desc | driver.find_element_by_accessibility_id('view-text')
className | driver.find_element_by_class_name(class_value)
坐标 | os.system('adb shell tap x y')
模糊定位 | contains(@key,value)
3. 实际案例(以qq登录为例)
# 1. 从appium库里导入driver对象
import time
from appium import webdriver
# 2. server启动参数
def denglu():
desired_caps = dict()
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = 'emulator-5554'
desired_caps['appPackage'] = 'com.tencent.mobileqq'
desired_caps['appActivity'] = 'com.tencent.mobileqq.activity.SplashActivity'
desired_caps['noRest'] = True
# 3. 声明driver对象
driver = webdriver.Remote('http://127.0.0.1:5554/wd/hub', desired_caps)
driver.implicitly_wait(10)
# 4. 点击登录
driver.find_element_by_id("com.tencent.mobileqq:id/btn_login").click()
# 5. 输入用户名
user_name = driver.find_element_by_xpath('//*[@content-desc="请输入QQ号码或手机或邮箱"]')
# 清除输入框
user_name.clear()
user_name.send_keys('492224xxxx')
# 6. 输入密码
user_pwd = driver.find_element_by_id('com.tencent.mobileqq:id/password')
# user_pwd = driver.find_element_by_xpath('//*[@resource-id="com.tencent.mobileqq:id/password"]')
# 清除输入框
user_pwd.clear()
user_pwd.send_keys('xxxxx')
# 7.勾选同意协议
driver.find_element_by_xpath('//*[@resource-id="com.tencent.mobileqq:id/pqz"]').click()
# 8.点击登录
driver.find_element_by_xpath('//*[@resource-id="com.tencent.mobileqq:id/login"]').click()
# 退出或关闭驱动
time.sleep(15)
driver.quit()