当前位置:网站首页>Appium自动化测试基础 — APPium基础操作API(二)
Appium自动化测试基础 — APPium基础操作API(二)
2022-07-05 15:03:00 【测试-八戒】
5、发送文件到手机和获取手机中的文件
(1)发送⽂件到⼿机
代码片段:
# 导入base64库
import base64
# 将文件转换成二进制文件
with open(file_path,'rb') as fp:
data = str(base64.b64encode(fp.read()),'utf-8')
# print(data)
# 将转换格式的文件发送到手机
driver.push_file(path, data)参数说明:file_path:需要上传的文件路径。path:⼿机设备上的路径(例如:/sdcard/a.txt)data:⽂件内数据,要求base64编码。
说明:
Python3.x中字符都为unicode编码,需要先导入base64库进行编码和解码,先把文件转成base64格式的二进制文件,然后进行文件传递到手机,因为设备之间传递是二进制的。
(2)从⼿机中拉取⽂件
代码片段:
import base64
# 返回数据为base64编码的数据
data = driver.pull_file(path)
# base64解码
with open('a.txt','wb') as fp:
fp.write(base64.b64decode(data)) 参数:path:⼿机设备上的路径例如: /sdcard/a.txt
(3)示例:
# 1.导入appium
import time
from appium import webdriver
import base64
# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
"platformName": "Android", # 系统名称
"platformVersion": "7.1.2", # 系统版本
"deviceName": "127.0.0.1:21503", # 设备名称
"appPackage": "com.cyanogenmod.filemanager", # APP包名
"appActivity": ".activities.NavigationActivity" # APP启动名
}
# 3.启动APP
# 声明手机驱动对象(实例化webdriver)
# 第一个参数为appium服务的地址,需要启动appium服务。
# 第二个参数为Desired capabilities对象
# 我们就先传入这两个参数就可以了。
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4.操作APP
# 4.1 发送文件到手机
file_path = r'C:\Users\L\Desktop\test.txt'
# 将文件转换成二进制文件
with open(file_path, 'rb') as fp:
data = str(base64.b64encode(fp.read()), 'utf-8')
# print(data)
# 将转换格式的文件发送到手机
path = r'/sdcard/test.txt'
driver.push_file(path, data)
# 4.2 从手机中拉取文件到电脑上
# 手机中文件的路径
path_app = '/sdcard/test.txt'
# 返回数据为base64编码的数据
data = driver.pull_file(path_app)
print(data)
# base64解码
with open('test.txt', 'wb') as fp:
fp.write(base64.b64decode(data))
# 提示:该文件会拉取到脚本文件所在的目录中
# 5.关闭APP
time.sleep(5)
driver.quit()6、获取当前屏幕内元素结构(重点)
(也就是获取当前屏幕的源码)
使用的API:
driver.page_source
作⽤:
返回当前页⾯的⽂档结构,可以为后续判断特定的元素是否存在提供前提。示例:
# 1.导入appium
import time
from appium import webdriver
# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
"platformName": "Android", # 系统名称
"platformVersion": "7.1.2", # 系统版本
"deviceName": "127.0.0.1:21503", # 设备名称
"appPackage": "com.android.settings", # APP包名
"appActivity": ".Settings" # APP启动名
}
# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4.操作APP
# 获取当前页面源码
# 只是设置首页中的页面源码
source = driver.page_source
# # print(source)
# 将app的页面源码保存到一个文件中
with open("source.txt", "w", encoding="UTF-8") as fp:
fp.write(source)
# 5.关闭APP
time.sleep(3)
driver.quit()7、脚本内启动其他app
使用的API:
driver.start_activity(appPackage,appActivity)
提示:appPackage,appActivity为所要启动app的包名和启动名示例:
# 从管理app页面中打开文件管理器app
# 1.导入appium
import time
from appium import webdriver
# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
"platformName": "Android", # 系统名称
"platformVersion": "7.1.2", # 系统版本
"deviceName": "127.0.0.1:21503", # 设备名称
"appPackage": "com.android.settings", # APP包名
"appActivity": ".Settings" # APP启动名
}
# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4.操作APP
# 从设置app页面启动文件管理器APP
time.sleep(3)
# 先用adb命令获取文件管理器的包名和启动名
# com.cyanogenmod.filemanager/.activities.NavigationActivity
driver.start_activity("com.cyanogenmod.filemanager", ".activities.NavigationActivity")
# 5.关闭APP
time.sleep(3)
driver.quit()
8、将应用程序置于后台运行(重点)
使用的API:
# 将应用置于后台运行(秒)
driver.background_app()示例:
# 1.导入appium
import time
from appium import webdriver
# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
"platformName": "Android", # 系统名称
"platformVersion": "7.1.2", # 系统版本
"deviceName": "127.0.0.1:21503", # 设备名称
"appPackage": "com.android.settings", # APP包名
"appActivity": ".Settings" # APP启动名
}
# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4.操作APP
time.sleep(3)
# 将设置app置于后台运行
# 将应用在后台运行5秒,返回前台
driver.background_app(5)
# 提示:测试前最好把设备中所有后台运行的app都关闭掉。
# 5.关闭APP
time.sleep(3)
driver.quit()提示:在测试app热启动的时候,就会常用到该命令。
重点:配套学习资料和视频教学
那么在这里我也精心准备了上述大纲的详细资料在下方链接如下


边栏推荐
- Jmeter性能测试:ServerAgent资源监控
- How to paste the contents copied by the computer into mobaxterm? How to copy and paste
- Bugku's eyes are not real
- Good article inventory
- NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读
- Redis distributed lock principle and its implementation with PHP (2)
- B站做短视频,学抖音死,学YouTube生?
- 漫画:程序员不是修电脑的!
- Bugku's Eval
- easyOCR 字符识别
猜你喜欢
随机推荐
Misc Basic test method and knowledge points of CTF
30岁汇源,要换新主人了
Common interview questions about swoole
qt creater断点调试程序详解
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
[JVM] operation instruction
Huiyuan, 30, is going to have a new owner
ICML 2022 | 探索语言模型的最佳架构和训练方法
Ctfshow web entry information collection
MySQL之CRUD
计算中间件 Apache Linkis参数解读
CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
Huawei Hubble incarnation hard technology IPO harvester
How to solve the problem of garbled code when installing dependency through NPM or yarn
Selection and use of bceloss, crossentropyloss, sigmoid, etc. in pytorch classification
mapper. Comments in XML files
复现Thinkphp 2.x 任意代码执行漏洞
Select sort and bubble sort
729. My schedule I: "simulation" & "line segment tree (dynamic open point) &" block + bit operation (bucket Division) "
Bugku cyberpunk









