python webdriver 测试框架-数据驱动json文件驱动的方式
数据驱动json文件的方式
test_data_list.json:
[
"邓肯||蒂姆",
"乔丹||迈克尔",
"库里||斯蒂芬",
"杜兰特||凯文",
"詹姆斯||勒布朗"
]
ReportTemplate.py:
#encoding=utf-8
def htmlTemplate(trData):
htmlStr = u'''<!DOCTYPE HTML>
<html>
<head>
<title>单元测试报告</title>
<style>
body {
width: 80%; /*整个body区域占浏览器的宽度百分比*/
margin: 40px auto; /*整个body区域相对浏览器窗口摆放位置(左右,上下)*/
font-weight: bold; /*整个body区域的字体加粗*/
font-family: 'trebuchet MS', 'Lucida sans', SimSun; /*表格中文字的字体类型*/
font-size: 18px; /*表格中文字字体大小*/
color: #000; /*整个body区域字体的颜色*/
}
table {
*border-collapse: collapse; /*合并表格边框*/
border-spacing: 0; /*表格的边框宽度*/
width: 100%; /*整个表格相对父元素的宽度*/
}
.tableStyle {
/*border: solid #ggg 1px;*/
border-style: outset; /*整个表格外边框样式*/
border-width: 2px; /*整个表格外边框宽度*/
/*border: 2px;*/
border-color: blue; /*整个表格外边框颜色*/
}
.tableStyle tr:hover {
background: rgb(173,216,230); /*鼠标滑过一行时,动态显示的颜色146,208,80*/
}
.tableStyle td,.tableStyle th {
border-left: solid 1px rgb(146,208,80); /*表格的竖线颜色*/
border-top: 1px solid rgb(146,208,80); /*表格的横线颜色 */
padding: 15px; /*表格内边框尺寸*/
text-align: center; /*表格内容显示位置*/
}
.tableStyle th {
padding: 15px; /*表格标题栏,字体的尺寸*/
background-color: rgb(146,208,80); /*表格标题栏背景颜色*/
/*表格标题栏设置渐变颜色*/
background-image: -webkit-gradient(linear, left top, left bottom, from(#92D050), to(#A2D668));
/*rgb(146,208,80)*/
}
</style>
</head>
<body>
<center><h1>测试报告</h1></center><br />
<table class="tableStyle">
<thead>
<tr>
<th>Search Words</th>
<th>Assert Words</th>
<th>Start Time</th>
<th>Waste Time(s)</th>
<th>Status</th>
</tr>
</thead>'''
endStr = u'''
</table>
</body>
</html>'''
# 拼接完整的测试报告HTML页面代码
html = htmlStr + trData + endStr
print html
# 生成.html文件
with open(u"d:\\test\\testTemplate.html", "w") as fp:
fp.write(html.encode("gbk"))
data_drivern_by_file.py:
# encoding=utf-8
from selenium import webdriver
import unittest, time
import logging, traceback
import ddt
from ReportTemplate import htmlTemplate
from selenium.common.exceptions import NoSuchElementException
#如果有no json的报错信息,请将json文件存储为utf-8,with Bom
# 初始化日志对象
logging.basicConfig(
# 日志级别
level = logging.INFO,
# 日志格式
# 时间、代码所在文件名、代码行号、日志级别名字、日志信息
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
# 打印日志的时间
datefmt = '%a, %Y-%m-%d %H:%M:%S',
# 日志文件存放的目录(目录必须存在)及日志文件名
filename = 'd:/report.log',#’d:\\report.log’也可以
# 打开日志文件的方式
filemode = 'w'
)
@ddt.ddt
class TestDemo(unittest.TestCase):
@classmethod
def setUpClass(cls):
# 整个测试过程只被调用一次
TestDemo.trStr = ""
def setUp(self):
self.driver=webdriver.Firefox(executable_path="c:\\geckodriver")
status = None # 用于存放测试结果状态,失败'fail',成功'pass'
flag = 0 # 数据驱动测试结果的标志,失败置0,成功置1
@ddt.file_data("test_data_list.json")
def test_dataDrivenByFile(self, value):
# 决定测试报告中状态单元格中内容的颜色
flagDict = {0: 'red', 1: '#00AC4E'}
url = "http://www.baidu.com"
# 访问百度首页
self.driver.get(url)
# 将浏览器窗口最大化
self.driver.maximize_window()
print value
# 将从.json文件中读取出的数据用“||”进行分隔成测试数据
# 和期望数据
testdata, expectdata = tuple(value.strip().split("||"))
# 设置隐式等待时间为10秒
self.driver.implicitly_wait(10)
try:
# 获取当前的时间戳,用于后面计算查询耗时用
start = time.time()
# 获取当前时间的字符串,表示测试开始时间
startTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 找到搜索输入框,并输入测试数据
self.driver.find_element_by_id("kw").send_keys(testdata)
# 找到搜索按钮,并点击
self.driver.find_element_by_id("su").click()
time.sleep(3)
# 断言期望结果是否出现在页面源代码中
self.assertTrue(expectdata in self.driver.page_source)
except NoSuchElementException, e:
logging.error(u"查找的页面元素不存在,异常堆栈信息:" \
+ str(traceback.format_exc()))
status = 'fail'
flag = 0
except AssertionError, e:
logging.info(u"搜索“%s”,期望“%s”,失败" %(testdata, expectdata))
status = 'fail'
flag = 0
except Exception, e:
logging.error(u"未知错误,错误信息:" + str(traceback.format_exc()))
status = 'fail'
flag = 0
else:
logging.info(u"搜索“%s”,期望“%s”通过" %(testdata, expectdata))
status = 'pass'
flag = 1
# 计算耗时,从将测试数据输入到输入框中到断言期望结果之间所耗时
wasteTime = time.time() - start - 3 # 减去强制等待的3秒
# 每一组数据测试结束后,都将其测试结果信息插入表格行
# 的HTML代码中,并将这些行HTML代码拼接到变量trStr变量中,
# 等所有测试数据都被测试结束后,传入htmlTemplate()函数中
# 生成完整测试报告的HTML代码
TestDemo.trStr += u'''
#这段儿会被多次拼接,每搜索一次就会把模板字符串后边的字符拼接上
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%.2f</td>
<td style="color:%s">%s</td>
</tr><br />''' % (testdata, expectdata,startTime, wasteTime, flagDict[flag], status)
def tearDown(self):
self.driver.quit()
@classmethod
def tearDownClass(cls):
# 写自定义的html测试报告
# 整个测试过程只被调用一次
htmlTemplate(TestDemo.trStr)
if __name__ == '__main__':
unittest.main()
结果:
D:\test>python test.py
邓肯||蒂姆
testdata,expectdata: 邓肯 蒂姆
.乔丹||迈克尔
testdata,expectdata: 乔丹 迈克尔
.库里||斯蒂芬
testdata,expectdata: 库里 斯蒂芬
.杜兰特||凯文
testdata,expectdata: 杜兰特 凯文
.詹姆斯||勒布朗
testdata,expectdata: 詹姆斯 勒布朗
.<!DOCTYPE HTML>
<html>
<head>
<title>单元测试报告</title>
<style>
body{
width:80%;/*整个body区域占浏览器的宽度百分比*/
margin:40px auto;/*整个body区域相对浏览器窗口摆放位置(左右,上下)*/
font-weight:bold;/*整个body区域的字体加粗*/
font-family:'trebuchet MS','Lucida sans',SimSun;/*表格中文字的字体类型*/
font-size:18px;/*表格中文字字体大小*/
color:#000;/*整个body区域字体的颜色*/
}
table{
*border-collapse:collapse;/*合并表格边框*/
border-spacing:0;/*表格的边框宽度*/
width:100%;
}
.tableStyle{
/*border:solid #ggg 1px;*/
border-style:outset;/*整个表格外边框样式*/
border-width:2px;/*整个表格外边框宽度*/
/*border:2px*/
border-color:blue;/*整个表格外边框颜色*/
}
.tableStyle tr:hover{
background:rgb(173,216,230);/*鼠标滑过一行时,动态显示的颜色*/
}
.tableStyle td,.tableStyle th{
border-left:solid 1px rgb(146,208,80);/*表格的竖线颜色*/
border-top:1px solid rgb(146,208,80);/*表格的横线颜色*/
padding:15px;/*表格内边框尺寸*/
text-align:center;/*表格内容显示位置*/
}
.tableStyle th{
padding:15px;/*表格标题栏,字体的尺寸*/
background-color:rgb(146,208,80);/*表格标题栏背景颜色*/
/*表格标题栏设置渐变颜色*/
background-image:-webkit-gradient(linear,left top,left bottom,from(#92D050) to(#A2D668));/*rgb(14,208,80)*/
}
</style>
</head>
<body>
<center><h1>测试报告</h1></center><br/>
<table class='tableStyle'>
<thead>
<tr>
<th>Search Words</th>
<th>Assert Words</th>
<th>Start Time</th>
<th>Waste Time(s)</th>
<th>Status</th>
</tr>
</thead>
<tr>
<td>邓肯</td>
<td>蒂姆</td>
<td>2018-06-27 21:38:14</td>
<td>0.58</td>
<td style="color:#00AC4E">pass</td>
</tr><br/>
<tr>
<td>乔丹</td>
<td>迈克尔</td>
<td>2018-06-27 21:38:29</td>
<td>0.53</td>
<td style="color:#00AC4E">pass</td>
</tr><br/>
<tr>
<td>库里</td>
<td>斯蒂芬</td>
<td>2018-06-27 21:38:43</td>
<td>0.53</td>
<td style="color:#00AC4E">pass</td>
</tr><br/>
<tr>
<td>杜兰特</td>
<td>凯文</td>
<td>2018-06-27 21:38:59</td>
<td>0.51</td>
<td style="color:#00AC4E">pass</td>
</tr><br/>
<tr>
<td>詹姆斯</td>
<td>勒布朗</td>
<td>2018-06-27 21:39:13</td>
<td>0.56</td>
<td style="color:#00AC4E">pass</td>
</tr><br/>
</table>
</body>
</html>
----------------------------------------------------------------------
Ran 5 tests in 74.468s
OK
html报告:
report0627.log:
Wed,2018-06-27 21:38:18 test.py[line:110] INFO 搜索"邓肯",期望"蒂姆"通过
Wed,2018-06-27 21:38:33 test.py[line:110] INFO 搜索"乔丹",期望"迈克尔"通过
Wed,2018-06-27 21:38:47 test.py[line:110] INFO 搜索"库里",期望"斯蒂芬"通过
Wed,2018-06-27 21:39:03 test.py[line:110] INFO 搜索"杜兰特",期望"凯文"通过
Wed,2018-06-27 21:39:17 test.py[line:110] INFO 搜索"詹姆斯",期望"勒布朗"通过
python webdriver 测试框架-数据驱动json文件驱动的方式的更多相关文章
- python webdriver 测试框架-数据驱动txt文件驱动,带报告的例子
数据驱动txt文件驱动的方式,带报告 data.txt: gloryroad test||光荣之路 摔跤爸爸||阿米尔 超人||电影 data_driven_by_txt_file.py: #enco ...
- python webdriver 测试框架-数据驱动xml驱动方式
数据驱动xml驱动的方式 存数据的xml文件:TestData.xml: <?xml version="1.0" encoding="utf-8"?> ...
- python webdriver 测试框架-数据驱动excel驱动的方式
简介: 数据驱动excel驱动方式,就是数据配置在excel里面,主程序调用的时候每次用从excel里取出的数据作为参数,进行操作, 需要掌握的地方是对excel的操作,要灵活的找到目标数据 测试数据 ...
- python webdriver 测试框架-数据驱动exce驱动,不用ddt的方式
data.xlsx: 脚本: #encoding=utf-8from selenium import webdriverimport timeimport datetimefrom openpyxl ...
- python webdriver 测试框架-数据驱动DDT的例子
先在cmd环境 运行 pip install ddt 安装数据驱动ddt模块 脚本: #encoding=utf-8 from selenium import webdriver import un ...
- python webdriver 测试框架-行为驱动例子
安装行为驱动模块lettuce(卷心菜)模块 pip install lettuce Successfully installed argparse-1.4.0 colorama-0.3.9 extr ...
- python nose测试框架全面介绍七--日志相关
引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的 ...
- python nose测试框架全面介绍十---用例的跳过
又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...
- python nose测试框架全面介绍六--框架函数别名
之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.pa ...
随机推荐
- Nginx(四)-- 配置文件之location
1.location的作用 location主要做定位功能,根据uri来进行不同的定位. 2.location的语法 location [=|~|~*|^~] /uri/ { …} = 开头表示精确匹 ...
- Ubuntu 12.04 部署 PostGIS 2.1
首先,卸载掉原有的postgis和postgresql-9.1-postgis,不然你就用1.5版好了~ 1 sudo dpkg --purge postgis postgresql-9.1-post ...
- 【Android 7.1.1】 锁屏界面点击“空白处”响应事件
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLa ...
- webpack中,require的五种用法
a.js: module.exports = function(x){ console.log(x); } 一,commonjs同步: var b = require('./a');b('你好')// ...
- webpack----entry
入口文件下对象的键值,不多说,上图: 其实app就等同于name,于是乎 dist下的index.html中引入的js,就是: <script type="text/javascrip ...
- ansible批量验证密码
author: headsen chen date: 2018-08-31 20:45:49 综合比较salt-ssh,ansible .sshpass等批量验证100台机器的密码,最好推荐用ans ...
- exports和module.exports区别
参考:module.exports与exports的区别.关于exports的总结 exports 和 module.exports 的区别 module.exports是真正的模块接口,而expor ...
- intelliij jdea灰色文件处理办法
- 删除 Linux /tmp 目录下的临时文件
使用 tmpreaper How to install and use tmpreaper How to Remove Files Older than N days using tmpreaper ...
- SVN 配置和使用
SVN使用环境 使用SVN管理源代码,必须有2套环境 服务器 用来存储客户端上传的源码 一般都是在Windows环境下安装Visual SVN Server 客户端 用来提交.回退.修改.下载等操作 ...