Python Appium 安卓自动化测试 基本使用

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


文章目录


前言

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、环境安装

1.1 Python Pip 安装模块

1.2 Sdk、Jdk、Appium-desktop 网盘下载

链接:https://pan.baidu.com/s/19C9fGmoXne8DgfXhrTB2TQ

提取码:kgwb

1.3 参考文档

https://www.byhy.net/tut/auto/appium/01/

二、Android Sdk uiautomateviewer 分析&定位App界面元素

2.1 启动步骤

在SDK的目录下找到tools/bin/uiautomatorviewer.bat,双击即可运行;
Python Appium 安卓自动化测试 基本使用 - Phone Spider-LMLPHP
Python Appium 安卓自动化测试 基本使用 - Phone Spider-LMLPHP

2.2 常见的问题

Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't exist!

2.2.1 解决方法1:

一般都是和Appium Desktop Appium 冲突了,关闭Appium Desktop Appium 重新获取app界面即可;

2.2.2 解决方法2:

命令面板运行:adb reconnect
运行后再次点击连接即可

2.3 resource-id 属性分析 常用于元素定位

resource-id=tv.danmaku.bili:id/search_src_text, 可通过search_src_text进行元素ID定位,也可以写全(tv.danmaku.bili:id/search_src_text)定位;

from selenium.webdriver.common.by import By
driver.find_element_by_id('search_src_text')
driver.find_element(By.ID, 'search_src_text')

2.4 bounds 属性分析 可用于元素定位

bounds = [175,88][887,171],表示某个元素左上角的宽高和右下角的宽高边界像素点位置;

2.5 text 属性分析 可用于文本判断

text=appium,组件里边的内容;

2.6 content-desc 属性分析 描述组件作用的文本

可以根据文本进行元素定位

from appium.webdriver.common.appiumby import AppiumBy
driver.find_element_by_accessibility_id('搜索查询')
driver.find_element(AppiumBy.ACCESSIBILITY_ID, '搜索查询')

2.7 xpath 元素定位

具体的操作介绍,在下边三、Appium Desktop Appium 查看;

2.8 UiSelector 元素定位,多条件定位

参考 这里的谷歌安卓官方文档介绍: https://developer.android.google.cn/training/testing/ui-automator

UiSelector常用的元素选择方法

  • text(“搜索查询”),可以根据元素的文本属性查找元素定位
  • textContains(“python”),根据文本包含什么字符串定位
  • textmartch(),可以使用正则表达式定位
  • className(“android.widget.TextView”),根据组件名称定位
  • resourceId(“tv.danmaku.bili:id/search_src_text”),根据组件ID定位
  • index(1),根据在父元素中的位置下标进行定位,下标从1开始计算,类似xpath 里面的*[n];
  • instance(0),在定位的元素结果中根据下标进行取值,下标从0开始计算;
  • childSelector(new UiSelector().className(“android.widget.TextView”)),在定位的元素中继续定位子元素;
from appium.webdriver.common.appiumby import AppiumBy
code = 'new UiSelector().text("搜索查询").className("android.widget.TextView").resourceId("tv.danmaku.bili:id/search_src_text")'
result = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, code)
childSelectorCode = 'new UiSelector().resourceId("tv.danmaku.bili:id/search_src_text").childSelector(new UiSelector().className("android.widget.TextView"))'
result = driver.find_elements_by_uiautomator(childSelectorCode);

注意上边的code变量,里面写的是Java代码,Java代码只能使用双引号,所以在编写的时候多多注意;

三、 Appium Desktop Appium 分析&定位App界面元素

3.1 启动步骤

1、打开Appium 点击Start Server,启动程序,点击右上角的放大镜
Python Appium 安卓自动化测试 基本使用 - Phone Spider-LMLPHP
2、添加配置

# python代码需要使用到的配置
desired_caps = {
  'platformName': 'Android',  # 被测手机是安卓
  'platformVersion': '10',  # 手机安卓版本
  'deviceName': 'xxx', # 设备名,安卓手机可以随意填写
  'appPackage': 'tv.danmaku.bili',  # 启动APP Package名称
  'appActivity': '.MainActivityV2',  # 启动Activity名称
  'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
  'resetKeyboard': True,  # 执行完程序恢复原来输入法
  'noReset': True,  # 不要重置App
  'newCommandTimeout': 6000,  # 最大请求超时的时间
  'automationName' : 'UiAutomator2'
  # 'app': r'd:\apk\bili.apk',
}
// Appium Desktop  Appium 捕获app界面元素配置
{
  "platformName": "Android",
  "platformVersion": "10",
  "deviceName": "xxx",
  "appPackage": "tv.danmaku.bili",
  "appActivity": ".MainActivityV2",
  "unicodeKeyboard": true,
  "resetKeyboard": true,
  "noReset": true,
  "newCommandTimeout": 6000,
  "automationName": "UiAutomator2"
}

Python Appium 安卓自动化测试 基本使用 - Phone Spider-LMLPHP

3.2 获取界面元素

Python Appium 安卓自动化测试 基本使用 - Phone Spider-LMLPHP

3.3 在Appium Desktop Appium 根据xpath语法进行元素搜索&定位

1、根据class进行定位需要写全://android.widget.TextView
2、根据根据resource-id定位某个组件://*[@resource-id=“tv.danmaku.bili:id/action_search”]
3、根据元素节点进行相对定位:定位热门元素;

定位语法://*[@resource-id=‘tv.danmaku.bili:id/tabs’]//android.view.ViewGroup[3]

from appium.webdriver.common.appiumby import AppiumBy
driver.find_element(AppiumBy.XPATH, "//*[@resource-id='tv.danmaku.bili:id/tabs']//android.view.ViewGroup[3]")
driver.find_element_by_xpath("//*[@resource-id='tv.danmaku.bili:id/tabs']//android.view.ViewGroup[3]")

四、定位注意事项

4.1 element & elements

下面的解释不只是针对xpath定位方法,也包含所有带element字眼的方法
find_element_by_xpath,不带s的表示定位匹配到的第一个,一般用来定位;
find_elements_by_xpath,带s的表示定位匹配到的所有,一般用来获取元素的数据值;

五、python 常规代码

from appium import webdriver
from selenium.webdriver.common.by import By
from appium.webdriver.extensions.android.nativekey import AndroidKey


"""
    获取app activity信息
    打开目标app至手机的当前活动屏幕中, 打开cmd 输入下面的命令 获取手机正在运行的app信息
        adb shell dumpsys activity recents | find "intent={"
    第一行就是手机当前app的信息、提取cmp的信息,.MainActivityV2
        cmp=tv.danmaku.bili/.MainActivityV2
    
"""

desired_caps = {
  'platformName': 'Android',  # 被测手机是安卓
  'platformVersion': '10',  # 手机安卓版本
  'deviceName': 'xxx',  # 设备名,安卓手机可以随意填写
  'appPackage': 'tv.danmaku.bili',  # 启动APP Package名称
  'appActivity': '.MainActivityV2',  # 启动Activity名称
  'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
  'resetKeyboard': True,  # 执行完程序恢复原来输入法
  'noReset': True,  # 不要重置App
  'newCommandTimeout': 6000,
  'automationName': 'UiAutomator2'
  # 'app': r'd:\apk\bili.apk',
}

# 连接Appium Server,初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

# 设置缺省等待时间
driver.implicitly_wait(5)

# 如果有`青少年保护`界面,点击`我知道了`
iknow = driver.find_elements(By.ID, "text3")
if iknow:
    iknow.click()

# 根据id定位搜索位置框,点击
driver.find_element(By.ID, 'expand_search').click()

# 根据id定位搜索输入框,点击
sbox = driver.find_element(By.ID, 'search_src_text')
sbox.send_keys('白月黑羽')
# 输入回车键,确定搜索
driver.press_keycode(AndroidKey.ENTER)

# 选择(定位)所有视频标题
eles = driver.find_elements(By.ID, 'title')

for ele in eles:
    # 打印标题
    print(ele.text)

input('**** Press to quit..')
driver.quit()

10-02 02:07