当前位置:网站首页>Appium automation test foundation - appium basic operation API (II)
Appium automation test foundation - appium basic operation API (II)
2022-07-05 15:29:00 【Test - Eight Precepts】
5、 Send files to the phone and get the files in the phone
(1) send out ⽂ Pieces arrive at ⼿ machine
code snippet :
# Import base64 library
import base64
# Convert files to binary files
with open(file_path,'rb') as fp:
data = str(base64.b64encode(fp.read()),'utf-8')
# print(data)
# Send the converted file to the phone
driver.push_file(path, data)
Parameter description :file_path
: File path to upload .path
:⼿ Path on the device ( for example :/sdcard/a.txt)data
:⽂ In piece data , requirement base64 code .
explain :
Python3.x All Chinese characters are unicode code , You need to import base64
Library for encoding and decoding , First turn the file into base64
Binary files in format , Then transfer the file to the mobile phone , Because the transfer between devices is binary .
(2) from ⼿ Pull in the machine ⽂ Pieces of
code snippet :
import base64
# The returned data is base64 Encoded data
data = driver.pull_file(path)
# base64 decode
with open('a.txt','wb') as fp:
fp.write(base64.b64decode(data))
Parameters :path
:⼿ The path on the device, for example : /sdcard/a.txt
(3) Example :
# 1. Import appium
import time
from appium import webdriver
import base64
# 2. establish Desired capabilities object , Add startup parameters
desired_caps = {
"platformName": "Android", # System name
"platformVersion": "7.1.2", # System version
"deviceName": "127.0.0.1:21503", # Equipment name
"appPackage": "com.cyanogenmod.filemanager", # APP Package name
"appActivity": ".activities.NavigationActivity" # APP Start name
}
# 3. start-up APP
# Declare the mobile phone driver object ( Instantiation webdriver)
# The first parameter is zero appium Address of service , Need to start the appium service .
# The second parameter is Desired capabilities object
# Let's just pass in these two parameters first .
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4. operation APP
# 4.1 Send files to your phone
file_path = r'C:\Users\L\Desktop\test.txt'
# Convert files to binary files
with open(file_path, 'rb') as fp:
data = str(base64.b64encode(fp.read()), 'utf-8')
# print(data)
# Send the converted file to the phone
path = r'/sdcard/test.txt'
driver.push_file(path, data)
# 4.2 Pull files from your phone to your computer
# The path of the file in the phone
path_app = '/sdcard/test.txt'
# The returned data is base64 Encoded data
data = driver.pull_file(path_app)
print(data)
# base64 decode
with open('test.txt', 'wb') as fp:
fp.write(base64.b64decode(data))
# Tips : The file will be pulled to the directory where the script file is located
# 5. close APP
time.sleep(5)
driver.quit()
6、 Get the element structure in the current screen ( a key )
( That is, get the source code of the current screen )
The use of API:
driver.page_source
do ⽤:
Return to the current page ⾯ Of ⽂ Gear structure , It can provide a premise for the subsequent judgment of whether a specific element exists .
Example :
# 1. Import appium
import time
from appium import webdriver
# 2. establish Desired capabilities object , Add startup parameters
desired_caps = {
"platformName": "Android", # System name
"platformVersion": "7.1.2", # System version
"deviceName": "127.0.0.1:21503", # Equipment name
"appPackage": "com.android.settings", # APP Package name
"appActivity": ".Settings" # APP Start name
}
# 3. start-up APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4. operation APP
# Get the current source page
# Just set the page source code in the home page
source = driver.page_source
# # print(source)
# take app Save the page source code to a file
with open("source.txt", "w", encoding="UTF-8") as fp:
fp.write(source)
# 5. close APP
time.sleep(3)
driver.quit()
7、 Start other programs within the script app
The use of API:
driver.start_activity(appPackage,appActivity)
Tips :appPackage,appActivity Start for the desired app Package name and startup name
Example :
# From management app Page to open the file manager app
# 1. Import appium
import time
from appium import webdriver
# 2. establish Desired capabilities object , Add startup parameters
desired_caps = {
"platformName": "Android", # System name
"platformVersion": "7.1.2", # System version
"deviceName": "127.0.0.1:21503", # Equipment name
"appPackage": "com.android.settings", # APP Package name
"appActivity": ".Settings" # APP Start name
}
# 3. start-up APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4. operation APP
# From settings app Page launch file manager APP
time.sleep(3)
# First use adb Command to get the package name and startup name of the file manager
# com.cyanogenmod.filemanager/.activities.NavigationActivity
driver.start_activity("com.cyanogenmod.filemanager", ".activities.NavigationActivity")
# 5. close APP
time.sleep(3)
driver.quit()
8、 Put the application in the background ( a key )
The use of API:
# Put the application in the background ( second )
driver.background_app()
Example :
# 1. Import appium
import time
from appium import webdriver
# 2. establish Desired capabilities object , Add startup parameters
desired_caps = {
"platformName": "Android", # System name
"platformVersion": "7.1.2", # System version
"deviceName": "127.0.0.1:21503", # Equipment name
"appPackage": "com.android.settings", # APP Package name
"appActivity": ".Settings" # APP Start name
}
# 3. start-up APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 4. operation APP
time.sleep(3)
# Set app Running in the background
# Run the app in the background 5 second , Back to the front desk
driver.background_app(5)
# Tips : Before testing, it's best to put all the background running in the equipment app All shut down .
# 5. close APP
time.sleep(3)
driver.quit()
Tips : In the test app During hot start , You will often use this command .
a key : Supporting learning materials and video teaching
So here I have carefully prepared the detailed information of the above outline in The link below is as follows
边栏推荐
- 做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking
- Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
- Summary of the second lesson
- Bugku easy_ nbt
- Number protection AXB function! (essence)
- Calculate weight and comprehensive score by R entropy weight method
- Install PHP extension spoole
- Definition of episodic and batch
猜你喜欢
一文搞定vscode编写go程序
Bugku alert
数学建模之层次分析法(含MATLAB代码)
P6183 [USACO10MAR] The Rock Game S
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
30岁汇源,要换新主人了
数据库学习——数据库安全性
keep-alive
Misc Basic test method and knowledge points of CTF
随机推荐
Brief introduction of machine learning framework
CSRF, XSS science popularization and defense
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
【简记】解决IDE golang 代码飘红报错
How can the boss choose programmers to help me with development?
Redis distributed lock principle and its implementation with PHP (2)
keep-alive
Leetcode: Shortest Word Distance II
sql server char nchar varchar和nvarchar的区别
F. Min cost string problem solving Report
MySQL 巨坑:update 更新慎用影响行数做判断!!!
Redis distributed lock principle and its implementation with PHP (1)
[JVM] operation instruction
Usage and usage instructions of JDBC connection pool
Bugku's Eval
Array sorting num ranking merge in ascending order
P6183 [USACO10MAR] The Rock Game S
Advanced level of static and extern
CSDN I'm coming
Xiao Sha's arithmetic problem solving Report