From 04170fdcead3329dd1384f406b0d39b2fd902ed5 Mon Sep 17 00:00:00 2001 From: Dominic Taylor Date: Mon, 13 Feb 2017 14:58:46 +0000 Subject: [PATCH 1/6] fixed bug codeceptjs is not defined --- lib/codecept.js | 4 ++++ lib/helper/Nightmare.js | 11 +++++++++-- lib/recorder.js | 43 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/lib/codecept.js b/lib/codecept.js index a7664ea45..7865c3e0b 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -1,5 +1,9 @@ 'use strict'; + +global.nightmagePageIsReady = true; + +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/Nightmare.js b/lib/helper/Nightmare.js index 01db4085d..c8337b27f 100644 --- a/lib/helper/Nightmare.js +++ b/lib/helper/Nightmare.js @@ -189,11 +189,18 @@ class Nightmare extends Helper { _startBrowser() { this.browser = this.Nightmare(this.options); - this.browser.on('dom-ready', () => this._injectClientScripts()); + // when page has started to load set out flag to false to force the next test to wait until flag is true + this.browser.on('did-start-loading', () => { + global.nightmagePageIsReady = 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(); + global.nightmagePageIsReady = true; + }); this.browser.on('console', (type, message) => { this.debug(`${type}: ${message}`); }); - if (this.options.windowSize) { let size = this.options.windowSize.split('x'); return this.browser.viewport(parseInt(size[0]), parseInt(size[1])); diff --git a/lib/recorder.js b/lib/recorder.js index d6d976d66..9ac9518d3 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -97,7 +97,48 @@ module.exports = { } tasks.push(taskName); log(currentQueue() + `Queued | ${taskName}`); - return promise = promise.then(fn); + + // ensure that the page is loaded and the code has been injected by nightmare + var wait = function(){ + + return new Promise(function(resolve) { + + // when the flag is set to true we can run the test + if(global.nightmagePageIsReady){ + + resolve(); + + } + else { + + // Check for the page to be loaded for n number of times. + var checkTimes = 50; + + // poll every n ms to see if page has loaded + var check = setInterval(() => { + + // the page is now ready + if(global.nightmagePageIsReady){ + 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); + throw new Error("Codecept failed to inject codecept.js"); + } + + }, 100); + + } + + }); + + }; + + return promise = promise.then(wait).then(fn); }, catch(customErrFn) { From c89837599878bcdbca7474388caf33ca28cb4090 Mon Sep 17 00:00:00 2001 From: Dominic Taylor Date: Tue, 14 Feb 2017 09:51:40 +0000 Subject: [PATCH 2/6] Revert "fixed bug codeceptjs is not defined" This reverts commit 7ee9e680df9f8529de50c78fa26473a792d3f8cb. --- lib/codecept.js | 3 --- lib/helper/Nightmare.js | 11 ++--------- lib/recorder.js | 43 +---------------------------------------- 3 files changed, 3 insertions(+), 54 deletions(-) diff --git a/lib/codecept.js b/lib/codecept.js index 7865c3e0b..321c97398 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -1,8 +1,5 @@ 'use strict'; - -global.nightmagePageIsReady = true; - let Mocha = require('mocha/lib/mocha'); let fsPath = require('path'); let readFileSync = require('fs').readFileSync; diff --git a/lib/helper/Nightmare.js b/lib/helper/Nightmare.js index c8337b27f..01db4085d 100644 --- a/lib/helper/Nightmare.js +++ b/lib/helper/Nightmare.js @@ -189,18 +189,11 @@ class Nightmare extends Helper { _startBrowser() { this.browser = this.Nightmare(this.options); - // when page has started to load set out flag to false to force the next test to wait until flag is true - this.browser.on('did-start-loading', () => { - global.nightmagePageIsReady = 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(); - global.nightmagePageIsReady = true; - }); + this.browser.on('dom-ready', () => this._injectClientScripts()); this.browser.on('console', (type, message) => { this.debug(`${type}: ${message}`); }); + if (this.options.windowSize) { let size = this.options.windowSize.split('x'); return this.browser.viewport(parseInt(size[0]), parseInt(size[1])); diff --git a/lib/recorder.js b/lib/recorder.js index 9ac9518d3..d6d976d66 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -97,48 +97,7 @@ module.exports = { } tasks.push(taskName); log(currentQueue() + `Queued | ${taskName}`); - - // ensure that the page is loaded and the code has been injected by nightmare - var wait = function(){ - - return new Promise(function(resolve) { - - // when the flag is set to true we can run the test - if(global.nightmagePageIsReady){ - - resolve(); - - } - else { - - // Check for the page to be loaded for n number of times. - var checkTimes = 50; - - // poll every n ms to see if page has loaded - var check = setInterval(() => { - - // the page is now ready - if(global.nightmagePageIsReady){ - 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); - throw new Error("Codecept failed to inject codecept.js"); - } - - }, 100); - - } - - }); - - }; - - return promise = promise.then(wait).then(fn); + return promise = promise.then(fn); }, catch(customErrFn) { From 078423cbdfd09e4eee5130ace4a60cb2e029b7cd Mon Sep 17 00:00:00 2001 From: Dominic Taylor Date: Tue, 14 Feb 2017 09:58:51 +0000 Subject: [PATCH 3/6] refectered - removed global ref, fixed bug using other helpers --- lib/actor.js | 34 ++++++++++++++++++++++++++++++++++ lib/helper.js | 2 ++ lib/helper/Nightmare.js | 8 ++++++++ 3 files changed, 44 insertions(+) diff --git a/lib/actor.js b/lib/actor.js index 76be1965c..1e29285e5 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) => { + + if (step.helper.isReady || step.helper.isReady === undefined) { + resolve(); + } else { + + // Check for the page to be loaded for n number of times. + var checkTimes = 50; + + // 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); + throw new Error(`${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/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..dc13b707a 100644 --- a/lib/helper/Nightmare.js +++ b/lib/helper/Nightmare.js @@ -189,6 +189,14 @@ class Nightmare extends Helper { _startBrowser() { this.browser = this.Nightmare(this.options); + 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('dom-ready', () => this._injectClientScripts()); this.browser.on('console', (type, message) => { this.debug(`${type}: ${message}`); From c4b917faed077f5c581af21971cbeb8c7cccb877 Mon Sep 17 00:00:00 2001 From: Dominic Taylor Date: Tue, 14 Feb 2017 10:02:18 +0000 Subject: [PATCH 4/6] removed duplicate client inject scripts --- lib/helper/Nightmare.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/helper/Nightmare.js b/lib/helper/Nightmare.js index dc13b707a..7b854388a 100644 --- a/lib/helper/Nightmare.js +++ b/lib/helper/Nightmare.js @@ -197,7 +197,6 @@ class Nightmare extends Helper { this._injectClientScripts(); this.isReady = true; }); - this.browser.on('dom-ready', () => this._injectClientScripts()); this.browser.on('console', (type, message) => { this.debug(`${type}: ${message}`); }); From 28e44d43c5b2c0c7b3548813799bfee8aefc9566 Mon Sep 17 00:00:00 2001 From: Dominic Taylor Date: Thu, 18 May 2017 11:05:44 +0100 Subject: [PATCH 5/6] Ensure tests fail if codecept is not injected & add increase time to 100 seconds for codecept.js to be injected --- lib/actor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/actor.js b/lib/actor.js index 1e29285e5..619332b35 100644 --- a/lib/actor.js +++ b/lib/actor.js @@ -64,7 +64,7 @@ function recordStep(step, args) { } else { // Check for the page to be loaded for n number of times. - var checkTimes = 50; + var checkTimes = 100; // poll every n ms to see if page has loaded var check = setInterval(() => { @@ -79,7 +79,7 @@ function recordStep(step, args) { // If check times is below 0 then throw an error as the page is unable to load if(checkTimes < 0){ clearInterval(check); - throw new Error(`${step.helperMethod} failed to inject codecept.js`); + reject(`${step.helperMethod} failed to inject codecept.js`); } }, 100); From 43903dbc711c10a9dc16f873235978adeb5c6a45 Mon Sep 17 00:00:00 2001 From: Dominic Taylor Date: Thu, 18 May 2017 14:34:15 +0100 Subject: [PATCH 6/6] fixed misseing reject function --- lib/actor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/actor.js b/lib/actor.js index 619332b35..ae7061f72 100644 --- a/lib/actor.js +++ b/lib/actor.js @@ -57,7 +57,7 @@ function recordStep(step, args) { // check to ensure browser is loaded recorder.add('wait-for-browser-ready', function(){ - return new Promise((resolve) => { + return new Promise((resolve, reject) => { if (step.helper.isReady || step.helper.isReady === undefined) { resolve();