From b53734dcf9cf879976a23b17b3e4a5db970616a2 Mon Sep 17 00:00:00 2001 From: NightHawk14 Date: Thu, 21 Jul 2016 15:52:16 +1000 Subject: [PATCH 1/2] feat(single-browser-instance): use a single browser instance for each suite for protractor/selenium tests --- lib/helper.js | 18 ++++++++++++++++++ lib/helper/Protractor.js | 8 ++++++-- lib/helper/SeleniumWebdriver.js | 7 ++++--- lib/interfaces/codeceptjs.js | 22 +++++++++------------- lib/listener/helpers.js | 8 ++++++++ test/unit/helper/Nightmare_test.js | 2 +- test/unit/helper/Protractor_test.js | 7 ++++++- test/unit/helper/SeleniumWebdriver_test.js | 12 ++++++------ 8 files changed, 58 insertions(+), 26 deletions(-) diff --git a/lib/helper.js b/lib/helper.js index a5dda47c5..e667efd9e 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -81,6 +81,24 @@ class Helper { } + /** + * Hook executed before each suite + * + * @param suite + */ + _beforeSuite() { + + } + + /** + * Hook executed after each suite + * + * @param suite + */ + _afterSuite() { + + } + /** * Access another configured helper: `this.helpers['AnotherHelper']` */ diff --git a/lib/helper/Protractor.js b/lib/helper/Protractor.js index b416ffe41..b90d1d455 100644 --- a/lib/helper/Protractor.js +++ b/lib/helper/Protractor.js @@ -112,13 +112,17 @@ class Protractor extends SeleniumWebdriver { } _before() { - this.browser = this.driverProvider.getNewDriver(); this.amInsideAngularApp(); this.context = this.options.rootElement; return this.browser; } - _after() { + _beforeSuite() { + this.browser = this.driverProvider.getNewDriver(); + return this.browser; + } + + _afterSuite() { return this.browser.quit(); } diff --git a/lib/helper/SeleniumWebdriver.js b/lib/helper/SeleniumWebdriver.js index c26ddca7b..5f1a1ac00 100644 --- a/lib/helper/SeleniumWebdriver.js +++ b/lib/helper/SeleniumWebdriver.js @@ -101,11 +101,12 @@ class SeleniumWebdriver extends Helper { ]; } - _before() { - return this.browser = this.browserBuilder.build(); + _beforeSuite() { + this.browser = this.browserBuilder.build(); + return this.browser; } - _after() { + _afterSuite() { return this.browser.quit(); } diff --git a/lib/interfaces/codeceptjs.js b/lib/interfaces/codeceptjs.js index 4c275eb16..264d874bd 100644 --- a/lib/interfaces/codeceptjs.js +++ b/lib/interfaces/codeceptjs.js @@ -22,20 +22,10 @@ var escapeRe = require('escape-string-regexp'); */ module.exports = function (suite) { var suites = [suite]; - suite.on('pre-require', function (context, file, mocha) { var common = require('mocha/lib/interfaces/common')(suites, context); - common.before('codeceptjs event', function () { - event.dispatcher.emit(event.suite.before); - }); - - common.after('codeceptjs event', function () { - event.dispatcher.emit(event.suite.after); - }); - - // create dispatcher context.BeforeAll = common.before; @@ -48,16 +38,22 @@ module.exports = function (suite) { * and/or tests. */ - context.Feature = function (title) { + context.Feature = function (title) { if (suites.length > 1) { suites.shift(); - } + } var suite = Suite.create(suites[0], title); suite.file = file; suite.timeout(0); suites.unshift(suite); suite.beforeEach('codeceptjs.before', scenario.setup); - suite.afterEach('finialize codeceptjs', scenario.teardown); + suite.afterEach('finialize codeceptjs', scenario.teardown); + suite.beforeAll('codeceptjs.beforeSuite', function() { + event.dispatcher.emit(event.suite.before, suite); + }); + suite.afterAll('codeceptjs.afterSuite', function() { + event.dispatcher.emit(event.suite.after, suite); + }); return suite; }; diff --git a/lib/listener/helpers.js b/lib/listener/helpers.js index 2912a2aa0..56d29d47f 100644 --- a/lib/listener/helpers.js +++ b/lib/listener/helpers.js @@ -24,6 +24,14 @@ event.dispatcher.on(event.all.before, function () { runHelpersHook('_init'); }); +event.dispatcher.on(event.suite.before, function () { + runHelpersHook('_beforeSuite'); +}); + +event.dispatcher.on(event.suite.after, function () { + runHelpersHook('_afterSuite'); +}); + event.dispatcher.on(event.test.before, function () { runAsyncHelpersHook('_before'); }); diff --git a/test/unit/helper/Nightmare_test.js b/test/unit/helper/Nightmare_test.js index 86bcf5cc6..c9b9a3598 100644 --- a/test/unit/helper/Nightmare_test.js +++ b/test/unit/helper/Nightmare_test.js @@ -93,4 +93,4 @@ describe('Nightmare', function () { }); }); -}); \ No newline at end of file +}); diff --git a/test/unit/helper/Protractor_test.js b/test/unit/helper/Protractor_test.js index c84583aec..7c260ccba 100644 --- a/test/unit/helper/Protractor_test.js +++ b/test/unit/helper/Protractor_test.js @@ -33,6 +33,11 @@ describe('Protractor', function() { global.codecept_dir = path.join(__dirname, '../../data'); I = new Protractor({url: site_url}); I._init(); + I._beforeSuite(); + }); + + after(() => { + return I._afterSuite(); }); beforeEach(() => { @@ -439,4 +444,4 @@ describe('Protractor', function() { }); }); }); -}); \ No newline at end of file +}); diff --git a/test/unit/helper/SeleniumWebdriver_test.js b/test/unit/helper/SeleniumWebdriver_test.js index 159b7ad85..06cbeea17 100644 --- a/test/unit/helper/SeleniumWebdriver_test.js +++ b/test/unit/helper/SeleniumWebdriver_test.js @@ -27,15 +27,15 @@ describe('SeleniumWebdriver', function () { browser: 'firefox' }); I._init(); + browser = I._beforeSuite(); }); - beforeEach(function() { - webApiTests.init({ I, site_url}); - return browser = I._before(); + after(function() { + I._afterSuite(); }); - afterEach(() => { - return I._after(); + beforeEach(function() { + webApiTests.init({ I, site_url}); }); describe('open page : #amOnPage', () => { @@ -106,4 +106,4 @@ describe('SeleniumWebdriver', function () { }); }); -}); \ No newline at end of file +}); From 5981873857886b4a1a443b697acc35b5bdc09bb9 Mon Sep 17 00:00:00 2001 From: NightHawk14 Date: Thu, 21 Jul 2016 15:56:15 +1000 Subject: [PATCH 2/2] docs(helpers): add afterSuite and beforeSuite hook to documentation helper --- docs/helpers.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/helpers.md b/docs/helpers.md index 4c3df8744..e6d552339 100644 --- a/docs/helpers.md +++ b/docs/helpers.md @@ -149,6 +149,8 @@ Implement corresponding methods to them. * `_before` - before a test * `_beforeStep` - before each step * `_afterStep` - after each step +* `_beforeSuite` - before each suite (Protractor and SeleniumWebdriver only) +* `_afterSuite` - after each suite (Protractor and SeleniumWebdriver only) Each implemented method should return a value as they will be added to global promise chain as well.