casper.js
3.98 KB
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
if (phantom.casperVersion.major >= 1 &&
(phantom.casperVersion.minor > 0 || phantom.casperVersion.patch > 2)
) {
require = patchRequire(require);
}
var options = phantom.casperArgs.options;
var createFunction =
require(options.spooky_lib + 'lib/bootstrap/create-function');
var casper = require('casper');
var emit = require(options.spooky_lib + 'lib/bootstrap/emit').emit;
var instance;
var methodsToProvide = {
back: [],
echo: ['message', 'style'],
forward: [],
open: ['location', 'settings'],
run: [function onComplete () {}, 'time'],
start: ['url', function then () {}],
then: [function fn () {}],
thenClick: ['selector', function fn () {}],
thenEvaluate: [function fn () {}, 'replacements'],
thenOpen: ['location', 'options'],
thenOpenAndEvaluate: ['location', function fn () {}, 'replacements'],
userAgent: ['agent'],
wait: ['timeout', function then () {}],
waitFor: [
function testFn () {}, function then () {},
function onTimeout () {}, 'timeout'
],
waitForPopup: [
'urlPattern', function then () {},
function onTimeout () {}, 'timeout'
],
waitForResource: [
'resource', function then () {},
function onTimeout () {}, 'timeout'
],
waitForSelector: [
'selector', function then () {},
function onTimeout () {}, 'timeout'
],
waitUntilVisible: [
'selector', function then () {},
function onTimeout () {}, 'timeout'
],
waitWhileSelector: [
'selector', function then () {},
function onTimeout () {}, 'timeout'
],
waitWhileVisible: [
'selector', function then () {},
function onTimeout () {}, 'timeout'
],
withFrame: ['frameInfo', function then () {}],
withPopup: ['popupInfo', function then () {}]
};
function callMethod(method, spec) {
var args = Array.prototype.slice.call(arguments, 2);
if (args.length > spec.length) {
throw [
'Expected maximum of', spec.length,
'arguments, but got', args.length
].join(' ');
}
for (var i = 0; i < args.length; i++) {
if (typeof spec[i] === 'function') {
args[i] = createFunction(args[i]);
}
}
instance[method].apply(instance, args);
return true;
}
var methods = {};
for (var k in methodsToProvide) {
/* jshint loopfunc: true */
methods[k] = (function (method, spec) {
return function () {
var args = [method, spec].
concat(Array.prototype.slice.call(arguments));
return callMethod.apply(null, args);
};
})(k, methodsToProvide[k]);
}
function getCreateFn(server) {
/**
* Create a new CasperJS instance
*/
return function create (options) {
options = options || {};
for (var k in options) {
if (k.indexOf('on') === 0) {
options[k] = createFunction(options[k]);
}
}
if (options.httpStatusHandlers) {
for (k in options.httpStatusHandlers) {
options.httpStatusHandlers[k] =
createFunction(options.httpStatusHandlers[k]);
}
}
server._instance = instance = casper.create(options);
instance.emit = function () {
var args = Array.prototype.slice.apply(arguments);
if (args[0] === 'starting' && server.stop) {
server.stop();
} else if (args[0] === 'run.complete' && server.start) {
server.start();
}
try {
emit.apply(this, arguments);
} catch (e) {
emit.call(this, 'log', e.message, args[1].location);
}
this.constructor.prototype.emit.apply(this, args.slice());
};
return {
methods: server.provides()
};
};
}
function provideAll(server) {
server.provide(getCreateFn(server));
server.provide(methods);
}
exports.provideAll = provideAll;