Python +appium 封装desired_caps模块
16lz
2021-01-24
使用python+appium做android的自动化测试时,首先需要启动appium服务,然后连接上手机,配置如下:
desired_caps = {"platformName": "Android", "deviceName": "127.0.0.1:62001", "platformVersion": "4.4.2", # apk包名 "appPackage": "com.ss.android.article.news", # apk的launcherActivity "appActivity": "com.ss.android.article.news.activity.SplashActivity", "unicodeKeyboard": True, "resetKeyboard": True}driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
但是换一个app的话就需要改动很多代码,很不方便,可以将参数在配置文件里
def desired_caps(self): desired_caps = {} desired_caps["platformName"] = self.data["platformName"] desired_caps["deviceName"] = self.data["deviceName"] desired_caps["platformVersion"] = self.data["platformVersion"] desired_caps["appPackage"] = self.data["appPackage"] desired_caps["appActivity"] = self.data["appActivity"] desired_caps["noReset"] = self.data["noReset"] desired_caps["unicodeKeyboard"] = self.data["unicodeKeyboard"] desired_caps["resetKeyboard"] = self.data["resetKeyboard"] driver = webdriver.Remote("http://" + str(self.data["host"]) + ":" + str(self.data["port"]) + "/wd/hub", desired_caps)
配置文件使用的yaml文件
platformName: AndroidplatformVersion: 4.4.2deviceName: 127.0.0.1:62001appPackage: com.ss.android.article.newsappActivity: com.ss.android.article.news.activity.SplashActivitynoReset: TrueunicodeKeyboard: TrueresetKeyboard: Truehost: 127.0.0.1port: 4723
但是其实把参数写在了配置文件里。每次启动的时候还是需要打开appium服务。现在使用adb命令来启动appium,一起封装在desired_caps里面
class adb_start_appium(object): def __init__(self): with open('C://Users/EDZ/PycharmProjects/untitled/appium/desired_caps/desired_caps.yaml') as file: self.data = yaml.load(file) def appium_start(self): cmd = 'start /b appium -a ' + str(self.data['host']) + ' -p ' + str(self.data['port']) + ' -U ' + self.data['deviceName'] print('%s at %s' % (cmd, ctime())) subprocess.Popen(cmd, shell=True, stdout=open('C://Users/EDZ/PycharmProjects/untitled/appium_SH/desired_caps/appium_log/'+str(self.data['port']) + '.log', 'a'), stderr=subprocess.STDOUT) def desired_caps(self): desired_caps = {} desired_caps["platformName"] = self.data["platformName"] desired_caps["deviceName"] = self.data["deviceName"] desired_caps["platformVersion"] = self.data["platformVersion"] desired_caps["appPackage"] = self.data["appPackage"] desired_caps["appActivity"] = self.data["appActivity"] desired_caps["noReset"] = self.data["noReset"] desired_caps["unicodeKeyboard"] = self.data["unicodeKeyboard"] desired_caps["resetKeyboard"] = self.data["resetKeyboard"] driver = webdriver.Remote("http://" + str(self.data["host"]) + ":" + str(self.data["port"]) + "/wd/hub", desired_caps) driver.implicitly_wait(8) return driver
更多相关文章
- Android实现学生管理系统
- Android(安卓)Studio的快捷键-官方文档
- android ndk之常用命令
- 给点击的布局加上背景
- Android应用程序全屏显示
- android中退出整个app应用程序
- Android(安卓)深入研究SQLite实例(六)
- ant android
- android通过google API获取天气信息