关键字驱动框架:将每一条测试用例分成四个不同的部分
- 测试步骤(Test Step):一个测试步骤的描述或者是测试对象的一个操作说明
- 测试步骤中的对象(Test Object):指页面的对象或者元素
- 对象执行的动作(Action):页面操作的动作
- 执行对象所需要的数据(Test Data):任何对象操作时所需要的值
例如:登录163邮箱,步骤分为:
- 打开浏览器
- 输入url
- 切换iframe
- 输入用户名
- 输入密码
- 点击登录
1:创建excel文件,使用excel文件来存放测试用例及测试步骤,excel内容如下:
login的sheet页中,描述了测试步骤,测试步骤中的对象可以分为:测试对象的定位方式以及定位方式表达值:
从excel文件中,可以看到,每一个步骤我们要执行的动作是什么,例如打开浏览器,我们需要定义一个open_browse方法,再读取excel文件的内容时,程序才知道要怎么做。因此我们需要为每个执行动作定义一个方法。
再在excel中增加一个sheet页,从来存放测试用例,如index:
2:搭建项目框架,只是简单的实现关键字驱动,需要的其他内容可以再往框架中加。
项目结构目录:
3:接下来我们来看一下各个文件夹下py文件的内容
首先是Util文件夹下,封装的查找元素控件的工具类find_ele.py文件
# find_ele.py
from selenium.webdriver.support.wait import WebDriverWaitdef find_element(driver, location_type, location_express):'''查找控件元素'''try:driver = WebDriverWait(driver, 20).until(lambda driver:driver.find_element(location_type, location_express))return driverexcept Exception as e:raise e
接下来我们就要在Util文件下,封装读取excel文件内容方法的excel_parse.py文件,再封装该方法前,需要在Setting文件下,创建Config.py文件,用来获取文件路径。
Config.py文件内容如下:
# Config.py
import osBase_Dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# 测试数据文件
Test_Data_Path = os.path.join(Base_Dir, 'TestData')
excel_parse.py文件内容如下:
# excel_parse.py
from Setting.Config import Test_Data_Path
from openpyxl import load_workbookclass ExcelParse:def __init__(self):self.workbook = Noneself.sheet = Nonedef load_workbook(self, filename):'''加载文件'''try:self.workbook = load_workbook(filename)except Exception as e:raise edef get_sheet(self, sheetname):'''获取sheet页'''try:self.sheet = self.workbook[sheetname]except Exception as e:raise edef get_row_num(self):'''返回行数'''return self.sheet.max_rowdef get_col_num(self):'''返回列数'''return self.sheet.max_columndef get_cell_value(self, row, col):'''返回某一单元格的值'''return self.sheet.cell(row=row, column=col).valuedef get_row_value(self, row):'''返回某一行的值'''try:col = self.get_col_num()data = []for i in range(1, col+1):data.append(self.get_cell_value(row, i))return dataexcept Exception as e:raise edef write_cell(self, row, col, filename, content):'''单元格赋值'''try:self.sheet.cell(row=row, column=col, value=content)self.workbook.save(filename)except Exception as e:raise eif __name__ == '__main__':execl = ExcelParse()execl.load_workbook(Test_Data_Path + '/login.xlsx')execl.get_sheet('login')res = execl.get_row_value(2)print(res)
然后就需要定义测试步骤中的执行动作的方法,在Util文件夹下,创建elementAction.py文件,内容如下:
# elementAction.py
from selenium import webdriver
from Util.find_ele import find_elementdriver = Nonedef open_browse(browser_name, *args):'''打开浏览器'''global drivertry:if browser_name.lower() == 'chrome':driver = webdriver.Chrome()elif browser_name.lower() == 'firefox':driver = webdriver.Firefox()else:driver = webdriver.Ie()except Exception as e:raise edef get_url(url, *args):'''打开网址'''try:driver.get(url)except Exception as e:raise edef max_window(*args):'''窗口最大化'''try:driver.maximize_window()except Exception as e:raise edef switch_frame(location_type, location_express, *args):'''切换iframe'''try:frame = find_element(driver, location_type, location_express)driver.switch_to.frame(frame)except Exception as e:raise edef input_content(location_type, location_express, content, *args):'''定位输入框,输入内容'''try:find_element(driver, location_type, location_express).send_keys(content)except Exception as e:raise edef click(location_type, location_express, *args):'''点击操作'''try:find_element(driver, location_type, location_express).click()except Exception as e:raise edef assert_title(title, *args):'''断言title是否正确'''try:assert title in driver.titleexcept Exception as e:raise edef close_browse():'''关闭浏览器'''driver.quit()if __name__ == '__main__':open_browse('chrome')get_url('http://mail.163.com')max_window()switch_frame('tag name', 'iframe')input_content('name', 'email', 'test123')input_content('name', 'password', 'a123456')click('id', 'dologin')assert_title('网易')
然后将从excel文件中读取出来的内容,拼接成要执行的方法。在Util文件夹下,创建common.py文件。
# common.py
def generate_method_express(location_type, location_express, key_word, operate_data):# location_type, location_express为空,operate_data不为空if key_word and operate_data and location_type is None and location_express is None:# 判断操作值的类型if isinstance(operate_data, int):method_express = key_word + '(' + str(operate_data) + ')'else:method_express = key_word + "('" + operate_data + "')"# print(method_express)# 只有关键字有值,其他的都为空,比如:max_window, close_browseelif key_word and operate_data is None and location_type is None and location_express is None:method_express = key_word + '()'# print(method_express)# location_type,location_express不为空,operate_data为空elif key_word and location_type and location_express and operate_data is None:method_express = key_word + "('" + location_type + "','" + location_express + "')"# print(method_express)# 都不为空else:if isinstance(operate_data, int):method_express = key_word + "('" + location_type + "','" + location_express + "'," + str(operate_data) + ")"else:method_express = key_word + "('" + location_type + "','" + location_express + "','" + operate_data + "')"print(method_express)return method_express
之后,就是编写执行测试用例了。
在TestScript文件夹下,创建test_login.py文件
# test_login.py
from Util.excel_parse import ExcelParse
from Setting.Config import Test_Data_Path
from Util.elementAction import *
from Util.common import generate_method_expressexcel = ExcelParse()
# 加载login.xlsx文件
excel.load_workbook(Test_Data_Path + '/login.xlsx')def test_run():try:# 获取indexsheet页excel.get_sheet('index')# 获取index的行数rows = excel.get_row_num()for i in range(2, rows+1):# 判断是否要执行is_run = excel.get_cell_value(i, 4).lower()if is_run == 'y':# 获取要执行的sheet页名称case_step_sheet = excel.get_cell_value(i, 3)# 切换到要执行的sheet页excel.get_sheet(case_step_sheet)# 获取要执行的步骤数step_num = excel.get_row_num()# print(step_num)success_num = 0 # 记录成功的步骤数for j in range(2, step_num+1):# 获取步骤描述step_desc = excel.get_cell_value(j, 1)# 定位方式location_type = excel.get_cell_value(j, 2)# 定位方式表达值location_express = excel.get_cell_value(j, 3)# 执行动作key_word = excel.get_cell_value(j, 4)# 执行数据operate_data = excel.get_cell_value(j, 5)# print(step_desc, location_type, location_express,key_word, operate_data)method_express = generate_method_express(location_type, location_express, key_word, operate_data)try:# 运行函数,eval(),将字符串str当成有效的表达式来求值并返回计算结果eval(method_express)except Exception as e:raise eelse:success_num += 1# 切换sheet页到indexexcel.get_sheet('index')if success_num == step_num - 1:# 成功的步骤数等于步骤sheet页的行数减1,表示测试执行成功,写入成功excel.write_cell(i, 5, Test_Data_Path + '/login.xlsx', 'pass')else:# 写入失败excel.write_cell(i, 5, Test_Data_Path + '/login.xlsx', 'Fall')except Exception as e:raise
执行test_login.py文件,关键字驱动就实现了。这只是简单的实现了关键字驱动,了解了关键字驱动应该是什么样的,日志、测试报告、执行入口可以自己再添加,上面的代码也可以再进行优化。