利用Jenkins+Phantomas进行前端性能自动化测试

来源:互联网 发布:张艺谋奥运会知乎 编辑:程序博客网 时间:2024/05/02 00:59

原文地址:https://www.bstester.com/2015/12/front-end-performance-using-jenkinsphantomas-automated-testing

前面我们介绍了怎么安装配置ShowSlow和HARViewer,以及Phantomas的使用简介,那怎么将这些工具串起来搭建一个前端自动化测试平台呢?且听我慢慢道来……

这里假设大家都已经正确安装了phantomjs、phantomas、slimerjs、showslow、harviewer和yslow(yslow.js下载地址:http://yslow.org/phantomjs/),这些都是需要用到的工具。

通过yslow进行页面分析并上报到showslow平台进行数据收集。

1
#phantomjs yslow.js http://www.liveapp.cn/ --info grade  -b http://192.168.1.11:8088/beacon/yslow/

这样就可以将yslow评估的报告上报到showslow平台了。
jp1

利用phantomas生成har文件

1
#phantomas  http://www.liveapp.cn/ --engine gecko --har live.har

这里我们指定了引擎为gecko,默认引擎为webkit,之所以指定引擎,是因为在测试中发现,gecko获取的信息比使用webkit要多很多,接近于浏览器调试模式下获取的结果,更接近于真实情况。但是也有个问题,gecko引擎不是很稳定,在Linux下会出现异常,但是不影响har文件的生成。
复制生成的har文件中的内容到HARViewer中可以直接查看结果。
jp2

至此,我们需要的数据已经可以通过手工生成,后续就是配置数据自动上报了。
yslow的数据通过上面的命令已经可以自动上报,那其实只剩下怎么上报har的问题了。
我们需要先打开showslow中上报har的接口开关。
编辑配置文件

1
#vim config.php

jp3
如上图所示,去掉第41和44行前面的注释即可。
开启后,可到http://192.168.1.11:8088/beacon/har/这里查看详细的配置说明。
jp4
我们通过文件上传的方式将har数据上报给showslow,使用python模拟文件上传即可完成。
在解决完数据上报的问题后,将这些脚本归集起来,在Jenkins中配置好,就可以完成自动化的测试了。

为了更加的灵活,我们需要可以在页面中配置需要监控测试的页面,而不是这样写死在脚本中,我们需要开启showslow的添加域名功能。
编辑配置文件

1
#vim global.php

jp5
如上图,将154行改成

1
$enableMyURLs = true;

这样,我们就可以通过如下图所示添加链接了,添加链接前,需要注册登录showslow。
jp6
在实际工作中,我们在测试时还需要模拟是PC端还是手机端,被测试的页面是否需要登录等等问题,这些在测试时都可以通过带相应的参数来配置,具体参数说明,请参考相应工具的帮助信息。
下面给出我在项目中使用的脚本:

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python
#coding=utf-8

from phantomas import Phantomas
import json
import http.client
from urllib.parse import urlencode
from urllib.parse import unquote
import sys,os,re
import time
import smtplib 
from email.mime.text import MIMEText
import pymysql

#获取harid,用于生成测试报告邮件用
def getHARid():
    try:
        conn = pymysql.connect('192.168.1.12','username','password','showslow',charset='utf8')
        cur = conn.cursor()
        cur.execute('SELECT id,url,har_last_id FROM urls;')
        res = cur.fetchall()
        cur.close()
        conn.close()
        return res
    except Exception as e:
        print(e)
        return False

#登录获取session
def login(host,username,password):
    parseURL = host.split('/')
    host =  parseURL[2]
    request_url = ''
    for i in range(3,len(parseURL)):
        request_url = request_url + '/' + parseURL[i]

    headers = {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
               'X-Requested-With':'XMLHttpRequest',
               'Connection':'keep-alive',
               'Referer':'http://' + host,
               'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36'}

    request_data = {"username":username,"password":password,"remeber":"0"}
    conn = http.client.HTTPConnection(host)
    conn.request('POST',request_url,urlencode(request_data),headers=headers)
    response = conn.getresponse()
    if response.status != 200:
        return None
    resp = response.read()
    resp = resp.decode('utf-8')
    return json.loads(resp)['message']

#生成har文件
def createHAR(url,platform,harName,cookie):
    config='config_mobile.json'
    if platform == 'website':
        config='config_website.json'
    try:
        print('正在为 %s 生成HAR' % url)
        os.system('phantomas %s --engine gecko --har %s --cookie %s --config %s &' % (url,harName,cookie,config))
        st = time.time()
        while not os.path.isfile(harName):
            time.sleep(5)
            et = time.time()
            if et - st > 300:
                break
    except Exception as e:
        print(e)
    finally:
        if os.path.isfile(harName):
            return harName
        else:
            print('%s 生成HAR失败!' % url)
            return False

#执行测试
def webTest(url,platform,cookie):
    config='config_mobile.json'
    if platform == 'website':
        config='config_website.json'
    try:
        print('正在测试 %s' % url)
        results = Phantomas(
            url=url,
            engine='webkit',
            cookie=cookie,
            config=config,
            ).run()
        return results
    except Exception as e:
        print(e)
        print('测试 %s 失败!' % url)
        return False

#创建测试报告
def createHTML(harRes,yslowRes,html,j,urlID,harID):
    if harRes.get_metric('domContentLoaded') < 1000:
        return html,j
    p = '<p>BodySize: %.2f kb' % (harRes.get_metric('bodySize')/1024.0)
    p += '<p>Requests: %i</p>' % harRes.get_metric('requests')
    p += '<p>DomInteractive: %i ms</p>' % harRes.get_metric('domInteractive')
    p += '<p>DomContentLoaded: %i ms</p>' % harRes.get_metric('domContentLoaded')
    p += '<p>DomContentLoadedEnd: %i ms</p>' % harRes.get_metric('domContentLoadedEnd')
    p += '<p>DomComplete: %i ms</p>' % harRes.get_metric('domComplete')
    pt = ''
    for i in harRes.get_offenders('slowestResponse'):
        pt += i + ''
    p += '<p>SlowestResponse:</p><p>%s</p>' % pt
    pt = ''
    for i in harRes.get_offenders('biggestResponse'):
        pt += i + ''
    p += '<p>BiggestResponse:</p><p>%s</p>' % pt
    pt = ''
    for i in harRes.get_offenders('assetsNotGzipped'):
        pt += i + ''
    p += '<p>AssetsNotGzipped:</p><p>%s</p>' % pt
    html += '''<div class="block">
<div class="url"><a href="http://192.168.1.11:8088/details/?urlid=%i" target="_blank">%s</a> (<a href="%s" target="_blank">查看HAR</a>)</div>
<div class="title" onclick="showDiv('pre%s')"><b>YSlow</b> 点击展开</div>
<pre id="pre%s">%s</pre>
<div class="title" onclick="showDiv('div%s')"><b>Phantomas</b> 点击展开</div>
<div id="div%s">%s</div>
</div>''' % (urlID,harRes.get_url(),'http://192.168.1.11:8088/harviewer/?inputUrl=http%3A%2F%2F192.168.1.11%3A8088%2Fdetails%2Fhar.php%3Fid%3D'+str(harID)+'callback%3DonInputData',j,j,yslowRes,j,j,p)
    j += 1
    return html,j

#上报har数据到showslow
def postHarToShowslow(host,harName,url):
    parseURL = host.split('/')
    host =  parseURL[2]
    request_url = ''
    for i in range(3,len(parseURL)):
        request_url = request_url + '/' + parseURL[i]

    headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                'Accept-Encoding':'gzip, deflate',
                'Accept-Language':'zh-CN,zh;q=0.8',
               'Content-Type':'x-application/har+json; charset=UTF-8'}
    fopen = open(harName,'rb')
    data = fopen.read()
    fopen.close()
    conn = http.client.HTTPConnection(host)
    conn.request('POST',request_url + '?url=' + url,data,headers=headers)
    response = conn.getresponse()
    return response.status
  
#进行yslow测试并上报
def ySlow(url,yslow,platform,cookie):
    ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4'
    viewport = '640x1040'
    headers = {"Cookie":cookie}
    if platform == 'website':
        ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'
        viewport = '1920x1080'
    cmd = "phantomjs yslow.js --info grade --ua '%s' %s -b %s -vp %s -ch '%s'" % (ua,url,yslow,viewport,json.dumps(headers))
    print('YSlow评估 %s' % cmd)
    os.system(cmd)
    return os.popen("phantomjs yslow.js --info grade --ua '%s' %s -f plain -vp %s -ch '%s'" % (ua,url,viewport,json.dumps(headers))).read()

#邮件发送
def sendMail(text):
    sender = 'no-reply@host.cn'  
    receiver = ['user@host.cn']
    mailToCc = []
    subject = '[ShowSlow]前端性能自动化测试报告'  
    smtpserver = 'smtp.exmail.qq.com'  
    username = 'no-reply@host.cn'  
    password = 'password'  
    
    msg = MIMEText(text,'html','utf-8')      
    msg['Subject'] = subject  
    msg['From'] = sender
    msg['To'] = ';'.join(receiver)
    msg['Cc'] = ';'.join(mailToCc)
    smtp = smtplib.SMTP()  
    smtp.connect(smtpserver)  
    smtp.login(username, password)  
    smtp.sendmail(sender, receiver + mailToCc, msg.as_string())  
    smtp.quit() 

def main():
    showslow = 'http://192.168.1.11:8088/monitor.php'
    harViewer = 'http://192.168.1.11:8088/beacon/har/'
    yslow = 'http://192.168.1.11:8088/beacon/yslow/'
    loginURL = 'http://www.host.com/login'
    username = 'user@host.cn'
    password = 'password'
    
    parseURL = showslow.split('/')
    host =  parseURL[2]
    request_url = ''
    for i in range(3,len(parseURL)):
        request_url = request_url + '/' + parseURL[i]

    conn = http.client.HTTPConnection(host)
    conn.request('GET',request_url)
    response = conn.getresponse()
    if response.status != 200:
        print('登录失败!!')
        return False
    urls = response.read().decode('utf8')
    session = login(loginURL,username,password)
    cookie = 'session=%s' % session
    html = '''< !DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<style>
.body {max-width:100%;margin-left:10%;margin-right:10%}
.url {height:30px;text-align:center;font-size:20px;line-height: 24px;margin-bottom:5px;margin-top:5px;}
.title {line-height: 18px;font-size: 16px;}
.title + pre,.title + div {height:110px;overflow:hidden}
.block {border: 1px;border-style: dashed;border-color: #cccccc;}
</style>
<script>
function showDiv(elm){
    if (document.getElementById){
        target = document.getElementById(elm);
        if (target.style.height == "100%"){
            target.style.height = "110px";
        } else {
            target.style.height = "100%";
        }
    }
}
</script>
</head>
<body>
<div class="body"><p><b>详情趋势请访问ShowSlow:</b> <a href="http://192.168.1.11:8088/">http://192.168.1.11:8088/</a> (仅报告DomContentLoaded > 1000ms的页面)</p>
<p><b>因腾讯邮箱显示问题,详情请访问:</b><a href="http://192.168.1.14/showslow.html">http://192.168.1.14/showslow.html</a></p>
'''
    i = 0
    rows = getHARid()
    for url in urls.split():
        harID = 0
        urlID = 0
        for row in rows:
            if row[1].decode('utf8') == url:
                urlID = row[0]
                harID = row[2]
                break
        harName = '%i.har' % int(time.time())
        platform = 'mobile'
        if not re.match(r'^http://u\.|^http://m\.',url):
            platform = 'website'
        if createHAR(url,platform,harName,cookie) != False:
            status = postHarToShowslow(harViewer,harName,url)
            print(url,status)
        results = webTest(url,platform,cookie)
        if results != False:
            html,i = createHTML(results,ySlow(url,yslow,platform,cookie),html,i,urlID,harID)
    html += '</div></body></html>'
    if i > 0:
        fp = open('/var/www/html/showslow.html','w',encoding='utf8')
        fp.write(html)
        fp.close()
        sendMail(html)

if __name__ == '__main__':
    main()

将脚本配置到Jenkins中,设置好定时执行时间,即可在指定的时间自动执行测试。
jp7


原文:

0 0
原创粉丝点击