python如何解析配置文件并应用到项目中
配置文件的类型
通常自动化测试中的配置文件是以.ini 和 .conf 为后缀的文件
配置文件的组成
1.section
2.option
3.value
配置文件的格式
[section_name]# =号可以使用:号代替option_name=value
配置文件的注释
通常使用#号或者;分号注释,有一点一定要注意,注释最好不要写到option_name=value行的后面,否则你会遇到意想不到的错误
配置文件的作用
那么我们的配置文件主要来干些什么呢?
1.可以存储测试中测试用例使用的测试数据
2.可以存储测试中用到的资源数据,比如数据库的地址,用户,密码等等
3.可以作为ui对象库使用,存储我们ui自动化测试项目中的页面元素信息
4.可以存储项目使用的全局变量,比如项目的根目录,日志,报告的路径等等
以上这些数据均可以存放在配置文件中,方便的我们读取,当项目的一些配置信息改变时,我们只要修改配置文件即可,而不用修改具体代码,大大减小项目的维护成本!
ok,既然我都标题党了,那么现在就告诉你怎么1分钟应用到项目中。有配置文件我们必定要先解析文件才行,我们现在有这样一个配置文件,存放如下内容
[126mail_login]loginPage.frame=xpath>//div[@id='loginDiv']/iframeloginPage.username=xpath>//input[@name='email']loginPage.password=xpath>//input[@name='password']loginPage.loginBtn=xpath>//a[@id='dologin'][126mail_homePage]homePage.addressbook=id>_mail_tabitem_1_4text[126mail_addContactPage]addContactPage.newContact=xpath>//span[text()='新建联系人']addContactPage.newName=id>input_NaddContactPage.newMail=xpath>//div[@id='iaddress_MAIL_wrap']//input[@class='nui-ipt-input']addContactPage.newMark=xpath>//span[@class='nui-chk-text']/preceding-sibling::span/baddContactPage.newPhone=xpath>//div[@id='iaddress_TEL_wrap']//input[@class='nui-ipt-input']addContactPage.newComment=id>input_DETAILaddContactPage.newCommit=xpath>//span[text()='确 定']
封装代码
下面这个封装是我之前写的,不算是很通用的功能,但是如果你的配置文件和我上面的一样用来存储ui对象库的话就完全适用了。
"""------------------------------------@Time : 2019/5/16 10:56@Auth : linux超@File : ParseConfigOld.py@IDE : PyCharm@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!------------------------------------"""from configparser import (ConfigParser,NoSectionError,NoOptionError)filename = 'configfile.ini'class ParseConfigFile(object):"""解析ini配置文件"""def __init__(self):try:self.cf = ConfigParser() # 获取配置文件对象self.cf.read(filename, encoding='utf-8') # 加载配置文件到内存中except Exception as e:raise edef getItemsSection(self, section):"""获取section下面所有section的键值:param sectionName::return:"""try:value = dict(self.cf.items(section))except (NoSectionError, KeyError):print('{} not exit'.format(section))except Exception as e:raise eelse:return valuedef getElementValue(self, section, option):"""根据自己的需要修改这部分代码"""try:# 我配置文件是用这个符号来分割的,所有解析的时候要使用这个符号分割,得到元素locator = self.cf.get(section, option).split('>')except (NoSectionError, NoOptionError, KeyError):print('section:{} or option:{} not exit'.format(section, option))except Exception as e:raise eelse:return locator # 获取option键对应的valuedef getAllSections(self):try:all_sections = self.cf.sections()except Exception as e:raise eelse:return all_sections # 所有的sections返回值是个列表if __name__=='__main__':cf = ParseConfigFile()locator = cf.getElementValue('126mail_login','loginPage.username')print(locator)print(cf.getItemsSection('126mail_login'))print(cf.getAllSections())
封装改进
下面的封装几乎可以完成任何自动化测试项目中配置文件存储任何数据类型的数据解析
"""------------------------------------@Time : 2019/5/16 9:11@Auth : linux超@File : ParseConfigFile.py@IDE : PyCharm@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!------------------------------------"""from configparser import (ConfigParser,NoOptionError,NoSectionError)class ParseConfigFile(ConfigParser):def __init__(self, filename):super().__init__()try:self.filename = filenameself.read(filename, encoding='utf-8')except Exception as e:raise edef get_all_option(self, section='DEFAULT'):"""获取指定section下所有的option"""try:options = self.options(section)return optionsexcept NoSectionError:print('NoSectionError : {} not exist'.format(section))except Exception as e:raise edef get_value(self, section='DEFAULT'):"""获取指定section中所有的option和value,返回一个字典"""try:value = dict(self.items(section))return valueexcept (NoSectionError, KeyError):print('{} not exist'.format(section))except Exception as e:raise edef get_option_value(self, section, option, flag=False):"""获取指定section和option对应的数据如果option对应数据为数字,则自动转换为int或者float如果option对应的数据是个可以使用eval转换的类型,则传递flag为True时,自动转换,否则输出str"""try:value = self.get(section, option)if value.isdigit():return int(value)try:return float(value)except Exception:passif isinstance(flag, bool) and flag:return eval(value)return valueexcept (NoSectionError, NoOptionError, KeyError):print('no option "{}" or section "{}"'.format(option, section))except Exception as e:raise edef __call__(self, section='DEFAULT', option=None, flag_eval=False, flag_bool=False):"""对象当成函数使用的时候会默认调用这个方法这个方法可以实现上面多数功能:param section::param option::param flag_eval: 如果为True 我们使用eval转换类型:param flag_bool: 如果为True 我们使用把数据转换为bool:return:"""if option is None:return dict(self[section])if isinstance(flag_bool, bool):if flag_bool:return self.getboolean(section, option)else:raise ValueError('{} must be type bool'.format(flag_bool))data = self.get(section, option)if data.isdigit():return int(data)try:return float(data)except Exception:passif isinstance(flag_eval, bool):if flag_eval:return eval(data)else:raise ValueError('{} must be type bool'.format(flag_eval))return dataif __name__ == '__main__':conf = ParseConfigFile('configfile.ini')print('所有的option', conf.get_all_option('FilePath'))print('获取section:{},option:{}对应的数据:{}'.format('FilePath', 'TestCase', conf.get_option_value('FilePath', 'TestCase')))print('获取section:{}下所有的键值{}'.format('FilePath', conf.get_value('ExcelNum')))print(conf())print(conf(section='FilePath', option='TestCase'))print(conf(option='a', flag_bool=True))print(conf(section='ExcelNum', option='Actual_Column_Num', flag_eval=True))
1分钟应用到项目中
啥? 你还不知道怎么一分钟应用到项目中?
好吧,看来是逃不过去了。 我要说了..... 首先复制代码,当然你已经知道上述代码的含义, 在你的项目中新建py文件,拷贝代码到你的文件中,ok接下来你可能已经知道怎么用了。这能有1分钟吗?
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。