UI自动化Selenium 数据驱动读取Excel

selenium 自动化,希望通过Excel进行数据驱动;

即代码自动读取并循环所有数据;

如下为Excel读取封装的函数

# Excel数据读取
def ExcelRead(filename, sheetname):
    # current_path = os.getcwd()
    current_path = "D:\WORK\自动化\pythonselenium"
    path = os.path.join(current_path, 'data')
    file = path + r"\\" + filename
    print(file)
    datafile = xlrd.open_workbook(path + '/' + filename)   #打开Excel
    sheet = datafile.sheet_by_name(sheetname)  #按Sheet页名称打开Sheet
    # 获得列表行数
    rows = sheet.nrows
    # 获得列表列数
    cols = sheet.ncols

    # 将列头存储到list
    listTitle = sheet.row_values(0)

    # 获得某列的索引
    # listTitle.index('tenantName')

    # 将数据存储到list
    i = 1
    listData = []
    while i < rows:
        listData.append(sheet.row_values(i))
        i = i + 1
    return listData

下面为具体调用实现:结合unittest框架和ddt数据驱动框架使用

import unittest
from ddt import ddt, data

@ddt
class Suite_Test(unittest.TestCase):
    @data(*ExcelRead('Test.xls', 'ERP和数见'))  
    def test_应用授权(self, data):
        url, tenantName, user, pwd, system = tuple(data) # 将元组数据读取并按顺序存储到变量中;此处变量和Excel中列头保持一致;
        self.dr = newDriver(url)
        driver = self.dr
        driver.maxWindow()
        # 可直接使用变量
        driver.inputElement2("tenantName", tenantName, By.NAME)
        driver.inputElement2("user", user, By.NAME)
        driver.inputElement2("pwd", pwd, By.NAME)
        driver.elementClick2("submit", By.NAME)

tuple读取后,存储为键值对

你可能感兴趣的:(selenium,自动化,selenium,运维)