better error handling for Casper.getGlobal()
Showing
3 changed files
with
22 additions
and
8 deletions
... | @@ -515,9 +515,21 @@ | ... | @@ -515,9 +515,21 @@ |
515 | * @return mixed | 515 | * @return mixed |
516 | */ | 516 | */ |
517 | getGlobal: function(name) { | 517 | getGlobal: function(name) { |
518 | return this.evaluate(function() { | 518 | var result = this.evaluate(function() { |
519 | return window[window.__casper_params__.name]; | 519 | var name = window.__casper_params__.name; |
520 | var result = {}; | ||
521 | try { | ||
522 | result.value = JSON.stringify(window[name]); | ||
523 | } catch (e) { | ||
524 | result.error = 'Unable to JSON encode window.' + name + ': ' + e; | ||
525 | } | ||
526 | return result; | ||
520 | }, {'name': name}); | 527 | }, {'name': name}); |
528 | if (result.error) { | ||
529 | throw result.error; | ||
530 | } else { | ||
531 | return JSON.parse(result.value); | ||
532 | } | ||
521 | }, | 533 | }, |
522 | 534 | ||
523 | /** | 535 | /** | ... | ... |
... | @@ -134,9 +134,9 @@ casper.then(function(self) { | ... | @@ -134,9 +134,9 @@ casper.then(function(self) { |
134 | return document.querySelector('input[name="file"]').files.length === 1; | 134 | return document.querySelector('input[name="file"]').files.length === 1; |
135 | }, true, 'Casper.fill() can select a file to upload'); | 135 | }, true, 'Casper.fill() can select a file to upload'); |
136 | self.test.assertEvalEquals(function() { | 136 | self.test.assertEvalEquals(function() { |
137 | return document.querySelector('input[name="checklist[]"][value="1"]').checked | 137 | return (document.querySelector('input[name="checklist[]"][value="1"]').checked && |
138 | && !document.querySelector('input[name="checklist[]"][value="2"]').checked | 138 | !document.querySelector('input[name="checklist[]"][value="2"]').checked && |
139 | && document.querySelector('input[name="checklist[]"][value="3"]').checked | 139 | document.querySelector('input[name="checklist[]"][value="3"]').checked); |
140 | }, true, 'Casper.fill() can fill a list of checkboxes'); | 140 | }, true, 'Casper.fill() can fill a list of checkboxes'); |
141 | self.click('input[type="submit"]'); | 141 | self.click('input[type="submit"]'); |
142 | }); | 142 | }); |
... | @@ -216,7 +216,8 @@ casper.then(function() { | ... | @@ -216,7 +216,8 @@ casper.then(function() { |
216 | // Casper.getGlobal() | 216 | // Casper.getGlobal() |
217 | casper.thenOpen('tests/site/global.html', function(self) { | 217 | casper.thenOpen('tests/site/global.html', function(self) { |
218 | self.test.comment('Casper.getGlobal()'); | 218 | self.test.comment('Casper.getGlobal()'); |
219 | self.test.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable') | 219 | self.test.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable'); |
220 | self.test.assertRaises(self.getGlobal, ['myUnencodableGlobal'], 'Casper.getGlobal() does not fail trying to encode an unencodable global'); | ||
220 | }); | 221 | }); |
221 | 222 | ||
222 | // Casper.options.onStepComplete | 223 | // Casper.options.onStepComplete |
... | @@ -234,13 +235,13 @@ casper.then(function(self) { | ... | @@ -234,13 +235,13 @@ casper.then(function(self) { |
234 | self.options.onResourceReceived = function(self, resource) { | 235 | self.options.onResourceReceived = function(self, resource) { |
235 | self.test.comment('Casper.options.onResourceReceived()'); | 236 | self.test.comment('Casper.options.onResourceReceived()'); |
236 | self.test.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object'); | 237 | self.test.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object'); |
237 | self.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object') | 238 | self.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object'); |
238 | self.options.onResourceReceived = null; | 239 | self.options.onResourceReceived = null; |
239 | }; | 240 | }; |
240 | self.options.onResourceRequested = function(self, request) { | 241 | self.options.onResourceRequested = function(self, request) { |
241 | self.test.comment('Casper.options.onResourceRequested()'); | 242 | self.test.comment('Casper.options.onResourceRequested()'); |
242 | self.test.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object'); | 243 | self.test.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object'); |
243 | self.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object') | 244 | self.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object'); |
244 | self.options.onResourceRequested = null; | 245 | self.options.onResourceRequested = null; |
245 | }; | 246 | }; |
246 | self.thenOpen('tests/site/page1.html'); | 247 | self.thenOpen('tests/site/page1.html'); | ... | ... |
... | @@ -3,6 +3,7 @@ | ... | @@ -3,6 +3,7 @@ |
3 | <body> | 3 | <body> |
4 | <script type="text/javascript"> | 4 | <script type="text/javascript"> |
5 | var myGlobal = 'awesome string'; | 5 | var myGlobal = 'awesome string'; |
6 | var myUnencodableGlobal = document; | ||
6 | </script> | 7 | </script> |
7 | </body> | 8 | </body> |
8 | </html> | 9 | </html> |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment