学习之鸿蒙&Android学习

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

2021-07-05  本文已影响0人  Peakmain

adb常用命令

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

adb shell dumpsys window | findstr mCurrentFocus
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

5、获取手机日志

adb logcat

6、adb其他命令


image.png

Appium框架

安装

  pip3 install 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

image.jpeg

定位一个元素

# id_value:元素的resource-id属性值 
driver.find_element_by_id(id_value)
# class_value元素的class属性值
diver.find_element_by_class_name(class_value)
# 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
# 导模块
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"))
# 从一个位置滑动到另一个坐标位置
# 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)
# 从一个元素滑动到另一个元素
# origin_el:滑动开始的元素
# destination_el:滑动结束的元素
driver.scroll(origin_el: WebElement, destination_el: WebElement)

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

# 从一个元素拖拽到另一个元素
# 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()
上一篇 下一篇

猜你喜欢

热点阅读