python selenium ddt 数据驱动测试(三)

浏览器设置

browser.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-04-15 09:00:00
# @Author  : Canon
# @Link    : https://www.python.org
# @Version : 3.6.1

from selenium import webdriver


def get_driver():
    """ 无界面运行 """
    # 配置浏览器参数
    # options = webdriver.ChromeOptions()
    # options.add_argument('--headless')
    # return webdriver.Chrome(chrome_options=options)
    """ 图形界面运行 """
    return webdriver.Chrome()

读取配置文件

conf_utils.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-04-15 09:00:00
# @Author  : Canon
# @Link    : https://www.python.org
# @Version : 3.6.1

import os
from configparser import ConfigParser

# 项目路径
CUR_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


class Project(object):
    def __init__(self):
        self.conf = ConfigParser()
        # 读取项目的配置文件
        self.conf.read(CUR_PATH + "/data/config/project.conf", encoding='UTF-8')

    def read_gateway(self):
        return CUR_PATH + self.conf.get("conf", "gateway")

    def read_oms(self):
        return CUR_PATH + self.conf.get("conf", "oms")

    def read_log(self):
        """
        读取日志的配置文件
        :return:
        """
        return CUR_PATH + self.conf.get("log", "path")


class Gateway(object):
    def __init__(self):
        self.conf = ConfigParser()
        # 读取支付网关的配置文件
        self.conf.read(Project().read_gateway(), encoding='UTF-8')

    def read_link(self):
        # 读取支付网关登录链接
        return self.conf.get("gateway", "login")

    def read_domain(self, section):
        """
        读取支付域名名称
        :meth section: 支付域名名称 type: str
        :return: 包含元组的列表
        """
        return self.conf.items(section)

    def read_path(self, sec, opt):
        return CUR_PATH + self.conf.get(sec, opt)

    def read_val(self, sec, opt):
        return self.conf.get(sec, opt)


if __name__ == '__main__':
    val = Gateway().read_domain("domain")
    print(val)

读取 excel 测试数据(使用 list_in_dict() 方法)

excel_utils.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-04-15 09:00:00
# @Author  : Canon
# @Link    : https://www.python.org
# @Version : 3.6.1

import xlrd


class ExcelUtils(object):
    def __init__(self, excel_path, sheet_name):
        # 打开 excel 文件
        self.data = xlrd.open_workbook(excel_path)
        # 获取指定的 sheet
        self.sheet = self.data.sheet_by_name(sheet_name)
        # 获取第一行的值
        self.row = self.sheet.row_values(0)
        # 获取第一列的值
        self.col = self.sheet.col_values(0)
        # excel 表的行数
        self.rowNum = self.sheet.nrows
        # excel 表的列数
        self.colNum = self.sheet.ncols
        # 当前行号
        self.curRowNo = 1

    def has_next(self):
        """
        当行数为0或者读取的行数小于行号时, 返回 False
        :return: True or False type: bool
        """
        if self.rowNum == 0 or self.rowNum <= self.curRowNo:
            return False
        else:
            return True

    def list_in_dict(self):
        """
        生成包含字典的列表数据, 第二行数据作为键, 第三行及之后的数据作为值
        :return: data_list type: list
        """
        data_list = []
        row_val = self.sheet.row_values(1)
        self.curRowNo += 1
        while self.has_next():
            data_dict = {}
            col = self.sheet.row_values(self.curRowNo)
            skip = 1
            for x in range(self.colNum):
                if row_val[x] == "Skip" and col[x] == "Yes":
                    skip = 0
                data_dict.setdefault(row_val[x], col[x])
            if skip == 1:
                data_list.append(data_dict)
            self.curRowNo += 1
        return data_list


if __name__ == '__main__':
    value = ExcelUtils("../data/testdata/gateway/ThreePay.xlsx", "PayPage").list_in_dict()
    print(value)

转载于:https://www.jianshu.com/p/0f86789ed6c6

猜你喜欢

转载自blog.csdn.net/weixin_34129696/article/details/91057444