Commit 8cf9aaaf 8cf9aaaf3ffcc5271886446e83f5a751154a2e10 by Nicolas Perriault

refs #15 - added Casper.options.onResourceRequested() and Casper.options.onResou…

…rceReceived() callbacks
1 parent 09fe2ca2
......@@ -41,22 +41,24 @@
}
// default options
this.defaults = {
clientScripts: [],
faultTolerant: true,
logLevel: "error",
httpStatusHandlers: {},
onDie: null,
onError: null,
onLoadError: null,
onPageInitialized: null,
onStepComplete: null,
onStepTimeout: null,
onTimeout: null,
page: null,
pageSettings: { userAgent: DEFAULT_USER_AGENT },
stepTimeout: null,
timeout: null,
verbose: false
clientScripts: [],
faultTolerant: true,
logLevel: "error",
httpStatusHandlers: {},
onDie: null,
onError: null,
onLoadError: null,
onPageInitialized: null,
onResourceReceived: null,
onResourceRequested: null,
onStepComplete: null,
onStepTimeout: null,
onTimeout: null,
page: null,
pageSettings: { userAgent: DEFAULT_USER_AGENT },
stepTimeout: null,
timeout: null,
verbose: false
};
// privates
// local properties
......@@ -1714,6 +1716,9 @@
casper.loadInProgress = false;
};
page.onResourceReceived = function(resource) {
if (isType(casper.options.onResourceReceived, "function")) {
casper.options.onResourceReceived(casper, resource);
}
if (resource.url === casper.requestUrl && resource.stage === "start") {
casper.currentHTTPStatus = resource.status;
if (isType(casper.options.httpStatusHandlers, "object") && resource.status in casper.options.httpStatusHandlers) {
......@@ -1722,6 +1727,11 @@
casper.currentUrl = resource.url;
}
};
page.onResourceRequested = function(request) {
if (isType(casper.options.onResourceRequested, "function")) {
casper.options.onResourceRequested(casper, request);
}
};
return page;
}
......
......@@ -216,13 +216,32 @@ casper.thenOpen('tests/site/global.html', function(self) {
// Casper.options.onStepComplete
casper.then(function(self) {
self.options.onStepComplete = function(self, stepResult) {
self.test.comment('Casper.options.onStepComplete()')
self.test.comment('Casper.options.onStepComplete()');
self.test.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete');
self.options.onStepComplete = null;
};
return 'ok';
});
// Casper.options.onResourceRequested & Casper.options.onResourceReceived
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.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.options.onResourceRequested = null;
};
self.thenOpen('tests/site/page1.html');
});
// History
casper
.thenOpen('tests/site/page1.html')
......