Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 11 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,20 @@ function driver(options, fn) {
return function phantom_driver(ctx, done) {
debug('going to %s', ctx.url);

nightmare
.on('error', error)
.on('timeout', function(timeout) {
return done(new Error(timeout));
})
.on('resourceReceived', function(resource) {
if (normalize(resource.url) == normalize(ctx.url)) {
debug('got response from %s: %s', resource.url, resource.status);
ctx.status = resource.status;
};
})
.on('urlChanged', function(url) {
debug('redirect: %s', url);
ctx.url = url;
})

wrapfn(fn, select)(ctx, nightmare);

function select(err, ret) {
if (err) return done(err);

nightmare
fn(ctx, nightmare)
.evaluate(function() {
return document.documentElement.outerHTML;
}, function(body) {
ctx.body = body;
return document.documentElement.outerHTML;
})
.end()
.then(function (body) {
debug('got response from %s, content length: %s', ctx.url, (body || '').length);
ctx.body = body;
done(null, ctx);
})
.run(function(err) {
if (err) return done(err);
debug('%s - %s', ctx.url, ctx.status);
done(null, ctx);
.catch(function (err) {
debug('nightmare error', err);
if (err) return done(err);
});
};
}
}

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
},
"dependencies": {
"debug": "^2.1.1",
"nightmare": "^1.7.0",
"nightmare": "^2.5.2",
"normalizeurl": "^0.1.3",
"wrap-fn": "^0.1.4"
},
"devDependencies": {
"cheerio": "^0.19.0",
"mocha": "*",
"nightmare-swiftly": "^0.2.4",
"x-ray-crawler": "^2.0.1"
},
"main": "index"
}
}
28 changes: 0 additions & 28 deletions test.js

This file was deleted.

29 changes: 13 additions & 16 deletions test/phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ var fs = require('fs');
describe('phantom driver', function() {

it('should have sensible defaults', function(done) {
var crawler = Crawler()
.driver(phantom())
var crawler = Crawler(phantom());

crawler('http://google.com', function(err, ctx) {
if (err) return done(err);
Expand All @@ -28,9 +27,8 @@ describe('phantom driver', function() {
})
});

it('should work with client-side pages', function(done) {
var crawler = Crawler()
.driver(phantom());
it.skip('should work with client-side pages', function(done) {
var crawler = Crawler(phantom());

crawler('https://exchange.coinbase.com/trade', function(err, ctx) {
if (err) return done(err);
Expand All @@ -39,28 +37,27 @@ describe('phantom driver', function() {
assert.equal(false, isNaN(+price));
done();
})
})
});

it('should support custom functions', function(done) {
var crawler = Crawler()
.driver(phantom(runner));
var crawler = Crawler(phantom(runner));

crawler('http://mat.io', function(err, ctx) {
crawler('http://www.npmjs.com', function(err, ctx) {
if (err) return done(err);
var $ = cheerio.load(ctx.body);
var title = $('title').text();
assert.equal('Lapwing Labs', title);
assert.equal('npm Documentation', title);
done();
})
});

function runner(ctx, nightmare) {
return nightmare
.goto(ctx.url)
.click('.Header-logo-item+ .Header-list-item a')
.wait()
.goto(ctx.url)
.click('#nav-docs-link')
.wait()
}
})
})
});
});

/**
* Read
Expand Down