Commit 6bf71011 6bf710114963065b367276ae5f1259a76522195c by Nicolas Perriault

better error handling for Casper.getGlobal()

1 parent 11a936d0
......@@ -515,9 +515,21 @@
* @return mixed
*/
getGlobal: function(name) {
return this.evaluate(function() {
return window[window.__casper_params__.name];
var result = this.evaluate(function() {
var name = window.__casper_params__.name;
var result = {};
try {
result.value = JSON.stringify(window[name]);
} catch (e) {
result.error = 'Unable to JSON encode window.' + name + ': ' + e;
}
return result;
}, {'name': name});
if (result.error) {
throw result.error;
} else {
return JSON.parse(result.value);
}
},
/**
......
......@@ -134,9 +134,9 @@ casper.then(function(self) {
return document.querySelector('input[name="file"]').files.length === 1;
}, true, 'Casper.fill() can select a file to upload');
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="checklist[]"][value="1"]').checked
&& !document.querySelector('input[name="checklist[]"][value="2"]').checked
&& document.querySelector('input[name="checklist[]"][value="3"]').checked
return (document.querySelector('input[name="checklist[]"][value="1"]').checked &&
!document.querySelector('input[name="checklist[]"][value="2"]').checked &&
document.querySelector('input[name="checklist[]"][value="3"]').checked);
}, true, 'Casper.fill() can fill a list of checkboxes');
self.click('input[type="submit"]');
});
......@@ -216,7 +216,8 @@ casper.then(function() {
// Casper.getGlobal()
casper.thenOpen('tests/site/global.html', function(self) {
self.test.comment('Casper.getGlobal()');
self.test.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable')
self.test.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable');
self.test.assertRaises(self.getGlobal, ['myUnencodableGlobal'], 'Casper.getGlobal() does not fail trying to encode an unencodable global');
});
// Casper.options.onStepComplete
......@@ -234,13 +235,13 @@ casper.then(function(self) {
self.options.onResourceReceived = function(self, resource) {
self.test.comment('Casper.options.onResourceReceived()');
self.test.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object');
self.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object')
self.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object');
self.options.onResourceReceived = null;
};
self.options.onResourceRequested = function(self, request) {
self.test.comment('Casper.options.onResourceRequested()');
self.test.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object');
self.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object')
self.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object');
self.options.onResourceRequested = null;
};
self.thenOpen('tests/site/page1.html');
......
......@@ -3,6 +3,7 @@
<body>
<script type="text/javascript">
var myGlobal = 'awesome string';
var myUnencodableGlobal = document;
</script>
</body>
</html>
\ No newline at end of file
......