evaluate.js
3.66 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
/*global casper*/
/*jshint strict:false maxparams:99*/
casper.test.begin('mapping argument context', 1, function(test) {
casper.start();
var context = {
"_boolean_true": true,
"_boolean_false": false,
"_int_number": 42,
"_float_number": 1337.42,
"_string": "plop! \"Ÿ£$\" 'no'",
"_array": [1, 2, 3],
"_object": {a: 1, b: 2},
"_function": function(){console.log('ok');}
};
var result = casper.evaluate(function(_boolean_true, _boolean_false, _int_number,
_float_number, _string, _array, _object, _function) {
return [].map.call(arguments, function(arg) {
return typeof(arg);
});
}, context);
test.assertEquals(
result.toString(),
['boolean', 'boolean', 'number', 'number', 'string', 'object', 'object', 'function'].toString(),
'Casper.evaluate() handles passed argument context correcly'
);
test.done();
});
casper.test.begin('handling no argument context', 1, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function() {
return 42;
}), 42, 'Casper.evaluate() handles evaluation with no context passed');
test.done();
});
casper.test.begin('handling of object context (BC mode)', 3, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function(a) {
return [a];
}, {a: "foo"}), ["foo"], 'Casper.evaluate() accepts an object as arguments context');
test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, {a: "foo", b: "bar"}), ["foo", "bar"], 'Casper.evaluate() accepts an object as arguments context');
test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, {a: "foo", b: "bar", c: "baz"}), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an object as arguments context');
test.done();
});
casper.test.begin('handling of array context', 3, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function(a) {
return [a];
}, ["foo"]), ["foo"], 'Casper.evaluate() accepts an array as arguments context');
test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, ["foo", "bar"]), ["foo", "bar"], 'Casper.evaluate() accepts an array as arguments context');
test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, ["foo", "bar", "baz"]), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an array as arguments context');
test.done();
});
casper.test.begin('natural arguments context (phantomjs equivalent)', 3, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function(a) {
return [a];
}, "foo"), ["foo"], 'Casper.evaluate() accepts natural arguments context');
test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, "foo", "bar"), ["foo", "bar"], 'Casper.evaluate() accepts natural arguments context');
test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, "foo", "bar", "baz"), ["foo", "bar", "baz"], 'Casper.evaluate() accepts natural arguments context');
test.done();
});
casper.test.begin('thenEvaluate() tests', 2, function(test) {
casper.start().thenEvaluate(function(a, b) {
window.a = a
window.b = b;
}, "foo", "bar");
casper.then(function() {
test.assertEquals(this.getGlobal('a'), "foo", "Casper.thenEvaluate() sets args");
test.assertEquals(this.getGlobal('b'), "bar",
"Casper.thenEvaluate() sets args the same way evaluate() does");
});
casper.run(function() {
test.done();
});
});