evaluate.js
4.86 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
/*eslint strict:0, max-params:0*/
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();
});
});
// https://github.com/casperjs/casperjs/issues/489
// https://groups.google.com/forum/?fromgroups=#!topic/casperjs/95IgDMFnEKM
casper.test.begin("evaluate() returns a value which can be altered", 1, function(test) {
var list;
casper.start().then(function() {
list = this.evaluate(function() {
return [{a: 1}, {b: 2}];
});
var first = list[0];
first.a = 42;
test.assertEquals(list, [{a: 42}, {b: 2}],
'evaluate() returns a cloned value which can be altered');
});
casper.run(function() {
test.done();
});
});
// https://github.com/casperjs/casperjs/issues/841
casper.test.begin("evaluate() with js disabled, throws error", 1, function(test) {
casper.options.pageSettings.javascriptEnabled = false;
casper.start().then(function() {
function getListResult() {
return this.evaluate(function() {
return [{a: 1}, {b: 2}];
});
}
test.assertThrows(getListResult, undefined,
"Casper.evaluate() raises an error if JavaScript is disabled in the page");
});
casper.run(function() {
test.done();
casper.options.pageSettings.javascriptEnabled = true;
});
});