使用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: Android platformVersion: 4.4.2 deviceName: 127.0.0.1:62001 appPackage: com.ss.android.article.news appActivity: com.ss.android.article.news.activity.SplashActivity noReset: True unicodeKeyboard: True resetKeyboard: True host: 127.0.0.1 port: 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