美文网首页学习之鸿蒙&Android学习
Android自动化测试——Appium的使用

Android自动化测试——Appium的使用

作者: Peakmain | 来源:发表于2021-07-05 15:54 被阅读0次

adb常用命令

1、获取包名和界面名(activity)

  • window
adb shell dumpsys window | findstr mCurrentFocus
  • Mac/Linux
adb shell dumpsys window windows | grep mFocusedApp
image.jpeg

2、文件传输

adb push 电脑的文件路径 手机文件夹的路径

比如

adb push C:\Users\admin\Desktop\app.apk /sdcard

3、从手机中拉取文件

adb pull 手机的文件路径 电脑的文件夹路径

比如

adb push /sdcard/app.apk C:\Users\admin\Desktopk

4、获取app启动时间

adb shell am start -W 包名/启动名

比如

adb shell am start -W com.android.settings/.Settings
image.jpeg
  • ThisTime:该界面(activity)启动耗时(毫秒)
  • TotalTime:应用自身启动耗时=ThisTime+应用application等资源启动时间(毫秒)
  • WaitTime:系统启动应用耗时=TotalTime+系统资源启动时间(毫秒)

5、获取手机日志

adb logcat

6、adb其他命令


image.png

Appium框架

安装

  pip3 install appium-python-client
  • 查看自己是否已经安装了appium-python-client
pip3 -list
image.jpeg

Hello Appium

  1. 打开测试的应用
  2. 使用adb命令获取包名和界面名
  3. 修改peakmain字典中的appPackage和appActivity的参数
  4. 打开appium-desktop并启动
  5. 新建python项目
# 导模块
from appium import webdriver
import time

peakmain = dict()
# 平台的名字,大小写无所谓,不能乱写
peakmain['platformName'] = 'Android'
# 平台的版本
peakmain['platformVersion'] = '10'
# 设备的名字 随便写,但是不能乱写(比如'')
peakmain['deviceName'] = 'kbzxssdaa685o7ws'
# 要打开的应用程序
peakmain['appPackage'] = 'com.android.settings'
# 需要启动的程序的activity
peakmain['appActivity'] = '.Settings'

# 连接appium
driver = webdriver.Remote('http://localhost:4723/wd/hub', peakmain)

time.sleep(5)

driver.quit()

常用操作API

  1. 脚本内启动其他app
# 导模块
from appium import webdriver
import time

peakmain = dict()
# 手机参数
peakmain['platformName'] = 'Android'
peakmain['platformVersion'] = '10'
peakmain['deviceName'] = 'kbzxssdaa685o7ws'
# 应用参数
peakmain['appPackage'] = 'com.android.settings'
peakmain['appActivity'] = '.Settings'

# 连接appium
driver = webdriver.Remote('http://localhost:4723/wd/hub', peakmain)

time.sleep(3)
driver.start_activity("com.android.mms", ".ui.ConversationList")#启动其他app

time.sleep(5)

# 退出
driver.quit()
  1. 获取app的包名和界面名
# 当前包名
print(driver.current_package)
# 当前界面名
print(driver.current_activity)
  1. 关闭app和驱动对象
# 关闭当前操作的app driver.close_app() 
# 关闭驱动对象,同时关闭所有关联的app driver.quit()
  1. 安装和卸载以及是否安装app
# 安装app
# app_path:apk路径
driver.install_app(app_path)
# 卸载app
# app_id:应用程序包名
driver.remove_app(app_id)
# 是否安装
# app_id:应用程序包名
driver.is_app_installed(app_id)
  1. 将应用置于后台
# second单位秒
 driver.background_app(second)

UIAutomatorViewer

  • 应用场景:定位元素的时候必须根据元素相关特征进行定位。
  • UIAutomatorViewer用来扫描和分析Android应用程序的UI控件的工具
  • 位于Android sdk 目录中(以下是我的目录):C:\Users\admin\AppData\Local\Android\Sdk\tools\bin
  • 双击UIAutomatorViewer
image.jpeg

定位一个元素

  • 首先通过UIAutomatorViewer定位某个元素
  • 通过一个id定位一个元素
# id_value:元素的resource-id属性值 
driver.find_element_by_id(id_value)
  • 通过一个class_name定位一个元素
# class_value元素的class属性值
diver.find_element_by_class_name(class_value)
  • 通过xpath定位一个元素
# xpath_value定位元素的xpath表达式
diver.find_element_by_xpath(xpath_value))
  • 案例
# 导模块
from appium import webdriver
import time

peakmain = dict()
# 手机参数
peakmain['platformName'] = 'Android'
peakmain['platformVersion'] = '10'
peakmain['deviceName'] = 'kbzxssdaa685o7ws'
# 应用参数
peakmain['appPackage'] = 'com.android.settings'
peakmain['appActivity'] = '.Settings'

# 连接appium
driver = webdriver.Remote('http://localhost:4723/wd/hub', peakmain)

# 设置里面的搜索
search_button = driver.find_element_by_id("android:id/input")
search_button.click()
# 搜索里面添加Hello Appium
driver.find_element_by_class_name("miui.widget.ClearableEditText").send_keys("Hello Appium")
# 关闭当前操作的app
# driver.quit()

定位一组元素

titles = driver.find_elements_by_id("android:id/title") 
titles[1].click()

元素等待

  • 隐式等待
    针对所有定位元素的超时时间为同一个值得时候
driver.implicitly_wait(10)# 单位是s
  • 显示等待
    • 关键类:WebDriverWait
    • 显示等待为单个元素有效,隐式为全局元素
    • 在设置了显示等待之后,可以等待一个超时时间,在这个超时时间之内进行查找,默认每0.5s找一次
    • 0.5s的帧率是可以设置的
    • 一旦找到这个元素,直接进行后续操作
    • 如果没找到,就会报错
# 导模块
from appium import webdriver
import time

from selenium.webdriver.support.wait import WebDriverWait

peakmain = dict()
# 手机参数
peakmain['platformName'] = 'Android'
peakmain['platformVersion'] = '10'
peakmain['deviceName'] = 'kbzxssdaa685o7ws'
# 应用参数
peakmain['appPackage'] = 'com.android.settings'
peakmain['appActivity'] = '.Settings'

# 连接appium
driver = webdriver.Remote('http://localhost:4723/wd/hub', peakmain)
# 等待3s,每1s找一次
wait = WebDriverWait(driver, 3,1)
element = wait.until(lambda x: x.find_elements_by_id("android:id/title"))[1]
element.click()

元素操作API

  • 点击元素
element.click()
  • 输入
element.send_keys(value)
  • 清空
element.clear()
  • 元素文本内容
element.text
  • 获取元素的位置和大小
element.location # 元素的位置,字典中有x和y两个key 
element.size # 元素的大小,字典中有width和height两个key
  • 根据属性名获取属性值
element.get_attribute(value) # 元素的属性

示例

titles = driver.find_elements_by_id("android:id/title")
for title in titles:
    print(title.get_attribute("enabled"))
    print(title.get_attribute("text"))
    print(title.get_attribute("name"))
    print(title.get_attribute("resourceId"))
    print(title.get_attribute("className"))
  • swipe滑动事件
    • 方法名
# 从一个位置滑动到另一个坐标位置
# start_x:起点x的坐标
# start_y:起点y的坐标
# end_x:终点x的坐标
# end_y:终点y的坐标
# druation:滑动这个操作一共持续的时间长度,单位:ms
driver.swipe(start_x,start_y,end_x,end_y,druation=None)
  • scroll
    不能设置持续时间,惯性大
# 从一个元素滑动到另一个元素
# origin_el:滑动开始的元素
# destination_el:滑动结束的元素
driver.scroll(origin_el: WebElement, destination_el: WebElement)
  • darg_and_drop

不能设置持续时间,没有惯性

# 从一个元素拖拽到另一个元素
# origin_el:滑动开始的元素
# destination_el:滑动结束的元素
driver.drag_and_drop(origin_el: WebElement, destination_el: WebElement)

高级手势TouchAction

TouchAction可以实现一些针对手势的操作,比如滑动、长按、拖拽等。我们可以将这些基本手势组合成一个相对复杂的手势,比如:我们解锁手机

  • 轻敲
# 导模块
from appium import webdriver
import time

from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait

peakmain = dict()
# 手机参数
peakmain['platformName'] = 'Android'
peakmain['platformVersion'] = '10'
peakmain['deviceName'] = 'kbzxssdaa685o7ws'
# 应用参数
peakmain['appPackage'] = 'com.android.settings'
peakmain['appActivity'] = '.Settings'

# 连接appium
driver = webdriver.Remote('http://localhost:4723/wd/hub', peakmain)

title = driver.find_elements_by_id("android:id/title")[1]
# 轻敲
TouchAction(driver).tap(title).perform()
# 关闭当前操作的app
# driver.quit()
  • 按下
TouchAction(driver).press(title).perform()
  • 抬起
# 导模块
from appium import webdriver
import time

from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait

peakmain = dict()
# 手机参数
peakmain['platformName'] = 'Android'
peakmain['platformVersion'] = '10'
peakmain['deviceName'] = 'kbzxssdaa685o7ws'
# 应用参数
peakmain['appPackage'] = 'com.android.settings'
peakmain['appActivity'] = '.Settings'

# 连接appium
driver = webdriver.Remote('http://localhost:4723/wd/hub', peakmain)

title = driver.find_elements_by_id("android:id/title")[1]
TouchAction(driver).press(title).perform()
time.sleep(2)
TouchAction(driver).release().perform()
# 关闭当前操作的app
# driver.quit()
  • 等待
TouchAction(driver).wait(time).perform()# time单位是毫秒
  • 长按
TouchAction(driver).long_press(title).perform()
  • 移动
TouchAction(driver).move_to(100, 100).perform()

手机操作API

  • 分辨率
driver.get_window_size()
  • 截图
driver.get_screenshot_as_file(filename)
# 示例
driver.get_screenshot_as_file(os.getcwd() + os.sep + "./srceen.png")
  • 获取和设置手机网络
image.jpeg
driver.network_connection # 获取网络 
driver.set_network_connection(1) # 设置网络
  • 发送键到设备
# keycode 
# metastate一般默认值 
driver.press_keycode(keycode,metastate)
  • 操作通知栏
driver.open_notifications()

相关文章

网友评论

    本文标题:Android自动化测试——Appium的使用

    本文链接:https://www.haomeiwen.com/subject/kfbysltx.html