Commit 90afad2b 90afad2b87dc605e350a3e8537d94c4787f78ab3 by Nicolas Perriault

Implemented a better alternative for retrieving contextualized parameters in Casper#evaluate()

Now as an alternative, CasperJS injects a `__casper_params__` Object instance containing all the parameters you passed:

    casper.evaluate(function() {
        document.querySelector('#username').value = __casper_params__.username;
        document.querySelector('#password').value = __casper_params__.password;
        document.querySelector('#submit').click();
    }, {
        username: 'Bazoonga',
        password: 'baz00nga'
    })
1 parent ae7d5b78
...@@ -57,6 +57,28 @@ casper.then(function(self) { ...@@ -57,6 +57,28 @@ casper.then(function(self) {
57 57
58 casper.test.assert(casper.steps.length === 2, 'then() adds a new navigation step'); 58 casper.test.assert(casper.steps.length === 2, 'then() adds a new navigation step');
59 59
60 // Casper#evaluate()
61
62 casper.then(function(self) {
63 var params = {
64 "boolean true": true,
65 "boolean false": false,
66 "int number": 42,
67 "float number": 1337.42,
68 "string": "plop! \"Ÿ£$\" 'no'",
69 "array": [1, 2, 3],
70 "object": {a: 1, b: 2}
71 };
72 var casperParams = self.evaluate(function() {
73 return __casper_params__;
74 }, params);
75 self.test.assertType(casperParams, "object", 'Casper.evaluate() exposes parameters in a dedicated object');
76 self.test.assertEquals(Object.keys(casperParams).length, 7, 'Casper.evaluate() exposes parameters object has the correct length');
77 for (var param in casperParams) {
78 self.test.assertEquals(JSON.stringify(casperParams[param]), JSON.stringify(params[param]), 'Casper.evaluate() can pass a ' + param);
79 }
80 });
81
60 // Casper#fill() 82 // Casper#fill()
61 casper.then(function(self) { 83 casper.then(function(self) {
62 self.test.assertTitle('CasperJS test form', 'click() casper can click on a text link and react when it is loaded 2/2'); 84 self.test.assertTitle('CasperJS test form', 'click() casper can click on a text link and react when it is loaded 2/2');
...@@ -116,7 +138,7 @@ casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, ...@@ -116,7 +138,7 @@ casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/,
116 xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); 138 xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz');
117 casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'addFailure() adds a failed testcase'); 139 casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'addFailure() adds a failed testcase');
118 140
119 // Casper.ClientUtils.log 141 // Casper.ClientUtils.log()
120 casper.then(function(self) { 142 casper.then(function(self) {
121 casper.test.comment('client utils log'); 143 casper.test.comment('client utils log');
122 var oldLevel = casper.options.logLevel; 144 var oldLevel = casper.options.logLevel;
...@@ -136,30 +158,30 @@ casper.then(function(self) { ...@@ -136,30 +158,30 @@ casper.then(function(self) {
136 casper.options.verbose = true; 158 casper.options.verbose = true;
137 }); 159 });
138 160
139 // Casper.wait 161 // Casper.wait()
140 var start = new Date().getTime(); 162 var start = new Date().getTime();
141 casper.wait(1000); 163 casper.wait(1000, function(self) {
142 casper.then(function(self) { 164 self.test.comment('wait');
143 casper.test.comment('wait');
144 self.test.assert(new Date().getTime() - start > 1000, 'Casper.wait() can wait for a given amount of time'); 165 self.test.assert(new Date().getTime() - start > 1000, 'Casper.wait() can wait for a given amount of time');
145 });
146 166
147 // Casper.waitFor 167 // Casper.waitFor()
148 casper.thenOpen('tests/site/waitFor.html', function(self) { 168 casper.thenOpen('tests/site/waitFor.html', function(self) {
149 casper.test.comment('waitFor'); 169 casper.test.comment('waitFor');
150 self.waitFor(function(self) { 170 self.waitFor(function(self) {
151 return self.evaluate(function() { 171 return self.evaluate(function() {
152 return document.querySelectorAll('li').length === 4; 172 return document.querySelectorAll('li').length === 4;
173 });
174 }, function(self) {
175 self.test.pass('Casper.waitFor() can wait for something to happen');
176 }, function(self) {
177 self.test.fail('Casper.waitFor() can wait for something to happen');
153 }); 178 });
154 }, function(self) {
155 self.test.pass('Casper.waitFor() can wait for something to happen');
156 }, function(self) {
157 self.test.fail('Casper.waitFor() can wait for something to happen');
158 }); 179 });
159 }); 180 });
160 181
161 // run suite 182 // run suite
162 casper.run(function(self) { 183 casper.run(function(self) {
184 self.test.comment('logging, again');
163 self.test.assertEquals(self.result.log.length, 3, 'log() logged messages'); 185 self.test.assertEquals(self.result.log.length, 3, 'log() logged messages');
164 self.test.renderResults(true, 0, save); 186 self.test.renderResults(true, 0, save);
165 }); 187 });
......