python3+selenium(十八)读取ini文件

命题

编写ini配置文件,以便提升程序复用性及易操作性

思路

  1. 编写ini文件
  2. 编写py文件,读取配置文件

知识点

  1. 读取配置文件模块:configparser
  2. 获取文件相对路径:root_dir=os.path.abspath(os.path.dirname(’,’))

源码

  1. 同一工程下新建目录结构如下:
    在这里插入图片描述

  2. config目录下新建config.ini文件如下:
    #this is config file,only store browser type and server URL
    [browserType]
    #browserName=Firefox
    browserName=Chrome
    #browserName=IE
    [testServer]
    URL=https://www.baidu.com
    #URL=http://www.google.com

  3. pro2包下新建readcon.py文件,对config.ini进行读取

#coding=utf-8
import configparser
import os

class TestReadConfigFile(object):

    def get_value(self):
        root_dir=os.path.abspath(os.path.dirname(','))#获取相对路径
        print(root_dir)
        config=configparser.ConfigParser()

        file_path=os.path.dirname(os.path.abspath('.'))+'/config/config.ini'
        config.read(file_path)
        browser=config.get("browserType","browserName")
        url=config.get("testServer","URL")

        return(browser,url)
trcf=TestReadConfigFile()
print(trcf.get_value())

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Amy8020/article/details/88971186