先封装初始化参数至一个文件中:
from appium import webdriver def init_driver(): desired_caps = {} # 系统 desired_caps['platformName'] = 'Android' # 版本 desired_caps['platformVersion'] = '6.0' # 设备号 desired_caps['deviceName'] = 'LE67A06300301948' # 包名 desired_caps['appPackage'] = 'com.android.settings' # 启动名 desired_caps['appActivity'] = '.Settings' # 声明手机驱动对象 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) return driver
在导入使用:
from init_driver.Init_driver import init_driver import base64 import os # 传文件到手机上 def push(tag, pc_path, phone_path, driver=None): # tag 1:adb tag 2:appium if tag == 1: os.system("adb push %s %s" % (pc_path, phone_path)) if tag == 2: with open(pc_path, "r") as f: data = str(base64.b64encode(f.read().encode("utf-8")), "utf-8") driver.push_file(phone_path, data) if __name__ == "__main__": # adb验证 push(tag=1, pc_path="./xm.txt", phone_path="/sdcard") # appium验证 driver = init_driver() push(tag=2, pc_path="./测试.txt", phone_path="/sdcard/测试.txt", driver=driver)