Commit 0b240e99 0b240e9939ce922f228f5cd0d2ea6e6ce642aaec by Laurent Jouanneau

Refs #482: CasperJS should wait after the browser initialization

The call of webpage.open() (or openUrl()) is synchronous in PhantomJS,
but is asynchronous in SlimerJS (it needs to open a real window etc..).

So CasperJS should not launch the next step just after the call of
webpage.openUrl(). A flag is set to true before this call, and
then set to false when receiving the first event from the page loader.
And this flag has to be checked before to execute the next step.
1 parent cd8bcd8f
...@@ -139,6 +139,7 @@ var Casper = function Casper(options) { ...@@ -139,6 +139,7 @@ var Casper = function Casper(options) {
139 this.history = []; 139 this.history = [];
140 this.loadInProgress = false; 140 this.loadInProgress = false;
141 this.navigationRequested = false; 141 this.navigationRequested = false;
142 this.browserInitializing = false;
142 this.logFormats = {}; 143 this.logFormats = {};
143 this.logLevels = ["debug", "info", "warning", "error"]; 144 this.logLevels = ["debug", "info", "warning", "error"];
144 this.logStyles = { 145 this.logStyles = {
...@@ -357,7 +358,7 @@ Casper.prototype.captureSelector = function captureSelector(targetFile, selector ...@@ -357,7 +358,7 @@ Casper.prototype.captureSelector = function captureSelector(targetFile, selector
357 */ 358 */
358 Casper.prototype.checkStep = function checkStep(self, onComplete) { 359 Casper.prototype.checkStep = function checkStep(self, onComplete) {
359 "use strict"; 360 "use strict";
360 if (self.pendingWait || self.loadInProgress || self.navigationRequested) { 361 if (self.pendingWait || self.loadInProgress || self.navigationRequested || self.browserInitializing) {
361 return; 362 return;
362 } 363 }
363 var step = self.steps[self.step++]; 364 var step = self.steps[self.step++];
...@@ -1380,6 +1381,7 @@ Casper.prototype.open = function open(location, settings) { ...@@ -1380,6 +1381,7 @@ Casper.prototype.open = function open(location, settings) {
1380 // custom headers 1381 // custom headers
1381 this.page.customHeaders = utils.mergeObjects(utils.clone(baseCustomHeaders), customHeaders); 1382 this.page.customHeaders = utils.mergeObjects(utils.clone(baseCustomHeaders), customHeaders);
1382 // perfom request 1383 // perfom request
1384 this.browserInitializing = true;
1383 this.page.openUrl(this.requestUrl, { 1385 this.page.openUrl(this.requestUrl, {
1384 operation: settings.method, 1386 operation: settings.method,
1385 data: settings.data 1387 data: settings.data
...@@ -2382,6 +2384,7 @@ function createPage(casper) { ...@@ -2382,6 +2384,7 @@ function createPage(casper) {
2382 message += ': ' + casper.requestUrl; 2384 message += ': ' + casper.requestUrl;
2383 casper.log(message, "warning"); 2385 casper.log(message, "warning");
2384 casper.navigationRequested = false; 2386 casper.navigationRequested = false;
2387 casper.browserInitializing = false;
2385 if (utils.isFunction(casper.options.onLoadError)) { 2388 if (utils.isFunction(casper.options.onLoadError)) {
2386 casper.options.onLoadError.call(casper, casper, casper.requestUrl, status); 2389 casper.options.onLoadError.call(casper, casper, casper.requestUrl, status);
2387 } 2390 }
...@@ -2400,6 +2403,7 @@ function createPage(casper) { ...@@ -2400,6 +2403,7 @@ function createPage(casper) {
2400 page.onNavigationRequested = function onNavigationRequested(url, type, willNavigate, isMainFrame) { 2403 page.onNavigationRequested = function onNavigationRequested(url, type, willNavigate, isMainFrame) {
2401 casper.log(f('Navigation requested: url=%s, type=%s, willNavigate=%s, isMainFrame=%s', 2404 casper.log(f('Navigation requested: url=%s, type=%s, willNavigate=%s, isMainFrame=%s',
2402 url, type, willNavigate, isMainFrame), "debug"); 2405 url, type, willNavigate, isMainFrame), "debug");
2406 casper.browserInitializing = false;
2403 if (isMainFrame && casper.requestUrl !== url) { 2407 if (isMainFrame && casper.requestUrl !== url) {
2404 casper.navigationRequested = true; 2408 casper.navigationRequested = true;
2405 2409
......