evaluate.js
3.39 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
/*global casper*/
/*jshint strict:false maxparams:99*/
casper.test.comment('Casper.evaluate()');
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);
casper.test.assertEquals(result.toString(),
['boolean', 'boolean', 'number', 'number', 'string', 'object', 'object', 'function'].toString(),
'Casper.evaluate() handles passed argument context correcly');
// no context
casper.test.assertEquals(casper.evaluate(function() {
return 42;
}), 42, 'Casper.evaluate() handles evaluation with no context passed');
// object context (previous casperjs versions compatibility mode)
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, {a: "foo"}), ["foo"], 'Casper.evaluate() accepts an object as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, {a: "foo", b: "bar"}), ["foo", "bar"], 'Casper.evaluate() accepts an object as arguments context');
casper.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');
// array context
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, ["foo"]), ["foo"], 'Casper.evaluate() accepts an array as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, ["foo", "bar"]), ["foo", "bar"], 'Casper.evaluate() accepts an array as arguments context');
casper.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');
// natural arguments context (phantomjs equivalent)
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, "foo"), ["foo"], 'Casper.evaluate() accepts natural arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, "foo", "bar"), ["foo", "bar"], 'Casper.evaluate() accepts natural arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, "foo", "bar", "baz"), ["foo", "bar", "baz"], 'Casper.evaluate() accepts natural arguments context');
casper.start().thenEvaluate(function(a, b) {
window.a = a
window.b = b;
}, "foo", "bar");
casper.then(function() {
this.test.comment('Casper.thenEvaluate()');
this.test.assertEquals(this.getGlobal('a'), "foo", "Casper.thenEvaluate() sets args");
this.test.assertEquals(this.getGlobal('b'), "bar",
"Casper.thenEvaluate() sets args the same way evaluate() does");
});
casper.run(function() {
this.test.done(13);
});