一、 工具说明
- 主流工具
- app自动化执行原理
- app类型(技术)
二、工具安装
2.3 adb工具
说明:通过电脑,操作android系统工具。
- adb工作原理
adb命令:
-
获取包名和启动名
- 包名:
一个安卓应用的唯一标识符,操作哪个应用需要依赖包名。
- 启动名:
应用界面中标识符,允许重复。
- 包名:
1、mac/linux:adb shell dumpsys window | grep usedApp
2、windows:adb shell dumpsys window | findstr usedApp
用adb shell dumpsys window | grep usedApp 命令查看app的启动入口,启动app时报错如下:
报错原因:当前的appActivity设置的不是app首次进入的appActivity
解决办法:获取首次应用的活动名称可以通过 启动一次随机 monkey命令来获取 adb shell monkey -p 包名 -v -v -v 1 或 adb logcat ActivityManager:I | grep "cmp"
adb shell monkey -p com.digifinex.app -v -v -v 1
- 上传和下载
- 启动时间命令
- 命令:
adb shell am start -W 包名.启动名
- 命令:
注意:查看时间一般要冷启动(应用程序没有启动)
冷启动:应用程序未启动
热启动:应用程序已启动在后台或当前页面。
-
查看日志
- 命令:
adb logcat > /Users/muwenping/Downloads/xxx.log
- 提示:
对app操作时,要开启日志,记录app操作的步骤。
- 命令:
-
其他场景命令:
提示:
1、adb start-server 正常不需要手动启动,自动启动adb.exe进程。当应用进程死机,需要执行杀服务,杀完后需手动启动。
2、adb connect ip:端口 正常不需要手动连接,系统会自动连接。如果执行adb devices 没有看到设备列表,需要手动连接。
2.4 查看应用属性信息
为什么要查看元素信息?
说明:自动化测试就是查找元素操作元素,要查找元素,就需要根据元素的信息来查找(id、class、text...)
如何查找?
使用:android SDK 自带工具:uiautomatorviewer工具
如何使用?
-
启动:
-
截屏查看:
-
使用常见问题:
三、基础操作API
-
1.1 入门示例:
前置:必须启动appium服务和真机/模拟器。
from appium import webdriver
#定义字典变量
desired_caps = {}
#字典追加启动参数
desired_caps["platformName"] = "Android"
#注意:版本号必须正确
desired_caps["platformVersion"] = "11"
#android不检测内容,但是不能为空
desired_caps["deviceName"] = "13231FDD4003XJ"
desired_caps["appPackage"] = "com.digifinex.app"
desired_caps["appActivity"] = ".ui.SplashActivity"
#设置中文
desired_caps["nicodeKeyboard"] = True
desired_caps["resetKeyboard"] = True
#获取driver
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
-
1.2 基础api
-
启动应用
-
方法:
driver.start_activity(包名,启动名)
-
说明:appium支持跨应用,可以在操作应用中切换到其他应用。
-
-
获取当前包名、启动名
-
方法:
- 获取当前所在应用包名:driver.current_package
- 获取当前所在应用启动名:driver.current_activity
-
练习:
-
'''
需求:
1、启动digifinex之后,暂停3秒,打开短信应用
2、打印当前默认应用包名和启动名
'''
sleep(3)
#启动短息
driver.start_activity("com.google.android.apps.messaging",".ui.ConversationListActivity")
#打印包名和启动名
print("当前所在应用包名:",driver.current_package)
print("当前所在应用启动名",driver.current_activity)
driver.quit()
- 其他基础Api练习
'''
需求:
1、判断digifinex是否安装,如果安装进行卸载,否则进行安装
2、启动短信界面
3、置于后台3秒钟
4、关闭短息界面
5、关闭app驱动
'''
#判断digifinex是否安装
if driver.is_app_installed("com.digifinex.app"):
print("已安装digifinex,现在执行卸载")
#卸载
driver.remove_app("com.digifinex.app")
else:
#安装
print("未安装digifinex,现在执行安装")
driver.install_app("/Users/muwenping/Downloads/DigiFinex_V2023.10.12_debugT.apk")
#启动短信界面
driver.start_activity("com.google.android.apps.messaging",".ui.ConversationListActivity")
sleep(3)
#置于后台3秒钟
driver.background_app(3)
sleep(3)
#关闭短息
driver.close_app()
print("关闭短信app后,获取包名:",driver.current_package)
sleep(3)
#关闭app驱动
driver.quit()
- 1.3 单个元素定位
示例:
#点击用户头像
driver.find_element_by_xpath("//*[@resource-id='com.digifinex.app:id/iv_icon']").click()
sleep(3)
#点击登录按钮
driver.find_element_by_xpath("//*[@text='Log In']").click()
sleep(3)
#切换到邮箱进行登录
driver.find_element_by_xpath("//*[@resource-id='com.digifinex.app:id/tl_title']/android.widget.LinearLayout[1]/android.widget.RelativeLayout[2]/android.widget.LinearLayout[1]").click()
sleep(3)
#使用id->输入邮箱
driver.find_element_by_id("com.digifinex.app:id/et_email").send_keys("zichangaibantest2@163.com")
sleep(10)
#使用class点击next
driver.find_element_by_xpath("//*[@resource-id='com.digifinex.app:id/tv_continue']").click()
sleep(3)
#使用xpath输入OPT验证码
driver.find_element_by_xpath("//*[@resource-id='com.digifinex.app:id/pie_code']").send_keys("111111")
sleep(3)
#使用id->谷歌验证码
driver.find_element_by_id("com.digifinex.app:id/et_google").send_keys("111111")
sleep(3)
#点击ok按钮
driver.find_element_by_id("com.digifinex.app:id/tv_confirm").click()
sleep(5)