Skip to content

Commit 4873696

Browse files
committed
Merge pull request #9 from rexxars/history-support
Added support for getting history of tests from WPT
2 parents 092d69b + b9dc22e commit 4873696

13 files changed

Lines changed: 173 additions & 6 deletions

File tree

lib/helper.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* Released under the MIT License
55
*/
66

7-
var xml2js = require('xml2js'),
8-
url = require('url'),
9-
os = require('os');
7+
var xml2js = require('xml2js'),
8+
url = require('url'),
9+
os = require('os'),
10+
csv = require('csv'),
11+
entities = require('entities');
1012

1113
var parser = new xml2js.Parser({explicitArray: false, mergeAttrs: true});
1214

@@ -98,6 +100,20 @@ function svToObj(delimiter, headers, sv) {
98100
return obj;
99101
}
100102

103+
function csvParser(data, callback) {
104+
csv()
105+
.from(data.toString(), { columns: true })
106+
.transform(function(row, index) {
107+
var key, value;
108+
for (key in row) {
109+
value = row[key].replace(/<b>|<\/b>/g, '');
110+
row[key] = entities.decode(value, 2);
111+
}
112+
return row;
113+
})
114+
.to.array(callback);
115+
}
116+
101117
// Net log has a buggy end of file, attempt to fix
102118
function netLogParser(data) {
103119
data = (data || '{}').toString();
@@ -223,6 +239,7 @@ module.exports = {
223239
xmlToObj: xmlToObj,
224240
csvToObj: svToObj.bind(null, ',', false),
225241
tsvToObj: svToObj.bind(null, '\t'),
242+
csvParser: csvParser,
226243
netLogParser: netLogParser,
227244
scriptToString: scriptToString,
228245
dataURI: dataURI,

lib/mapping.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,12 @@ var commands = {
624624
param: 'id',
625625
info: 'get test request info/details'
626626
},
627+
'history': {
628+
name: 'getHistory',
629+
param: 'days',
630+
optional: true,
631+
info: 'get history of previously run tests'
632+
},
627633
'waterfall': {
628634
name: 'getWaterfallImage',
629635
param: 'id',

lib/webpagetest.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var paths = {
2828
har: 'export.php',
2929
waterfall: 'waterfall.php',
3030
thumbnail: 'thumbnail.php',
31-
cancel: 'cancelTest.php'
31+
cancel: 'cancelTest.php',
32+
history: 'testlog.php'
3233
};
3334

3435
var filenames = {
@@ -39,6 +40,7 @@ var filenames = {
3940
netLog: 'netlog.txt',
4041
consoleLog: 'console_log.json',
4142
testInfo: 'testinfo.json',
43+
history: 'history.csv',
4244
waterfall: 'waterfall.png',
4345
screenshot: 'screen.jpg',
4446
screenshotStartRender: 'screen_render.jpg',
@@ -509,6 +511,28 @@ function getTestInfo(id, options, callback) {
509511
}, options);
510512
}
511513

514+
function getHistory(days, options, callback) {
515+
callback = callback || options;
516+
options = options === callback ? {} : options;
517+
var query = {
518+
all: 'on',
519+
f: 'csv',
520+
days: days ? parseInt(days, 10) : 1
521+
};
522+
523+
return api.call(this, paths.history, function(err, data) {
524+
if (err) {
525+
return callback(err);
526+
} else if (!(data instanceof Buffer)) {
527+
return callback(err, data);
528+
}
529+
530+
helper.csvParser(data, function(parsed) {
531+
callback(err, parsed);
532+
});
533+
}, query, options);
534+
}
535+
512536
function getWaterfallImage(id, options, callback) {
513537
var query,
514538
pathname = paths.waterfall;
@@ -618,6 +642,7 @@ WebPageTest.prototype = {
618642
getNetLogData: getNetLogData,
619643
getConsoleLogData: getConsoleLogData,
620644
getTestInfo: getTestInfo,
645+
getHistory: getHistory,
621646
getWaterfallImage: getWaterfallImage,
622647
getScreenshotImage: getScreenshotImage,
623648
scriptToString: WebPageTest.scriptToString,
@@ -638,6 +663,7 @@ WebPageTest.prototype = {
638663
netlog: getNetLogData,
639664
console: getConsoleLogData,
640665
testinfo: getTestInfo,
666+
history: getHistory,
641667
waterfall: getWaterfallImage,
642668
screenshot: getScreenshotImage
643669
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
"dependencies": {
3636
"commander": "~2.0.0",
3737
"mocha": "~1.13.0",
38-
"xml2js": "~0.2.8"
38+
"xml2js": "~0.2.8",
39+
"csv": "~0.3.6",
40+
"entities": "~0.3.0"
3941
},
4042
"devDependencies": {
4143
"nock": "~0.22.1"

test/command-line-test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ describe('WebPageTest Command Line', function() {
231231
});
232232
});
233233

234+
it('gets a history input returns the API url', function(done) {
235+
exec(mock('history 2'), function(err, data) {
236+
if (err) return done(err);
237+
data = JSON.parse(data);
238+
assert.equal(data.url, wptServer + 'testlog.php?all=on&f=csv&days=2');
239+
done();
240+
});
241+
});
242+
234243
it('gets a waterfall image input returns the API url', function(done) {
235244
exec(mock('waterfall 120816_V2_2'), function(err, data) {
236245
if (err) return done(err);
@@ -289,7 +298,7 @@ describe('WebPageTest Command Line', function() {
289298
[
290299
'', 'status', 'results', 'locations', 'testers', 'test', 'cancel', 'har',
291300
'pagespeed', 'utilization', 'request', 'timeline', 'netlog', 'console',
292-
'testinfo', 'waterfall', 'screenshot', 'listen'
301+
'testinfo', 'history', 'waterfall', 'screenshot', 'listen'
293302
].forEach(function eachCmd(command) {
294303
it('gets a ' + command + ' help input and returns the help text', function(done) {
295304
exec(mock(command + ' --help'), function(err, data) {

test/dryrun-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ describe('Dry Run', function() {
235235
});
236236
});
237237

238+
it('gets history request', function(done) {
239+
wpt.getHistory(2, {dryRun: true}, function (err, data) {
240+
if (err) return done(err);
241+
assert.equal(data.url, wptServer + 'testlog.php?all=on&f=csv&days=2');
242+
done();
243+
});
244+
});
245+
238246
it('gets a waterfall image request', function(done) {
239247
wpt.getWaterfallImage('120816_V2_2', {dryRun: true}, function (err, data) {
240248
if (err) return done(err);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
Usage: history [options] [days]
3+
4+
Options:
5+
6+
-h, --help output usage information
7+

test/fixtures/command-line/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
netlog [options] <id> get the Chrome Developer Tools Net log data (if available) from test
1818
console [options] <id> get the browser console log data (if available) from test
1919
testinfo <id> get test request info/details
20+
history [days] get history of previously run tests
2021
waterfall [options] <id> get the waterfall PNG image
2122
screenshot [options] <id> get the fully loaded page screenshot in JPG format (PNG if in full resolution)
2223
listen [hostname:port] start webpagetest-api server on <hostname>:<port> [hostname:7791]

test/fixtures/objects/history.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"Date/Time": "11/28/13 16:24:24",
4+
"Location": "Dublin, Ireland - Chrome - Cable",
5+
"Test ID": "131128_9N_S23",
6+
"URL": "http://tech.vg.no/"
7+
},
8+
{
9+
"Date/Time": "11/28/13 16:24:23",
10+
"Location": "Dulles, VA - Chrome - Cable",
11+
"Test ID": "131128_5W_S28",
12+
"URL": "http://www.bdaygreetings.com"
13+
},
14+
{
15+
"Date/Time": "11/28/13 16:24:23",
16+
"Location": "Buenos Aires, Argentina - Chrome - Cable",
17+
"Test ID": "131128_HA_S27",
18+
"URL": "https://buyingflow.mercadolibre.com.ar/?item_id=MLA485711339&quantity=1"
19+
},
20+
{
21+
"Date/Time": "11/28/13 16:24:19",
22+
"Location": "Dulles, VA - IE 9 - Cable",
23+
"Test ID": "131128_XC_S26",
24+
"URL": "http://www.pietzoomers.com"
25+
},
26+
{
27+
"Date/Time": "11/28/13 16:24:19",
28+
"Location": "São Paulo, Brasil - Chrome - Cable",
29+
"Test ID": "131128_A2_S25",
30+
"URL": "http://www.livrariasaraiva.com.br"
31+
},
32+
{
33+
"Date/Time": "11/28/13 16:24:17",
34+
"Location": "Buenos Aires, Argentina - IE8 - Cable",
35+
"Test ID": "131128_NW_S24",
36+
"URL": "http://ti.kwikkiangie.ac.id"
37+
},
38+
{
39+
"Date/Time": "11/28/13 16:23:11",
40+
"Location": "Madrid, Spain - IE 8 - Cable",
41+
"Test ID": "131128_CM_S15",
42+
"URL": "https://www.rastreator.com/seguro-coche/datos-comparativa.aspx"
43+
},
44+
{
45+
"Date/Time": "11/28/13 16:23:04",
46+
"Location": "Dublin, Ireland - Chrome - Cable",
47+
"Test ID": "131128_H8_S11",
48+
"URL": "http://www.bilgi-sor.com"
49+
},
50+
{
51+
"Date/Time": "11/28/13 16:22:40",
52+
"Location": "Dulles, VA - IE 9 - Cable",
53+
"Test ID": "131128_YA_S0S",
54+
"URL": "http://www.debrastefan.com/"
55+
},
56+
{
57+
"Date/Time": "11/28/13 16:22:35",
58+
"Location": "Dulles, VA - IE 9 - Cable",
59+
"Test ID": "131128_HS_S0M",
60+
"URL": "http://recreational-looting.herokuapp.com"
61+
}
62+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"Date/Time","Location","Test ID","URL"
2+
"11/28/13 16:24:24","<b>Dublin, Ireland</b> - <b>Chrome</b> - <b>Cable</b>","131128_9N_S23","http://tech.vg.no/"
3+
"11/28/13 16:24:23","Dulles, VA - <b>Chrome</b> - <b>Cable</b>","131128_5W_S28","http://www.bdaygreetings.com"
4+
"11/28/13 16:24:23","Buenos Aires, Argentina - <b>Chrome</b> - <b>Cable</b>","131128_HA_S27","https://buyingflow.mercadolibre.com.ar/?item_id=MLA485711339&amp;quantity=1"
5+
"11/28/13 16:24:19","Dulles, VA - IE 9 - <b>Cable</b>","131128_XC_S26","http://www.pietzoomers.com"
6+
"11/28/13 16:24:19","S&atilde;o Paulo, Brasil - <b>Chrome</b> - <b>Cable</b>","131128_A2_S25","http://www.livrariasaraiva.com.br"
7+
"11/28/13 16:24:17","Buenos Aires, Argentina - IE8 - <b>Cable</b>","131128_NW_S24","http://ti.kwikkiangie.ac.id"
8+
"11/28/13 16:23:11","<b>Madrid, Spain - IE 8</b> - <b>Cable</b>","131128_CM_S15","https://www.rastreator.com/seguro-coche/datos-comparativa.aspx"
9+
"11/28/13 16:23:04","<b>Dublin, Ireland</b> - <b>Chrome</b> - <b>Cable</b>","131128_H8_S11","http://www.bilgi-sor.com"
10+
"11/28/13 16:22:40","Dulles, VA - IE 9 - <b>Cable</b>","131128_YA_S0S","http://www.debrastefan.com/"
11+
"11/28/13 16:22:35","Dulles, VA - IE 9 - <b>Cable</b>","131128_HS_S0M","http://recreational-looting.herokuapp.com"

0 commit comments

Comments
 (0)