1、导入driver对象
from appium import webdriver
2、声明手机驱动对象
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = '127.0.0.1:7555'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'
desired_caps['app'] = './xx.apk' #取指定路径下的apk
driver=webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
desired_caps常用参数:
platformName 平台名称:iOS,Android,FirefoxOS
platformVersion 设备系统版本号
deviceName 设备号 IOS:instruments -s devices, Android: adb devices
app 安装文件路径:/abs/path/to/my.apk or http://myapp.com/app
appActivity 启动的Activity
appPackage 启动的包
unicodeKeyboard unicode设置(允许中文输入)
resetKeyboard 键盘设置(允许中文输入)
3、关闭当前操作的APP
driver.close_app()
'''运行该命令后会断开设备连接,因此可用driver.keyevent(3)(点击home键)代替'''
4、启动其他APP
driver.start_activity("包名",“启动名”)
5、关闭驱动对象,同时关闭所有关联的APP
driver.quit()
通过工具获取APP元素:
进入D:\application\android-tool\android-sdk_r24.4.1-windows\android-sdk-windows\tools目录下,运行uiautomatorviewer.bat
点击device screenshot with compressed…可获取当前正在连接的设备的当前页面
基础操作:
安装apk: driver.install_app(本机apk_path)
卸载APP:driver.remove_app(app包名)
判断APP是否已安装:dirver.is_app_installed(“包名”)
发送文件到手机:
import base64
data = str(base64.b64encode('utf-8')),'utf-8')
driver.push_file(path,data)
'''
参数:
path:手机设备上的路径(如:/sdcard/a.txt)
data:文件内数据,要求base64编码
Python3.x中字符都为unicode编码,而b64encode函数的参数为byte类型,需要先转码;生成的数据为byte类型,需要将byte转换回去。
'''
data = str(base64.b64encode("push 1234567".encode('utf-8')), 'utf-8')
driver.push_file("/sdcard/push.txt", data)
从手机拉取文件:
phone_data = driver.push_file("/sdcard/push.txt")
print(str(base64.b64decode(phone_data), 'utf-8'))
获取当前屏幕内元素内容:
driver.page_source
(返回当前页面的文档结构,判断特定元素是否存在)
初始化操作
def init_driver():
desired_caps = {}
# 系统
desired_caps['platformName'] = 'Android'
# 版本
desired_caps['platformVersion'] = '6.0.1'
# 设备号
desired_caps['deviceName'] = '127.0.0.1:7555'
# 包名
desired_caps['appPackage'] = 'com.android.settings'
# 启动名
desired_caps['appActivity'] = '.Settings'
# 允许输入中文
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
# desired_caps['app'] = './xx.apk'
# 声明手机驱动对象
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
return driver
定位方式:(基于当前屏幕范围内可见元素)
1、id
driver.find_element_by_id("com.android.settings:id/search").click()
2、class
driver.find_element_by_class_name("android.widget.ImageButton").click()
3、xpath
xpath_value = "//*[contains(@class,'android.widget.TextView')]"
设置显式等待
WebDriverWait(driver, timeout=5, poll_frequency=0.5).until(lambda x: x.find_element_by_class_name("android.widget.LinearLayout"))
打印当前时间
print(time.strftime("%H:%M:%S", time.localtime()))
常用方法:
点击 click()
输入 send_keys()
清空 clear()
获取文本 text
获取属性值 get_attribute(value)
value=‘name’ 返回content-desc / text属性值
value=‘text’ 返回text属性值
value=‘className’ 返回class属性值,API>=18才支持
value='resourceId’返回resource-id属性值,API>=18才支持
获取坐标 location
获取包名 current_package
获取启动名 current_activity
# 按坐标滑动
driver.swipe(101, 1064, 101, 573, 5000)
# 按元素滑动
start_ele = driver.find_elements_by_xpath("//*[contains(@text,'年轻人')]")
end_ele = driver.find_elements_by_xpath("//*[contains(@text,'为什么大家')]")
driver.scroll(start_ele, end_ele)
# # 拖动
driver.drag_and_drop(start_ele, end_ele)
# 应用置于后台
driver.background_app(10)
# 获取手机时间
local_time = driver.device_time
print(local_time)
# 获取手机分辨率
phone_size = driver.get_window_size()
键值:
'''
keyevent(keycode, metastate=None):
参数:
keycode:发送给设备的关键代码
metastate:关于被发送的关键代码的元信息,一般为默认值
'''
# 音量减
for i in range(0, 3):
driver.keyevent(5)
# 打开通知栏
driver.open_notifications()
# 点击home键
driver.keyevent(3)
# 获取手机当前网络
print(driver.network_connection)
'''
Possible values:
Value (Alias) | Data | Wifi | Airplane Mode
-------------------------------------------------
0 (None) | 0 | 0 | 0
1 (Airplane Mode) | 0 | 0 | 1
2 (Wifi only) | 0 | 1 | 0
4 (Data only) | 1 | 0 | 0
6 (All network on) | 1 | 1 | 0
These are available through the enumeration `appium.webdriver.ConnectionType`
'''
# 设置网络(需要root权限)
driver.set_network_connection(1)
# 屏幕截图
driver.get_screenshot_as_file("../screenshot_page/air.png")