有两种方式,一个是file open,一个是with open
方式1
file = open('../config/WiFibanlv_caps.yaml', 'r') data = yaml.load(file) file.close() #必须使用close,否则文件会出现占用情况
方式2:
with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file: data = yaml.load(file)
安装包的相对路径使用方法:
步骤1:先导入对应的模块import os
步骤2:找到当前文件的目录,使用方法os.path.driname(__file__)
步骤3:根据当前文件目录,找到上一级目录,以此类推
步骤4:拼接安装包所在的路径
if __name__ == '__main__': with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file: data = yaml.load(file) base_dir=os.path.dirname(os.path.dirname(__file__))#os.path.dirname(__file__)表示获取当前文件的路径 print(os.path.dirname(__file__)) print(base_dir) app_path=os.path.join(base_dir, 'app',data['appname']) #join是表示把几个路径拼接起来, #将base_dir和app文件夹拼接起来,然后找到app文件夹下面的的apk文件名称 print(app_path)
定义yaml配置文件
定义log、conf文件
封装capability启动app的脚本desired_caps.py
#!urs/bin/python #!_*_ coding:UTF-8 _*_ from appium import webdriver import yaml import logging import logging.config import os #日志配置文件 CON_LOG='../config/log.conf' logging.config.fileConfig(CON_LOG) logging=logging.getLogger() def appium_desired(): #读取配置文件的数据,有两种方式 #方式1,必须以close结尾 file = open('../config/WiFibanlv_caps.yaml', 'r') data = yaml.load(file) file.close() #必须使用close,否则文件会出现占用情况 #方式2 with open('../config/WiFibanlv_caps.yaml', 'r', encoding='utf-8') as file: data = yaml.load(file) logging.info("Initialize APP...") desired_caps = {} desired_caps['platformName'] = data['platformName'] desired_caps['platformVersion'] = data['platformVersion'] # 第一个模拟器默认127.0.0.1:62001 第二个默认:127.0.0.1:62025 desired_caps['deviceName'] = data['deviceName'] #定义apk安装包的相对路径 base_dir = os.path.dirname(os.path.dirname(__file__)) # os.path.dirname(__file__)表示获取当前文件的路径 app_path = os.path.join(base_dir, 'app', data['appname']) desired_caps['app'] = app_path desired_caps['packageName'] = data['packageName'] desired_caps['appActivity'] = data['appActivity'] desired_caps['noReset'] = data['noReset'] desired_caps['unicodekeyboard'] = data['unicodekeyboard'] desired_caps['resetkeyboard'] = data['resetkeyboard'] desired_caps['uiautomationName'] = data['uiautomationName'] logging.info("Start APP...") driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', desired_caps) driver.implicitly_wait(8) return driver #调试当前脚本方法 if __name__ == '__main__': appium_desired()
每封装一个模块,都要引用__mian__检测下当前脚本是否能运行成功,capability封装后运行成功结果如下: