Selenium40-配置文件

配置文件

  • 配置文件是记录可能会有改动的配置项目的文件
  • 提取配置文件的目的是为了使代码更加灵活,对可能经常变动的地方修改更加方便,所以使用配置文件读取
  • 配置文件的应用场景:邮件配置信息、服务器地址信息

常用的配置文件格式

conf配置文件语法

  • 括号[]内为section,紧接着是以key-value的形式的options的配置内容
  • # 作为注释
  • [] 里是区域名或节(section)
  • 等号左侧为选项名(options_name)
  • 等号右侧为选项值(options_value)

准备配置文件

  • conf包里新建mail.conf
#基础服务器
[mail]
#设置登录及服务器信息
mail_host = smtp.qq.com
mail_port = 465
mail_username = 2269……311@qq.com
mail_password = gux……e
receivers = 2269……311@qq.com

configparser模块

  • configparser模块是python自带的模块,可以用于读取conf格式的配置文件内容。

  • configparser模块使用步骤:

    • 导入configparser模块的ConfigParser类
      • import configparser
    • 初始化ConfigParser对象实例
      • 实例=configparser.ConfigParser()
      • 示例:conf = configparser.ConfigParser()
    • 读取特定配置文件里的信息
      • read方法
    • 调用Get方法读取文件里指定节下的选项名对应的值
      • get方法

read方法

  • read方法:读取配置文件
  • 可选参数:encoding代表字符集编码格式
  • 示例:conf.read('../config/mail.conf',encoding='utf-8')

get方法

  • get方法:获取指点section下指点option的值。
  • 语法:实例.get(“section name”,”opiton name”)
  • 返回值:option value
  • 示例:mail_host = conf.get("mail",'mail_host')

测试读取配置文件功能

自定义读取邮箱配置文件模块

  • read_mailconf.py里MailConf类:定义多个属性,存储配置文件里的各项信息

获得邮箱配置各项信息

  • 获得邮箱配置信息的各种方法

调试读取邮箱配置文件模块

  • 实现调试代码,运行查看结果

使用邮箱配置文件

  • 导入 与 读取

扩展需求

  • 需求: 服务器的地址和账号信息都有可能变化,包括IP、端口、各个页面的访问地址、账号用户名、密码等信息。
  • 扩展:将服务器地址和账号等信息写入一个配置文件进行管理
  • server.conf

被测系统服务器配置文件

  • conf里的server.conf

自定义读取服务器配置文件模块

  • read_serverconf.py里ServerConf类:定义多个属性,存储配置文件里的各项信息

获得服务器绝对路径URL

  • read_serverconf.py里ServerConf类:封装获得绝对路径URL的方法

修改pageobject里的URL

  • pageobject包里的loginpage.py
from utils.read_serverconf import ServerConf
class LoginPage(PageBase):
    def __init__(self,driver):
        self.driver=driver
        self.url = ServerConf().get_fp_login_page_url()
    # 打开前台登录页
    def open(self):
        self.driver.get(self.url)

  • pageobject包里的messagepage.py
from utils.read_serverconf import ServerConf
class MessagePage(PageBase):
    def __init__(self,driver):
        self.driver=driver
        self.url=ServerConf().get_fp_message_page_url()
    # 打开前台留言板页
    def open(self):
        self.driver.get(self.url)
posted @ 2022-12-29 22:46  暄总-tester  阅读(101)  评论(0)    收藏  举报