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 @@ ...@@ -41,22 +41,24 @@
41 } 41 }
42 // default options 42 // default options
43 this.defaults = { 43 this.defaults = {
44 clientScripts: [], 44 clientScripts: [],
45 faultTolerant: true, 45 faultTolerant: true,
46 logLevel: "error", 46 logLevel: "error",
47 httpStatusHandlers: {}, 47 httpStatusHandlers: {},
48 onDie: null, 48 onDie: null,
49 onError: null, 49 onError: null,
50 onLoadError: null, 50 onLoadError: null,
51 onPageInitialized: null, 51 onPageInitialized: null,
52 onStepComplete: null, 52 onResourceReceived: null,
53 onStepTimeout: null, 53 onResourceRequested: null,
54 onTimeout: null, 54 onStepComplete: null,
55 page: null, 55 onStepTimeout: null,
56 pageSettings: { userAgent: DEFAULT_USER_AGENT }, 56 onTimeout: null,
57 stepTimeout: null, 57 page: null,
58 timeout: null, 58 pageSettings: { userAgent: DEFAULT_USER_AGENT },
59 verbose: false 59 stepTimeout: null,
60 timeout: null,
61 verbose: false
60 }; 62 };
61 // privates 63 // privates
62 // local properties 64 // local properties
...@@ -1714,6 +1716,9 @@ ...@@ -1714,6 +1716,9 @@
1714 casper.loadInProgress = false; 1716 casper.loadInProgress = false;
1715 }; 1717 };
1716 page.onResourceReceived = function(resource) { 1718 page.onResourceReceived = function(resource) {
1719 if (isType(casper.options.onResourceReceived, "function")) {
1720 casper.options.onResourceReceived(casper, resource);
1721 }
1717 if (resource.url === casper.requestUrl && resource.stage === "start") { 1722 if (resource.url === casper.requestUrl && resource.stage === "start") {
1718 casper.currentHTTPStatus = resource.status; 1723 casper.currentHTTPStatus = resource.status;
1719 if (isType(casper.options.httpStatusHandlers, "object") && resource.status in casper.options.httpStatusHandlers) { 1724 if (isType(casper.options.httpStatusHandlers, "object") && resource.status in casper.options.httpStatusHandlers) {
...@@ -1722,6 +1727,11 @@ ...@@ -1722,6 +1727,11 @@
1722 casper.currentUrl = resource.url; 1727 casper.currentUrl = resource.url;
1723 } 1728 }
1724 }; 1729 };
1730 page.onResourceRequested = function(request) {
1731 if (isType(casper.options.onResourceRequested, "function")) {
1732 casper.options.onResourceRequested(casper, request);
1733 }
1734 };
1725 return page; 1735 return page;
1726 } 1736 }
1727 1737
......
...@@ -216,13 +216,32 @@ casper.thenOpen('tests/site/global.html', function(self) { ...@@ -216,13 +216,32 @@ casper.thenOpen('tests/site/global.html', function(self) {
216 // Casper.options.onStepComplete 216 // Casper.options.onStepComplete
217 casper.then(function(self) { 217 casper.then(function(self) {
218 self.options.onStepComplete = function(self, stepResult) { 218 self.options.onStepComplete = function(self, stepResult) {
219 self.test.comment('Casper.options.onStepComplete()') 219 self.test.comment('Casper.options.onStepComplete()');
220 self.test.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete'); 220 self.test.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete');
221 self.options.onStepComplete = null; 221 self.options.onStepComplete = null;
222 }; 222 };
223 return 'ok'; 223 return 'ok';
224 }); 224 });
225 225
226 // Casper.options.onResourceRequested & Casper.options.onResourceReceived
227 casper.then(function(self) {
228 self.options.onResourceReceived = function(self, resource) {
229 self.test.comment('Casper.options.onResourceReceived()');
230 self.test.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object');
231 self.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object')
232 self.options.onResourceReceived = null;
233 };
234 self.options.onResourceRequested = function(self, request) {
235 self.test.comment('Casper.options.onResourceRequested()');
236 self.test.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object');
237 self.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object')
238 self.options.onResourceRequested = null;
239 };
240 self.thenOpen('tests/site/page1.html');
241 });
242
243
244
226 // History 245 // History
227 casper 246 casper
228 .thenOpen('tests/site/page1.html') 247 .thenOpen('tests/site/page1.html')
......