Skip to content

Commit

Permalink
now it can support a url array
Browse files Browse the repository at this point in the history
  • Loading branch information
miniflycn committed Dec 9, 2013
1 parent df48016 commit 2713ef3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/har.js
Expand Up @@ -12,7 +12,7 @@ module.exports = (function () {
/**
* Har
* @class
* @param {String} address
* @param {String | Array} address
*/
function Har(address) {
var init = this.init.bind(this);
Expand All @@ -23,7 +23,7 @@ module.exports = (function () {
Har.prototype = {
constructor: Har,
init: function (address) {
var worker = spawn('phantomjs', [__dirname + '/netsiff.js', address])
var worker = spawn('phantomjs', [__dirname + '/netsiff.js'].concat(address))
, that = this;
worker.stdout.on('data', function (data) {
that.onsuccess(JSON.parse(data.toString()));
Expand Down
18 changes: 12 additions & 6 deletions lib/netsiff.js
Expand Up @@ -7,7 +7,12 @@
var webpage = require('webpage')
, write = require('system').stdout.write
, error = require('system').stderr.write
, address = require('system').args[1];
, ads = require('system').args.slice(1);

function begin() {
return siff(ads.shift());
}
begin();

/**
* siff
Expand Down Expand Up @@ -43,21 +48,22 @@ function siff(address) {
page.open(page.address, function (status) {
var har;
if (status !== 'success') {
error('FAIL to load the address');
phantom.exit(1);
error('FAIL to load the address: ' + address);
} else {
page.endTime = new Date();
page.title = page.evaluate(function () {
return document.title;
});
har = createHAR(page.address, page.title, page.resources, page.startTime, page.endTime);
write(JSON.stringify(har));
phantom.exit();
}
page.close();

setTimeout(function () {
ads.length ? begin() : phantom.exit();
}, 200);
});
}
siff(address);


/**
* createHar
Expand Down
9 changes: 9 additions & 0 deletions test/har.js
Expand Up @@ -28,6 +28,15 @@ describe('har', function () {
});
});

it('should able to get some har datas', function (done) {
var num = 0;
har(['http://127.0.0.1:7777/available/1.html', 'http://127.0.0.1:7777/available/2.html'])
.success(function (data) {
data.log.pages[0].id.should.equal(num === 0 ? 'http://127.0.0.1:7777/available/1.html' : 'http://127.0.0.1:7777/available/2.html')
if (++num === 1) return done();
})
})

it('should occur a error', function (done) {
har('wrongaddress').fail(function (data) {
data.should.equal('ADDRESS is wrong!');
Expand Down

0 comments on commit 2713ef3

Please sign in to comment.