Skip to content

Commit 5eb0f39

Browse files
author
John Doherty
authored
Merge pull request #81 from kbytesys/browser-teardown
Browser teardown
2 parents 7c40725 + 0abd303 commit 5eb0f39

File tree

6 files changed

+84
-34
lines changed

6 files changed

+84
-34
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
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/

README.MD

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,20 @@ node ./node_modules/selenium-cucumber-js/index.js -s ./step-definitions
4141
### Options
4242

4343
```bash
44-
-h, --help output usage information
45-
-V, --version output the version number
46-
-s, --steps <path> path to step definitions. defaults to ./step-definitions
47-
-p, --pageObjects <path> path to page objects. defaults to ./page-objects
48-
-o, --sharedObjects [paths] path to shared objects (repeatable). defaults to ./shared-objects
49-
-b, --browser <path> name of browser to use. defaults to chrome
50-
-r, --reports <path> output path to save reports. defaults to ./reports
51-
-d, --disableLaunchReport disable the auto opening the browser with test report
52-
-j, --junit <path> output path to save junit-report.xml defaults to ./reports
53-
-t, --tags <tagName> name of tag to run
54-
-f, --featureFile <path> a specific feature file to run
55-
-x, --timeOut <n> steps definition timeout in milliseconds. defaults to 10 seconds
56-
-n, --noScreenshot disable auto capturing of screenshots when an error is encountered
44+
-h, --help output usage information
45+
-V, --version output the version number
46+
-s, --steps <path> path to step definitions. defaults to ./step-definitions
47+
-p, --pageObjects <path> path to page objects. defaults to ./page-objects
48+
-o, --sharedObjects [paths] path to shared objects (repeatable). defaults to ./shared-objects
49+
-b, --browser <path> name of browser to use. defaults to chrome
50+
-k, --browser-teardown <optional> browser teardown strategy after every scenario (always, clear, none). defaults to "always"
51+
-r, --reports <path> output path to save reports. defaults to ./reports
52+
-d, --disableLaunchReport disable the auto opening the browser with test report
53+
-j, --junit <path> output path to save junit-report.xml defaults to ./reports
54+
-t, --tags <tagName> name of tag to run
55+
-f, --featureFile <path> a specific feature file to run
56+
-x, --timeOut <n> steps definition timeout in milliseconds. defaults to 10 seconds
57+
-n, --noScreenshot disable auto capturing of screenshots when an error is encountered
5758
```
5859

5960
By default tests are run using Google Chrome, to run tests using another browser supply the name of that browser along with the `-b` switch. Available options are:
@@ -112,7 +113,16 @@ Feature: Searching for vote cards app
112113
Then I should see some results
113114
```
114115

115-
The browser automatically closes after each scenario to ensure the next scenario uses a fresh browser environment.
116+
### Browser teardown strategy
117+
118+
The browser automatically closes after each scenario to ensure the next scenario uses a fresh browser environment. But
119+
you can change this behavior with the "-k" or the "--browser-teardown" parameter.
120+
121+
Value | Description
122+
---------- | ---------------
123+
`always` | the browser automatically closes (default)
124+
`clear` | the browser automatically clears cookies, local and session storages
125+
`none` | the browser does nothing
116126

117127
### Step definitions
118128

@@ -270,6 +280,15 @@ helpers.getPseudoElementBeforeValue('body header');
270280

271281
// get the content value of a :after pseudo element
272282
helpers.getPseudoElementAfterValue('body header');
283+
284+
// clear the cookies
285+
helpers.clearCookies();
286+
287+
// clear both local and session storages
288+
helpers.clearStorages();
289+
290+
// clear both cookies and storages
291+
helpers.clearCookiesAndStorages('body header');
273292
```
274293

275294
### Visual Comparison

index.js

Lines changed: 3 additions & 0 deletions
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 "always"', 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;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"chromedriver": "^2.41.0",
4444
"commander": "2.9.0",
4545
"cucumber": "1.3.3",
46-
"cucumber-html-reporter": "2.0.3",
46+
"cucumber-html-reporter": "4.0.4",
4747
"cucumber-junit": "1.6.0",
4848
"electron": "^1.7.6",
4949
"electron-chromedriver": "^1.7.1",

runtime/helpers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,17 @@ module.exports = {
259259
}
260260

261261
return driver.executeScript(getAfterContentValue, cssSelector);
262+
},
263+
264+
clearCookies: function() {
265+
return driver.manage().deleteAllCookies();
266+
},
267+
268+
clearStorages: function() {
269+
return driver.executeScript('window.localStorage.clear(); window.sessionStorage.clear();')
270+
},
271+
272+
clearCookiesAndStorages: function() {
273+
return helpers.clearCookies().then(helpers.clearStorages());
262274
}
263275
};

runtime/world.js

Lines changed: 34 additions & 18 deletions
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,26 @@ function importSupportObjects() {
165168
global.helpers = require('../runtime/helpers.js');
166169
}
167170

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

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

183-
if (!global.driver || !global.eyes) {
206+
if (!global.driver) {
184207
global.driver = getDriverInstance();
208+
}
185209

186-
// TOOD: this all feels a bit hacky, needs rethinking...
210+
if (!global.eyes) {
187211
global.eyes = getEyesInstance();
188212
}
189213
});
@@ -214,7 +238,12 @@ module.exports = function () {
214238
fs.writeFileSync(junitOutputPath, xmlReport);
215239
}
216240

217-
done();
241+
if (browserTeardownStrategy !== 'always') {
242+
closeBrowser().then(() => done());
243+
}
244+
else {
245+
new Promise((resolve) => resolve(done()));
246+
}
218247
});
219248

220249
// executed after each scenario (always closes the browser to ensure fresh tests)
@@ -224,28 +253,15 @@ module.exports = function () {
224253
return driver.takeScreenshot().then(function (screenShot) {
225254

226255
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() {
234256

257+
return teardownBrowser().then(function() {
235258
if (eyes) {
236259
// If the test was aborted before eyes.close was called ends the test as aborted.
237260
return eyes.abortIfNotClosed();
238261
}
239-
240-
return Promise.resolve();
241262
});
242263
});
243264
}
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-
})
265+
return teardownBrowser();
250266
});
251267
};

0 commit comments

Comments
 (0)