-
Notifications
You must be signed in to change notification settings - Fork 29
/
phantom.js
71 lines (59 loc) · 1.56 KB
/
phantom.js
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
/**
* Module Dependencies
*/
var Crawler = require('x-ray-crawler');
var cheerio = require('cheerio');
var join = require('path').join;
var assert = require('assert');
var phantom = require('../');
var fs = require('fs');
/**
* Tests
*/
describe('phantom driver', function() {
it('should have sensible defaults', function(done) {
var crawler = Crawler()
.driver(phantom())
crawler('http://google.com', function(err, ctx) {
if (err) return done(err);
var $ = cheerio.load(ctx.body);
var title = $('title').text();
assert.equal('Google', title);
done();
})
});
it('should work with client-side pages', function(done) {
var crawler = Crawler()
.driver(phantom());
crawler('https://exchange.coinbase.com/trade', function(err, ctx) {
if (err) return done(err);
var $ = cheerio.load(ctx.body);
var price = $('.market-num').text();
assert.equal(false, isNaN(+price));
done();
})
})
it('should support custom functions', function(done) {
var crawler = Crawler()
.driver(phantom(runner));
crawler('http://mat.io', function(err, ctx) {
if (err) return done(err);
var $ = cheerio.load(ctx.body);
var title = $('title').text();
assert.equal('Lapwing Labs', title);
done();
})
function runner(ctx, nightmare) {
return nightmare
.goto(ctx.url)
.click('.Header-logo-item+ .Header-list-item a')
.wait()
}
})
})
/**
* Read
*/
function get(path) {
return require(join(__dirname, 'fixtures', path));
}