Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a single browser instance per suite for Protractor/Selenium helper #167

Merged
merged 2 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
18 changes: 18 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']`
*/
Expand Down
8 changes: 6 additions & 2 deletions lib/helper/Protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
7 changes: 4 additions & 3 deletions lib/helper/SeleniumWebdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
22 changes: 9 additions & 13 deletions lib/interfaces/codeceptjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};

Expand Down
8 changes: 8 additions & 0 deletions lib/listener/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/helper/Nightmare_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ describe('Nightmare', function () {
});
});

});
});
7 changes: 6 additions & 1 deletion test/unit/helper/Protractor_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -439,4 +444,4 @@ describe('Protractor', function() {
});
});
});
});
});
12 changes: 6 additions & 6 deletions test/unit/helper/SeleniumWebdriver_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -106,4 +106,4 @@ describe('SeleniumWebdriver', function () {
});
});

});
});