2424 file .write (down_res .content )
2525file .close ()
2626
27+ '''获取ini/config.ini的邮件配置信息'''
28+ def getConfig (section , key ):
29+ cf .read (cfp , encoding = "utf8" )
30+ value = cf .get (section , key )
31+ return value
32+
33+
2734'''截取HTML测试报告关键信息'''
2835def reportElementHandle (wfile ):
2936 # 读取HTML文件
@@ -32,19 +39,53 @@ def reportElementHandle(wfile):
3239
3340 # 使用BeautifulSoup解析HTML
3441 soup = BeautifulSoup (html_content , 'lxml' )
42+ dic = {}
43+ timeoutAPI90 = 0
44+ timeoutAPI95 = 0
45+ failAPI = 0
46+
47+ # 读取config.ini API超时设置
48+ api_90timeout = int (getConfig ('apitimeout' , 'api_90timeout' ))
49+ api_95timeout = int (getConfig ('apitimeout' , 'api_95timeout' ))
3550
51+ ## HTML-info
3652 summ = soup .find (class_ = 'info' )
3753 allspan = summ .find_all ('span' )
38- dic = {}
3954 dic ['starttime' ]= allspan [0 ].text
4055 dic ['endtime' ] = allspan [1 ].text
56+ dic ['host' ] = allspan [2 ].text
57+
58+ ## HTML-requests
59+ rq = soup .find (class_ = 'requests' )
60+ alltd = rq .find_all ('td' )
61+ dic ['total RPS' ] = alltd [- 2 ].text
62+ dic ['Failures/s' ] = alltd [- 1 ].text
63+
64+ alltr = rq .find ('tbody' ).find_all ('tr' )
65+ dic ['API Amount' ] = len (alltr ) - 1
66+ for tr in alltr :
67+ alltd = tr .find_all ('td' )
68+ if int (alltd [3 ].text ) > 0 :
69+ failAPI += 1
70+ # 去掉聚合td的统计
71+ if failAPI > 0 :
72+ failAPI -= 1
73+ dic ['failAPIs' ] = failAPI
74+
75+ ## HTML-responses
76+ rs = soup .find (class_ = 'responses' )
77+ alltr = rs .find ('tbody' ).find_all ('tr' )
78+ for tr in alltr :
79+ alltd = tr .find_all ('td' )
80+ if int (alltd [6 ].text ) > api_90timeout :
81+ timeoutAPI90 += 1
82+ if int (alltd [7 ].text ) > api_95timeout :
83+ timeoutAPI95 += 1
84+ dic ['90timeoutAPIs' ] = timeoutAPI90
85+ dic ['95timeoutAPIs' ] = timeoutAPI95
86+
4187 return dic
4288
43- '''获取ini/config.ini的邮件配置信息'''
44- def getConfig (section , key ):
45- cf .read (cfp , encoding = "utf8" )
46- value = cf .get (section , key )
47- return value
4889
4990'''发送邮件'''
5091def send_email (subject , dicbody , file = None ):
@@ -57,15 +98,93 @@ def send_email(subject, dicbody, file=None):
5798
5899 # 创建 MIMEMultipart 对象
59100 # 邮件三个头部信息
60- mail = MIMEMultipart ()
101+ mail = MIMEMultipart ('alternative' )
61102 mail ['Subject' ] = subject
62103 mail ['From' ] = sender
63104 mail ['To' ] = "," .join (receiver )
64105
65- starttime = dicbody ['starttime' ]
66- endtime = dicbody ['endtime' ]
67- bodytext = f"starttime:{ starttime } GMT\n endtime:{ endtime } GMT\n 详细见附件"
68- mail .attach (MIMEText (bodytext , 'plain' , 'utf-8' ))
106+ # HTML内容
107+ if float (dicbody ['Failures/s' ]) > 0 :
108+ color_Failures = "red"
109+ else :
110+ color_Failures = "green"
111+
112+ if dicbody ['failAPIs' ] > 0 :
113+ color_failAPIs = "red"
114+ else :
115+ color_failAPIs = "green"
116+
117+ if dicbody ['90timeoutAPIs' ] > 0 :
118+ color_90timeoutAPIs = "red"
119+ else :
120+ color_90timeoutAPIs = "green"
121+
122+ if dicbody ['95timeoutAPIs' ] > 0 :
123+ color_95timeoutAPIs = "red"
124+ else :
125+ color_95timeoutAPIs = "green"
126+
127+ html_content = f"""
128+ <!DOCTYPE html>
129+ <html lang="en">
130+ <head>
131+ <meta charset="UTF-8">
132+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
133+ <title>Email Example</title>
134+ </head>
135+ <body>
136+ <table id="summary", style="width: 100%; margin:auto">
137+ <tr>
138+ <th style="font-weight: bold;font-size: 15px;">Host</th>
139+ <td colspan="4",style="font-size: 15px;">{ dicbody ['host' ]} </td>
140+ </tr>
141+ <tr>
142+ <th style="font-weight: bold;font-size: 15px;">StartTime</th>
143+ <td colspan="4",style="font-size: 15px;">{ dicbody ['starttime' ]} +00:00</td>
144+ </tr>
145+ <tr>
146+ <th style="font-weight: bold;font-size: 15px;">EndTime</th>
147+ <td colspan="4",style="font-size: 15px;">{ dicbody ['endtime' ]} +00:00</td>
148+ </tr>
149+ <tr>
150+ <th style="font-weight: bold; color: orange;font-size: 17px;">RPS</th>
151+ </tr>
152+ <tr>
153+ <th style="font-weight: bold;font-size: 15px;">Totals</th>
154+ <td colspan="4",style="font-weight: bold; font-size: 17px; color: green;">{ dicbody ['total RPS' ]} </td>
155+ </tr>
156+ <tr>
157+ <th style="font-weight: bold;font-size: 15px;">Failures</th>
158+ <td colspan="4",style="font-weight: bold; font-size: 17px; color: { color_Failures } ;">{ dicbody ['Failures/s' ]} </td>
159+ </tr>
160+ <tr>
161+ <th style="font-weight: bold; color: orange;font-size: 17px;">APIs</th>
162+ </tr>
163+ <tr>
164+ <th style="font-weight: bold;font-size: 15px;">Totals</th>
165+ <td colspan="4",style="font-size: 15px;">{ dicbody ['API Amount' ]} </td>
166+ </tr>
167+ <tr>
168+ <th style="font-weight: bold;font-size: 15px;">Failures</th>
169+ <td colspan="4",style="font-size: 15px; font-weight: bold; color: { color_failAPIs } ;">{ dicbody ['failAPIs' ]} </td>
170+ </tr>
171+ <tr>
172+ <th style="font-weight: bold; font-size: 15px;">90%Tout</th>
173+ <td colspan="4",style="font-size: 15px; font-weight: bold; color: { color_90timeoutAPIs } ;">{ dicbody ['90timeoutAPIs' ]} </td>
174+ </tr>
175+ <tr>
176+ <th style="font-weight: bold;font-size: 15px;">95%Tout</th>
177+ <td colspan="4",style="font-size: 15px; font-weight: bold; color: { color_95timeoutAPIs } ;">{ dicbody ['95timeoutAPIs' ]} </td>
178+ </tr>
179+ </table>
180+ </body>
181+ </html>
182+ """
183+ mail .attach (MIMEText (html_content , 'html' ))
184+
185+ # # 纯文本
186+ # bodytext = f"failAPIs:\n {dicbody['failAPIs']}\n timeoutAPIs:\n {dicbody['timeoutAPIs']}"
187+ # mail.attach(MIMEText(bodytext, 'plain', 'utf-8'))
69188
70189 # 附件
71190 att1 = MIMEText (open (file , 'rb' ).read (), 'html' , 'utf-8' )
0 commit comments