python3+selenium框架设计05-配置文件和浏览器引擎类

python3配置文件的增删改查等操作可以使用内置的ConfigParser模块,可以自行百度学习,也可以看 Python3学习笔记27-ConfigParser模块

配置文件一般存放着环境信息,比如url和一些测试过程中后续需要用到的字段。还有测试中需要导入,导出的一些文件。在项目下新建Config文件夹,文件夹下新建config.ini文件。项目结构如下

实际项目测试中,比如测试环境和生产的地址的切换,用户信息这些都是放在配置文件中,所以会频繁的使用对配置文件进行操作的方法,那我们就需要对配置文件的增删改查进行封装

先看下config.ini文件

因为ConfigParser模块在写入时会把注释给清空掉。所以如果要实现配置文件去控制想要测试哪个环境信息,只能配置一个开关。没办法通过去掉注释这种方法。

environment是配置一些开关,比如browser用来配置浏览器,可以设定0是谷歌浏览器,1是火狐浏览器。switch是环境开关,0是测试环境,1是生产环境

test放一些关于测试环境用到的信息

prod放一些关于生产环境用到的信息

接下来进行具体的实现。在Common文件夹下,对Base_Page.py文件添加全局变量

path = getcwd.get_cwd()
config_path = os.path.join(path, 'Config/config.ini')
config = configparser.ConfigParser()
config.read(config_path,encoding="utf-8-sig")

上面代码是为了提供配置文件的路径,接下来是封装配置的文件的增删改查

    def config_get(self,key,section=None):
        '''读取配置文件字段的值并返回'''
        switch = config.get('environment', 'switch')
        if section==None and switch == str(0):
            section = 'test'
        elif section==None and switch == str(1):
            section = 'prod'
        config_get = config.get(section,key)
        return  config_get

这是将读取配置文件的方法进行封装。代码中key是配置文件中section某个键,section是具体哪个部分的配置文件,默认是None。如果需要读的信息不是test或者prod下,才需要进行传入。来看下测试代码

from framework.Base_Page import BasePage

s = BasePage(driver=1)
bro =s.config_get('browser','environment')
print('browser是:%s' %bro)

url = s.config_get('url')
print('url是:%s' % url)

可以看到第一个如果要读取不是test或prod下的配置内容,需要把section和key都传进去,注意顺序先key再section。第二个因为switch是1,所以读的是prod下的,也就是生产环境的url。

接下来看写入配置文件

   def config_write(self,key = None,value = None,section = None):
        '''往配置文件写入键值'''
        switch = config.get('environment', 'switch')
        if section == None and switch == str(0):
            section = 'test'
        elif section == None and switch == str(1):
            section = 'prod'
        if key is not None and value is not None:
            config.set(section,key,value)
            log1.info('在section:%s下写入%s=%s' %(section,key,value))
            with open(config_path,'w',encoding='utf-8')as f :
                config.write(f)
        else:
            config.add_section(section)
            log1.info('新增section:%s' % section)
            with open(config_path,'w',encoding='utf-8')as f :
                config.write(f)

和上面读取的思路差不多懒得解释了。来看测试代码。

s.config_write('用户名','username','environment')
s.config_write('yonghuming','username')
s.config_write(section='yonghu')

可以看到新增成功了。

再看下删除

 def config_delete(self,key = None,section = None):
        '''删除配置文件字段'''
        switch = config.get('environment', 'switch')
        if section == None and switch == str(0):
            section = 'test'
        elif section == None and switch == str(1):
            section = 'prod'
        if key is not None :
            config.remove_option(section,key)
            log1.info('删除section:%s下key为:%s的记录' % (section,key))
            with open(config_path,'w',encoding='utf-8')as f :
                config.write(f)
        else:
            config.remove_section(section)
            log1.info('删除section:%s' % section)
            with open(config_path,'w',encoding='utf-8')as f :
                config.write(f)

和读取也是差不多的思路,看下测试代码

s.config_delete('用户名','environment')
s.config_delete('yonghuming')
s.config_delete(section='yonghu')

在对配置文件操作封装文成之后,封装浏览器引擎类,根据配置文件的不对,启动不同的浏览器。

def open_browser(self):
        browser = self.config_get('browser','environment')
        log1.info('读取浏览器配置')
        url = self.config_get('url')
        log1.info('读取url:%s' % url)
        try:
            if browser == str(0) :
                self.driver = webdriver.Chrome()
                log1.info('打开的浏览器为chrome')
            elif browser == str(1) :
                self.driver = webdriver.Firefox()
                log1.info('打开的浏览器为chrome')
            self.driver.get(url)
            self.driver.maximize_window()
            log1.info('浏览器最大化')
            self.driver.implicitly_wait(10)
            log1.info('设置静态等待时间10秒')
            return self.driver
        except BaseException:
            log1.error('浏览器打开报错')

看下测试代码

from framework.Base_Page import BasePage
import unittest
class baidu(unittest.TestCase):

    def test_nos(self):
        s = BasePage(self)
        self.driver = s.open_browser()

if __name__ == '__main__':
    unittest.main()

我这边执行成功

原文地址:https://www.cnblogs.com/myal/p/9415209.html