-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_Html.py
153 lines (153 loc) · 6.1 KB
/
py_Html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# encoding: utf-8
"""
@file: py_Html.py
"""
import os
titles='接口测试'
def title(titles):
title='''<!DOCTYPE html>
<html>
<head>
<title>%s</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 Shim 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
<!-- 注意: 如果通过 file:// 引入 Respond.js 文件,则该文件无法起效果 -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<style type="text/css">
.hidden-detail,.hidden-tr{
display:none;
}
</style>
</head>
<body>
'''%(titles)
return title
connent='''
<div class='col-md-4 col-md-offset-4' style='margin-left:3%;'>
<h1>接口测试的结果</h1>'''
def shouye(starttime,endtime,passge,fail,excepthions,weizhicuowu):
beijing='''
<table class="table table-hover table-condensed">
<tbody>
<tr>
<td><strong>开始时间:</strong> %s</td>
</tr>
<td><strong>结束时间:</strong> %s</td></tr>
<td><strong>耗时:</strong> %s</td></tr>
<td><strong>结果:</strong>
<span >Pass: <strong >%s</strong>
Fail: <strong >%s</strong>
exception: <strong >%s</strong>
weizhicuowu : <strong >%s</strong></span></td>
</tr>
</tbody></table>
</div> '''%(starttime,endtime,(endtime-starttime),passge,fail,excepthions,weizhicuowu)
return beijing
shanghai='''<div class="row " style="margin:60px">
<div style=' margin-top: 18%;' >
<div class="btn-group" role="group" aria-label="...">
<button type="button" id="check-all" class="btn btn-primary">所有用例</button>
<button type="button" id="check-success" class="btn btn-success">成功用例</button>
<button type="button" id="check-danger" class="btn btn-danger">失败用例</button>
<button type="button" id="check-warning" class="btn btn-warning">错误用例</button>
<button type="button" id="check-except" class="btn btn-defult">异常用例</button>
</div>
<div class="btn-group" role="group" aria-label="...">
</div>
<table class="table table-hover table-condensed table-bordered" style="word-wrap:break-word; word-break:break-all; margin-top: 7px;">
<tr >
<td ><strong>用例ID </strong></td>
<td><strong>用例名字</strong></td>
<td><strong>key</strong></td>
<td><strong>请求内容</strong></td>
<td><strong>url</strong></td>
<td><strong>请求方式</strong></td>
<td><strong>预期</strong></td>
<td><strong>实际返回</strong></td>
<td><strong>结果</strong></td>
</tr>
'''
def passfail(tend):
if tend =='pass':
htl='''<td bgcolor="green">pass</td>'''
elif tend =='fail':
htl='''<td bgcolor="fail">fail</td>'''
elif tend=='weizhi':
htl='''<td bgcolor="red">error</td>'''
else:
htl = '<td bgcolor="crimson">exect</td>'
return htl
def ceshixiangqing(reslt,id,name,key,params,url,method,yuqi,json,relust):
xiangqing='''
<tr class="case-tr %s">
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
%s
</tr>
'''%(reslt,id,name,key,params,url,method,yuqi,json,passfail(relust))
return xiangqing
weibu='''</div></div></table><script src="https://code.jquery.com/jquery.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
$("#check-danger").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".success").addClass("hidden-tr");
$(".warning").addClass("hidden-tr");
$(".error").addClass("hidden-tr");
});
$("#check-warning").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".success").addClass("hidden-tr");
$(".danger").addClass("hidden-tr");
$(".error").addClass("hidden-tr");
});
$("#check-success").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".warning").addClass("hidden-tr");
$(".danger").addClass("hidden-tr");
$(".error").addClass("hidden-tr");
});
$("#check-except").click(function(e){
$(".case-tr").removeClass("hidden-tr");
$(".warning").addClass("hidden-tr");
$(".danger").addClass("hidden-tr");
$(".success").addClass("hidden-tr");
});
$("#check-all").click(function(e){
$(".case-tr").removeClass("hidden-tr");
});
</script>
</body></html>'''
def relust(titles,starttime,endtime,passge,fail,id,name,key,params,url,method,anticipate,json,relust,exceptions,weizhi):
if type(name) ==list:
relus=' '
for i in range(len(name)):
if relust[i] == "pass":
clazz = "success"
elif relust[i]== "fail":
clazz = "warning"
elif relust[i]== "weizhi":
clazz = "danger"
else:
clazz='error'
relus+=(ceshixiangqing(clazz,id[i],name[i],key[i],params[i],url[i],method[i],anticipate[i],json[i],relust[i]))
text=title(titles)+connent+shouye(starttime,endtime,passge,fail,exceptions,weizhi)+shanghai+relus+weibu
else:
text=title(titles)+connent+shouye(starttime,endtime,passge,fail,exceptions,weizhi)+shanghai+ceshixiangqing(id,name,key,params,url,method,anticipate,json,relust)+weibu
return text
def createHtml(filepath,titles,starttime,endtime,passge,fail,id,name,key,coneent,url,meth,yuqi,json,relusts,exceptions,weizhi):
texts=relust(titles,starttime,endtime,passge,fail,id,name,key,coneent,url,meth,yuqi,json,relusts,exceptions,weizhi)
with open(filepath,'wb') as f:
f.write(texts.encode('utf-8'))