当前位置:网站首页>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热启动的时候,就会常用到该命令。
重点:配套学习资料和视频教学
那么在这里我也精心准备了上述大纲的详细资料在下方链接如下


边栏推荐
- Common MySQL interview questions
- The difference between abstract classes and interfaces in PHP (PHP interview theory question)
- SQL Server learning notes
- Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
- P1451 calculate the number of cells / 1329: [example 8.2] cells
- Common redis data types and application scenarios
- Mysql---- function
- How to solve the problem of garbled code when installing dependency through NPM or yarn
- sql server char nchar varchar和nvarchar的区别
- Huiyuan, 30, is going to have a new owner
猜你喜欢

Good article inventory

Huawei Hubble incarnation hard technology IPO harvester

I spring web upload

P1451 求细胞数量/1329:【例8.2】细胞

Detailed explanation of QT creator breakpoint debugger

Ctfshow web entry information collection

B站做短视频,学抖音死,学YouTube生?
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

Reasons and solutions for redis cache penetration and cache avalanche

CPU design related notes
随机推荐
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
Common interview questions about swoole
Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
MySQL之CRUD
What are CSRF, XSS, SQL injection, DDoS attack and timing attack respectively and how to prevent them (PHP interview theory question)
Bugku's Eval
Good article inventory
Detailed explanation of usememo, memo, useref and other relevant hooks
把 ”中台“ 的思想迁移到代码中去
CODING DevSecOps 助力金融企业跑出数字加速度
sql server学习笔记
Easyocr character recognition
[12 classic written questions of array and advanced pointer] these questions meet all your illusions about array and pointer, come on!
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
Jmeter性能测试:ServerAgent资源监控
社区团购撤城“后遗症”
CPU design practice - Chapter 4 practice task 3 use pre delivery technology to solve conflicts caused by related issues
Talking about how dataset and dataloader call when loading data__ getitem__ () function
Fr exercise topic --- comprehensive question
Bugku's eyes are not real