diff --git a/lib/actor.js b/lib/actor.js index 76be1965c..ae7061f72 100644 --- a/lib/actor.js +++ b/lib/actor.js @@ -55,6 +55,40 @@ function recordStep(step, args) { let task = `${step.name}: ${step.humanizeArgs()}`; let val; + // check to ensure browser is loaded + recorder.add('wait-for-browser-ready', function(){ + return new Promise((resolve, reject) => { + + if (step.helper.isReady || step.helper.isReady === undefined) { + resolve(); + } else { + + // Check for the page to be loaded for n number of times. + var checkTimes = 100; + + // poll every n ms to see if page has loaded + var check = setInterval(() => { + + // the page is now ready + if(step.helper.isReady){ + resolve(); + clearInterval(check); + } + + checkTimes--; + // If check times is below 0 then throw an error as the page is unable to load + if(checkTimes < 0){ + clearInterval(check); + reject(`${step.helperMethod} failed to inject codecept.js`); + } + + }, 100); + + } + + }); + }); + // run step inside promise recorder.add(task, () => { event.emit(event.step.started, step); diff --git a/lib/codecept.js b/lib/codecept.js index a7664ea45..321c97398 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -1,5 +1,6 @@ 'use strict'; +let Mocha = require('mocha/lib/mocha'); let fsPath = require('path'); let readFileSync = require('fs').readFileSync; let readdirSync = require('fs').readdirSync; diff --git a/lib/helper.js b/lib/helper.js index 7644e7a73..c88994671 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -125,4 +125,6 @@ class Helper { } +Helper.isReady = true; + module.exports = Helper; diff --git a/lib/helper/Nightmare.js b/lib/helper/Nightmare.js index 01db4085d..7b854388a 100644 --- a/lib/helper/Nightmare.js +++ b/lib/helper/Nightmare.js @@ -189,7 +189,14 @@ class Nightmare extends Helper { _startBrowser() { this.browser = this.Nightmare(this.options); - this.browser.on('dom-ready', () => this._injectClientScripts()); + this.browser.on('did-start-loading', () => { + this.isReady = false; + }); + // when dom is ready, inject client script and set our flag to true to enable tests to run again + this.browser.on('dom-ready', () => { + this._injectClientScripts(); + this.isReady = true; + }); this.browser.on('console', (type, message) => { this.debug(`${type}: ${message}`); });