關(guān)鍵字驅(qū)動(dòng)框架:將每一條測(cè)試用例分成四個(gè)不同的部分
- 測(cè)試步驟(Test Step):一個(gè)測(cè)試步驟的描述或者是測(cè)試對(duì)象的一個(gè)操作說(shuō)明
- 測(cè)試步驟中的對(duì)象(Test Object):指頁(yè)面的對(duì)象或者元素
- 對(duì)象執(zhí)行的動(dòng)作(Action):頁(yè)面操作的動(dòng)作
- 執(zhí)行對(duì)象所需要的數(shù)據(jù)(Test Data):任何對(duì)象操作時(shí)所需要的值
例如:登錄163郵箱,步驟分為:
- 打開(kāi)瀏覽器
- 輸入url
- 切換iframe
- 輸入用戶名
- 輸入密碼
- 點(diǎn)擊登錄
1:創(chuàng)建excel文件,使用excel文件來(lái)存放測(cè)試用例及測(cè)試步驟,excel內(nèi)容如下:
login的sheet頁(yè)中,描述了測(cè)試步驟,測(cè)試步驟中的對(duì)象可以分為:測(cè)試對(duì)象的定位方式以及定位方式表達(dá)值:
從excel文件中,可以看到,每一個(gè)步驟我們要執(zhí)行的動(dòng)作是什么,例如打開(kāi)瀏覽器,我們需要定義一個(gè)open_browse方法,再讀取excel文件的內(nèi)容時(shí),程序才知道要怎么做。因此我們需要為每個(gè)執(zhí)行動(dòng)作定義一個(gè)方法。
再在excel中增加一個(gè)sheet頁(yè),從來(lái)存放測(cè)試用例,如index:
2:搭建項(xiàng)目框架,只是簡(jiǎn)單的實(shí)現(xiàn)關(guān)鍵字驅(qū)動(dòng),需要的其他內(nèi)容可以再往框架中加。
項(xiàng)目結(jié)構(gòu)目錄:
3:接下來(lái)我們來(lái)看一下各個(gè)文件夾下py文件的內(nèi)容
首先是Util文件夾下,封裝的查找元素控件的工具類(lèi)find_ele.py文件
# find_ele.py
from selenium.webdriver.support.wait import WebDriverWait
def find_element(driver, location_type, location_express):
'''查找控件元素'''
try:
driver = WebDriverWait(driver, 20).until(lambda driver:driver.find_element(location_type, location_express))
return driver
except Exception as e:
raise e
接下來(lái)我們就要在Util文件下,封裝讀取excel文件內(nèi)容方法的excel_parse.py文件,再封裝該方法前,需要在Setting文件下,創(chuàng)建Config.py文件,用來(lái)獲取文件路徑。
Config.py文件內(nèi)容如下:
# Config.py
import os
Base_Dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 測(cè)試數(shù)據(jù)文件
Test_Data_Path = os.path.join(Base_Dir, 'TestData')
excel_parse.py文件內(nèi)容如下:
# excel_parse.py
from Setting.Config import Test_Data_Path
from openpyxl import load_workbook
class ExcelParse:
def __init__(self):
self.workbook = None
self.sheet = None
def load_workbook(self, filename):
'''加載文件'''
try:
self.workbook = load_workbook(filename)
except Exception as e:
raise e
def get_sheet(self, sheetname):
'''獲取sheet頁(yè)'''
try:
self.sheet = self.workbook[sheetname]
except Exception as e:
raise e
def get_row_num(self):
'''返回行數(shù)'''
return self.sheet.max_row
def get_col_num(self):
'''返回列數(shù)'''
return self.sheet.max_column
def get_cell_value(self, row, col):
'''返回某一單元格的值'''
return self.sheet.cell(row=row, column=col).value
def 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 data
except Exception as e:
raise e
def 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 e
if __name__ == '__main__':
execl = ExcelParse()
execl.load_workbook(Test_Data_Path + '/login.xlsx')
execl.get_sheet('login')
res = execl.get_row_value(2)
print(res)
然后就需要定義測(cè)試步驟中的執(zhí)行動(dòng)作的方法,在Util文件夾下,創(chuàng)建elementAction.py文件,內(nèi)容如下:
# elementAction.py
from selenium import webdriver
from Util.find_ele import find_element
driver = None
def open_browse(browser_name, *args):
'''打開(kāi)瀏覽器'''
global driver
try:
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 e
def get_url(url, *args):
'''打開(kāi)網(wǎng)址'''
try:
driver.get(url)
except Exception as e:
raise e
def max_window(*args):
'''窗口最大化'''
try:
driver.maximize_window()
except Exception as e:
raise e
def 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 e
def input_content(location_type, location_express, content, *args):
'''定位輸入框,輸入內(nèi)容'''
try:
find_element(driver, location_type, location_express).send_keys(content)
except Exception as e:
raise e
def click(location_type, location_express, *args):
'''點(diǎn)擊操作'''
try:
find_element(driver, location_type, location_express).click()
except Exception as e:
raise e
def assert_title(title, *args):
'''斷言title是否正確'''
try:
assert title in driver.title
except Exception as e:
raise e
def close_browse():
'''關(guān)閉瀏覽器'''
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('網(wǎng)易')
然后將從excel文件中讀取出來(lái)的內(nèi)容,拼接成要執(zhí)行的方法。在Util文件夾下,創(chuàng)建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:
# 判斷操作值的類(lèi)型
if isinstance(operate_data, int):
method_express = key_word + '(' + str(operate_data) + ')'
else:
method_express = key_word + "('" + operate_data + "')"
# print(method_express)
# 只有關(guān)鍵字有值,其他的都為空,比如:max_window, close_browse
elif 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
之后,就是編寫(xiě)執(zhí)行測(cè)試用例了。
在TestScript文件夾下,創(chuàng)建test_login.py文件文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-689024.html
# 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_express
excel = ExcelParse()
# 加載login.xlsx文件
excel.load_workbook(Test_Data_Path + '/login.xlsx')
def test_run():
try:
# 獲取indexsheet頁(yè)
excel.get_sheet('index')
# 獲取index的行數(shù)
rows = excel.get_row_num()
for i in range(2, rows+1):
# 判斷是否要執(zhí)行
is_run = excel.get_cell_value(i, 4).lower()
if is_run == 'y':
# 獲取要執(zhí)行的sheet頁(yè)名稱(chēng)
case_step_sheet = excel.get_cell_value(i, 3)
# 切換到要執(zhí)行的sheet頁(yè)
excel.get_sheet(case_step_sheet)
# 獲取要執(zhí)行的步驟數(shù)
step_num = excel.get_row_num()
# print(step_num)
success_num = 0 # 記錄成功的步驟數(shù)
for j in range(2, step_num+1):
# 獲取步驟描述
step_desc = excel.get_cell_value(j, 1)
# 定位方式
location_type = excel.get_cell_value(j, 2)
# 定位方式表達(dá)值
location_express = excel.get_cell_value(j, 3)
# 執(zhí)行動(dòng)作
key_word = excel.get_cell_value(j, 4)
# 執(zhí)行數(shù)據(jù)
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:
# 運(yùn)行函數(shù),eval(),將字符串str當(dāng)成有效的表達(dá)式來(lái)求值并返回計(jì)算結(jié)果
eval(method_express)
except Exception as e:
raise e
else:
success_num += 1
# 切換sheet頁(yè)到index
excel.get_sheet('index')
if success_num == step_num - 1:
# 成功的步驟數(shù)等于步驟sheet頁(yè)的行數(shù)減1,表示測(cè)試執(zhí)行成功,寫(xiě)入成功
excel.write_cell(i, 5, Test_Data_Path + '/login.xlsx', 'pass')
else:
# 寫(xiě)入失敗
excel.write_cell(i, 5, Test_Data_Path + '/login.xlsx', 'Fall')
except Exception as e:
raise
執(zhí)行test_login.py文件,關(guān)鍵字驅(qū)動(dòng)就實(shí)現(xiàn)了。這只是簡(jiǎn)單的實(shí)現(xiàn)了關(guān)鍵字驅(qū)動(dòng),了解了關(guān)鍵字驅(qū)動(dòng)應(yīng)該是什么樣的,日志、測(cè)試報(bào)告、執(zhí)行入口可以自己再添加,上面的代碼也可以再進(jìn)行優(yōu)化。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-689024.html
到了這里,關(guān)于UI自動(dòng)化之關(guān)鍵字驅(qū)動(dòng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!