APP自动化测试-Appium
环境搭建
1,安装client编程库
pycharm中安装appium-python-client
pip install appium-python-client
2,安装Appium Server
3,安装JDK
4,安装 Android SDK
--------------------------------------------------------------------------
手机启动开发者模式,并允许USB调试
打开命令行窗口测试是否连接成功
adb devices -l
查找 应用 Package 和 Activity
adb shell dumpsys activity recents | find "intent={"
例子一:
# 2023-07-04
from appium import webdriver from selenium.webdriver.common.by import By from appium.webdriver.extensions.android.nativekey import AndroidKey desired_caps = { 'platformName': 'android', # 被测手机是安卓 'platformVersion': '12', # 手机安卓版本 'deviceName': 'abc', # 设备名,安卓手机可以随意填写 '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()
参考学习:https://www.byhy.net/tut/auto/appium/01/#%E4%B8%80%E4%B8%AA%E4%BE%8B%E5%AD%90
例子二:
# 导包 from appium import webdriver from selenium.webdriver.common.by import By # 准备自动化配置信息 desired_caps = { # 移动设备平台 'platformName': 'Android', # 平台OS版本号,写整数位即可 'plathformVersion': '12', # 设备的名称--值可以随便写 'deviceName': 'test0106', # 提供被测app的信息-包名,入口信息 'appPackage': 'com.hpbr.bosszhipin', 'appActivity': '.module.launcher.WelcomeActivity', # 如果被测应用没有安装到手机上,可以指定apk的在电脑上路径 # 'app':r'D:\apk\xxx.apk', # 确保自动化之后不重置app 'noReset': True, # 设置session的超时时间,单位秒 'newCommandTimeout': 6000, # 如果不想每次都安装UI2驱动,可以这么设置 'skipServerInstallation': True } # 初始化driver对象-用于控制手机 driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) driver.implicitly_wait(10) # 稳定元素 # 点击放大镜 driver.find_element(By.XPATH, '//*[@resource-id="com.hpbr.bosszhipin:id/ly_menu"]/android.widget.RelativeLayout[2]/.').click() # 搜索框输入职位信息 search_input = driver.find_element(By.ID, 'com.hpbr.bosszhipin:id/et_search') search_input.send_keys('软件测试') # 输入参数 # 选择符合条件的第一个搜索结果 driver.find_element(By.ID, 'com.hpbr.bosszhipin:id/tv_filtered_name').click() # 获取当前页面所有职位信息元素 job_msg = driver.find_elements(By.ID, 'com.hpbr.bosszhipin:id/view_job_card') for job in job_msg: # 输出岗位名称 name = job.find_element(By.ID, 'com.hpbr.bosszhipin:id/tv_position_name') # print(name.text) # 输出薪资 salray = job.find_element(By.ID, 'com.hpbr.bosszhipin:id/tv_salary_statue') # print(salray.text) # 输出公司名称 # 找到元素返回包含一个元素的列表,找不到就返回空列表 company = job.find_elements(By.ID, 'com.hpbr.bosszhipin:id/tv_company_name') # 避免屏幕遮挡了公司名,查到不到目标元素,设置一共默认值 company_text = '空' # 当找打company元素的时候,就使用该元素的文本 if company: company_text = company[0].text print('%s|%s|%s' % (name.text, salray.text, company_text)) # 点击第一个搜索结果 job_msg[0].click() # 获取职位名称下面的信息:地区、工作年限、学历、工作性质 location = driver.find_element(By.ID, 'tv_required_location').text work_exp = driver.find_element(By.ID, 'tv_required_work_exp').text degree = driver.find_element(By.ID, 'tv_required_degree').text print(f'地区:{location}|工作经验:{work_exp}|学历:{degree}') driver.quit()
adb无线连接手机
注意:设置手机和PC在同一网络下,即连接同一WIFI;用USB连接手机
1,查看设备连接
adb devices -l
2,开启手机的5555端口
adb tcpip 5555
3,查看手机ip地址
adb shell ip -f inet addr show wlan0
4,运行connect命令
adb connect 192.168.101.15:5555
5,再次查看设备
adb devices -l
问题:
使用USB调试模式后手机的输入法不能用了?
解决方法:手机设置里面修改输入法。
Appium 服务器初始化参数 (Capability)
https://www.cnblogs.com/poloyy/p/12916569.html
官方文档翻译:https://static.kancloud.cn/testerhome/appium_docs_cn/2001853
常用的ADB命令:
1,连接模拟器
adb connect 127.0.0.1:62001
各个模拟器的port:adb connect 127...1:port(设备)
模拟器名称 默认端口
Genymotion模拟器 5555
夜神模拟器 62001/52001
海马玩模拟器 26944
mumu模拟器 7555
天天模拟器 6555
逍遥安卓模拟器 21503
BlueStacks蓝叠3模拟器 5555
雷神安卓模拟器 5555
腾讯手游助手 5555
2,查看连接的设备
adb devices -l
3,查看被测app的包名及入口启动页面
先进入aapt所在的路径:D: \Andriod_SDK\bui1d-tools\29.0.3>
aapt dump badging apk的路径
有apk时
adb shell dumpsys activity recents | find "intent={"
Appium日志查看
可定位的控件属性
Allure报告
https://blog.csdn.net/weixin_44896406/article/details/127010563
Allure报告生成命令
allure generate case\report -o allure-report --clean
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」