Skip to content

Commit 6b1361b

Browse files
committed
partial support for browser teardown strategy
1 parent 7c40725 commit 6b1361b

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.vscode
22
node_modules
33
package-lock.json
4-
. idea/
4+
.idea/

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var config = {
2828
sharedObjects: './shared-objects',
2929
reports: './reports',
3030
browser: 'chrome',
31+
browserTeardownStrategy: 'always',
3132
timeout: 15000
3233
};
3334

@@ -44,6 +45,7 @@ program
4445
.option('-p, --pageObjects <path>', 'path to page objects. defaults to ' + config.pageObjects, config.pageObjects)
4546
.option('-o, --sharedObjects [paths]', 'path to shared objects (repeatable). defaults to ' + config.sharedObjects, collectPaths, [config.sharedObjects])
4647
.option('-b, --browser <path>', 'name of browser to use. defaults to ' + config.browser, config.browser)
48+
.option('-k, --browser-teardown <optional>', 'browser teardown strategy after every scenario (always, clear, none). defaults to "none"', config.browserTeardownStrategy)
4749
.option('-r, --reports <path>', 'output path to save reports. defaults to ' + config.reports, config.reports)
4850
.option('-d, --disableLaunchReport [optional]', 'Disables the auto opening the browser with test report')
4951
.option('-j, --junit <path>', 'output path to save junit-report.xml defaults to ' + config.reports)
@@ -59,6 +61,7 @@ program.on('--help', function () {
5961

6062
// store browserName globally (used within world.js to build driver)
6163
global.browserName = program.browser;
64+
global.browserTeardownStrategy = program.browserTeardown;
6265

6366
// store Eyes Api globally (used within world.js to set Eyes)
6467
global.eyesKey = config.eye_key;

runtime/world.js

+38-16
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ function createWorld() {
118118

119119
// expose properties to step definition methods via global variables
120120
Object.keys(runtime).forEach(function (key) {
121+
if (key === 'driver' && browserTeardownStrategy !== 'always') {
122+
return;
123+
}
121124

122125
// make property/method available as a global (no this. prefix required)
123126
global[key] = runtime[key];
@@ -165,6 +168,23 @@ function importSupportObjects() {
165168
global.helpers = require('../runtime/helpers.js');
166169
}
167170

171+
function teardownBrowser(driver) {
172+
switch (browserTeardownStrategy) {
173+
case 'none':
174+
return new Promise(
175+
function(resolve, reject) {});
176+
case 'clear':
177+
return driver.manage().deleteAllCookies();
178+
default:
179+
// firefox quits on driver.close on the last window
180+
return driver.close().then(function () {
181+
if (browserName !== 'firefox'){
182+
return driver.quit();
183+
}
184+
});
185+
}
186+
}
187+
168188
// export the "World" required by cucumber to allow it to expose methods within step def's
169189
module.exports = function () {
170190

@@ -180,10 +200,11 @@ module.exports = function () {
180200
// create the driver and applitools eyes before scenario if it's not instantiated
181201
this.registerHandler('BeforeScenario', function (scenario) {
182202

183-
if (!global.driver || !global.eyes) {
203+
if (!global.driver) {
184204
global.driver = getDriverInstance();
205+
}
185206

186-
// TOOD: this all feels a bit hacky, needs rethinking...
207+
if (!global.eyes) {
187208
global.eyes = getEyesInstance();
188209
}
189210
});
@@ -214,7 +235,19 @@ module.exports = function () {
214235
fs.writeFileSync(junitOutputPath, xmlReport);
215236
}
216237

217-
done();
238+
console.log(browserTeardownStrategy);
239+
240+
if (browserTeardownStrategy !== 'always') {
241+
driver.close().then(function () {
242+
if (browserName !== 'firefox'){
243+
return driver.quit();
244+
}
245+
done();
246+
});
247+
}
248+
else {
249+
done();
250+
}
218251
});
219252

220253
// executed after each scenario (always closes the browser to ensure fresh tests)
@@ -224,14 +257,8 @@ module.exports = function () {
224257
return driver.takeScreenshot().then(function (screenShot) {
225258

226259
scenario.attach(new Buffer(screenShot, 'base64'), 'image/png');
227-
// firefox quits on driver.close on the last window
228-
return driver.close().then(function () {
229-
if (browserName !== 'firefox'){
230-
return driver.quit();
231-
}
232-
})
233-
.then(function() {
234260

261+
teardownBrowser(driver).then(function() {
235262
if (eyes) {
236263
// If the test was aborted before eyes.close was called ends the test as aborted.
237264
return eyes.abortIfNotClosed();
@@ -241,11 +268,6 @@ module.exports = function () {
241268
});
242269
});
243270
}
244-
// firefox quits on driver.close on the last window
245-
return driver.close().then(function () {
246-
if (browserName !== 'firefox'){
247-
return driver.quit();
248-
}
249-
})
271+
return teardownBrowser(driver);
250272
});
251273
};

0 commit comments

Comments
 (0)