完成app自动化需要一些基础条件的支持,本节将讲解APP初始化API.
前置代码
# server 启动参数 desired_caps = {} desired_caps[\'platformName\'] = \'Android\' desired_caps[\'platformVersion\'] = \'5.1\' desired_caps[\'deviceName\'] = \'192.168.56.101:5555\' desired_caps[\'appPackage\'] = \'com.android.settings\' desired_caps[\'appActivity\'] = \'.Settings\' desired_caps[\'unicodeKeyboard\'] = True desired_caps[\'resetKeyboard\'] = True # 声明driver对象 driver = webdriver.Remote(\'http://127.0.0.1:4723/wd/hub\', desired_caps)
安装APK到手机
driver.install_app(app_path)
参数:
app_path:脚本机器中APK文件路径
手机中移除APP
driver.remove_app(app_id)
参数:
app_id:需要卸载的app包名
判断APP是否已安装
driver.is_app_installed(bundle_id)
参数:
bundle_id: 可以传入app包名,返回结果为True(已安装) / False(未安装)
发送文件到手机
import base64 data = str(base64.b64encode(data.encode(\'utf-8\')),\'utf-8\') driver.push_file(path,data) 参数: path:手机设备上的路径(例如:/sdcard/a.txt) data:文件内数据,要求base64编码 Python3.x中字符都为unicode编码,而b64encode函数的参数为byte类型,需要先转码; 生成的数据为byte类型,需要将byte转换回去。
从手机中拉取文件
import base64 data = driver.pull_file(path) # 返回数据为base64编码 print(str(base64.b64decode(data),\'utf-8\')) # base64解码 参数: path: 手机设备上的路径
获取当前屏幕内元素结构
driver.page_source
作用:
返回当前页面的文档结构,判断特定的元素是否存在