- Appium前置代码
# 导入driver对象
from appium import webdriver
# server 启动参数
desired_caps = {}
# 设备信息(系统、版本、设备号)
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = '192.168.72.103:5555'
# app信息(包名、启动名)
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
# 声明driver对象
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
try:
pass
except Exception as e:
print(e)
finally:
# 关闭驱动对象
driver.quit()
一、ADB
-
发送桌面文件a.txt到手机的sdcard目录下
touch a.txt
adb push a.txt /sdcard
adb shell
ls /sdcard
-
从手机的/sdcard目录下拉取a.txt到D盘
mkdir d: est001
adb pull /sdcard/a.txt d: est001
-
通过adb安装、卸载软件
adb install [路径/xxx.apk]
adb uninstall [包名]
-
获取模拟机中通讯录运行日志并输出到文件
先启动通讯录
adb shell dumpsys window windows | grep mFocusedApp |cut -d " " -f7
adb logcat | grep com.android.contacts >>./contacts.log
tail -f contacts.log
提示:
- Windows
findstr
- Mac
grep
- 缓冲区字节数未满,则日志为空
- 监控模拟机中通讯录实时运行CPU使用率输出到文件
adb shell top | grep com.android.contacts >> ./contacts_cpu.txt
tail -f contacts_cpu.txt
6. 获取某软件的自身启动的时间和系统启动软件时间
先启动软件
adb shell dumpsys window windows | grep mFocusedApp |cut -d " " -f7
adb shell am start -W 包名/启动名 | grep Time | tail -n 2
二、Appium
- 判断某软件是否安装在模拟机中,如果安装就卸载,未安装就安装
第一种:
# 导入driver对象
from appium import webdriver
import time
import logging
# server 启动参数
desired_caps = {}
# 设备信息(系统、版本、设备号)
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = '192.168.72.103:5555'
# app信息(包名、启动名)
desired_caps['appPackage'] = 'com.android.launcher3'
desired_caps['appActivity'] = '.Launcher'
# 声明driver对象
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
try:
phone_size = driver.get_window_size()
x = phone_size.get("width")*0.5
y_0 = phone_size.get("height")*0.7
y_1 = phone_size.get("height")*0.3
if driver.is_app_installed("com.oasisfeng.greenify"):
driver.remove_app("com.oasisfeng.greenify")
print("卸载成功")
else:
driver.install_app("C:/Users/zifeng/Desktop/Greenify.apk")
print("APK安装成功")
driver.swipe(x, y_0, x, y_1)
driver.get_screenshot_as_file("C:/Users/zifeng/Desktop/screen.png")
except Exception as e:
print(e)
finally:
# 关闭驱动对象
driver.quit()
- 发送当前文件b.txt 到手机的/sdcrad目录下
touch b.txt
with open("C:/Users/zifeng/Desktop/b.txt", 'r', encoding='utf-8') as f:
data = str(base64.b64encode(f.read().encode('utf-8')), 'utf-8')
driver.push_file("/sdcard/b1.txt", data)
print("文件传输成功")
- 从手机目录/sdcard中读取b1.txt,存储到桌面并命名为phone.txt
data = driver.pull_file("/sdcard/b1.txt")
with open("C:/Users/zifeng/Desktop/phone.txt", 'w') as f:
f.write(str(base64.b64decode(data), 'utf-8'))
print("文件读取到桌面成功")
- 判断模拟机中计算器是否包含删除,包含打印True,不包含打印False
driver.start_activity("com.android.calculator2", ".Calculator")
ele_calc_button = driver.find_elements_by_xpath("//*[contains(@class,'android.widget.Button')]")
tag = 1
for i in ele_calc_button:
if i.get_attribute("name") == "DEL" or i.get_attribute("name") == "删除":
print(True)
tag = 0
break
if tag == 1:
print(False)
- 进入模拟机短信,给用户13488834010的号码连续发三条短信,内容分别是123456、sd345、你好
# 导入driver对象
import base64
from appium import webdriver
import time
import logging
# server 启动参数
desired_caps = {}
# 设备信息(系统、版本、设备号)
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = '192.168.72.103:5555'
# app信息(包名、启动名)
desired_caps['appPackage'] = 'com.android.messaging'
desired_caps['appActivity'] = '.ui.conversationlist.ConversationListActivity'
# 声明driver对象
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
try:
# pass
driver.find_element_by_id("com.android.messaging:id/start_new_conversation_button").click()
phone_num = driver.find_element_by_class_name("android.widget.MultiAutoCompleteTextView")
phone_num.click()
phone_num.send_keys("13488834010")
driver.keyevent(66)
data = driver.find_element_by_id("com.android.messaging:id/compose_message_text")
for i in ("123456", "sd345", "你好"):
data.clear()
data.send_keys(i)
driver.find_element_by_id("com.android.messaging:id/self_send_icon").click()
for i in range(2):
driver.keyevent(4)
time.sleep(2)
except Exception as e:
print(e)
finally:
# 关闭驱动对象
driver.quit()
三、Pytest
更新中......