一、如下是用例格式,第一个sheet为用例汇总,后面的sheet为具体的用例步骤
sheet:测试用例
sheet:搜索
sheet:刘江博客验证
二、读取该excel文件取出关键字等信息,作为关键字的参数,通过反射机制传递给关键字方法去执行。
关键字模块如下:ObjectMap.py
# coding:utf-8
from selenium.webdriver.support.ui import WebDriverWait # 获取单个页面元素对象
def get_element(driver, locationType, locatorExpression):
try:
element = WebDriverWait(driver, 3).until(lambda x:x.find_element(by=locationType,value = locatorExpression))
return element
except Exception, e:
raise e # 获取一组元素
def get_elements(driver , locationType, locatorExpression):
try:
elements = WebDriverWait(driver, 3).until(lambda x:x.find_elements(by=locationType,value=locatorExpression))
return elements
except Exception, e:
raise e # 由于关键字函数的参数个数不一样,所以通过传递动态参数*args实现传参,关键字方法
# 最多需要(driver , locationType, locatorExpression, operationValue)四个参数
def open_browser(driver, *args):
driver.get(args[2]) def input_string(driver, *args):
try:
WebDriverWait(driver, 3).until(lambda x: x.find_element(by=args[0], value=args[1])).send_keys(args[2])
except Exception:
raise def click(driver, *args):
WebDriverWait(driver, 3).until(lambda x: x.find_element(by=args[0], value=args[1])).click() def assert_equal(driver, *args):
try:
assert args[2] in driver.title
except AssertionError, e:
raise AssertionError(e)
except Exception, e:
raise e
三、测试用例执行模块
# coding:utf-8
from util import ObjectMap, ExcelUtil
import xlrd, xlwt
import time
from xlutils.copy import copy def run_test():
from selenium import webdriver
start_time = time.time()
# 读取excel中的关键字的值,定位方式的值,定位表达式,和操作值等参数值。然后将参数值传到对应关键字方法中
excelFile = xlrd.open_workbook(r"D:\KeyWordsFrameWork\testScripts\search.xlsx", formatting_info=True)
sheet0 = excelFile.sheet_by_index(0) # 获取第一个sheet页
# 循环第一个sheet页,其中每一行代表一条用例
for row0 in range(1, sheet0.nrows):
# 一定要在这个for循环里面再写一次excelFile,要不然执行多条用例时只会在最后一条用例写入结果
excelFile = xlrd.open_workbook(r"D:\KeyWordsFrameWork\testScripts\search.xlsx", formatting_info=True)
driver = webdriver.Chrome()
case_name = sheet0.row_values(row0)[3] # 获取用例对应的sheet页
sheet = excelFile.sheet_by_name(case_name) # 从第一个sheet中获取对应的用例名称找到对应的用例sheet
maxRows = sheet.nrows # 获取用例的最大行数,也就是步骤数
successfulSteps = 0 # 定义执行成功的步骤数
retDict = {} # 存放用例结果的字典 if sheet0.row_values(row0)[4] == 'y': # 只执行标记为y的用例
# print case_name
for row in range(1, maxRows): # 循环用例步骤
keyword = sheet.row_values(row)[2] # 读取关键字列
# print keyword
locationType = sheet.row_values(row)[3] # 获取定位方式
if locationType not in ['id','xpath','name']:
pass
locatorExpression = sheet.row_values(row)[4] # 获取定位表达式的值
operationValue = sheet.row_values(row)[5] # 获取操作值
# dir(ObjectMap)获取该模块的所有方法和变量
# print dir(ObjectMap)
# 遍历ObjectMap中的方法和属性,如果有名称一致的则通过反射调用对应方法执行关键字
for i in dir(ObjectMap):
if keyword == i:
# print i
# 要用到反射机制,通过函数名字符串调用对应方法:http://www.liujiangblog.com/course/python/48
if hasattr(ObjectMap, keyword):
# print '有这个方法'
func = getattr(ObjectMap, keyword)
try:
func(driver, locationType, locatorExpression, operationValue)
print '%s:execute OK' % keyword
# 执行每个关键字成功后,成功步数+1
successfulSteps += 1
except Exception:
print '%s:excute ERROR' % keyword
finally:
# 如果成功的步骤数等于用例的步骤数,则整条用例执行成功,写入执行结果
if successfulSteps == sheet.nrows - 1:
retDict['result'] = 'PASS'
print retDict['result']
end_time = time.time()
take_time = end_time - start_time
print take_time
# 向已经存在的excel中写入时,需要先copy,copy后sheet页获取sheet方法为get_sheet,与
# 原来的获取方法不一样
excleFileCopy = copy(excelFile)
case_sheet = excleFileCopy.get_sheet(0)
case_sheet.write(row0, 5, take_time)
case_sheet.write(row0, 6, retDict['result'])
# 写入后需要save
excleFileCopy.save(r"D:\KeyWordsFrameWork\testScripts\search.xlsx")
print 'pass write is ok'
else:
retDict['result'] = 'FAIL'
print retDict['result']
end_time = time.time()
take_time = end_time - start_time
print take_time
excleFileCopy = copy(excelFile)
case_sheet = excleFileCopy.get_sheet(0)
case_sheet.write(row0, 5, take_time)
case_sheet.write(row0, 6, retDict['result'])
excleFileCopy.save(r"D:\KeyWordsFrameWork\testScripts\search.xlsx")
print 'fail write is ok'
else:
raise Exception
driver.quit() if __name__ == '__main__':
run_test()