Skip to content

Instantly share code, notes, and snippets.

@paulbjensen
Last active June 18, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulbjensen/5678129755587594bb1dc53e25e6007b to your computer and use it in GitHub Desktop.
Save paulbjensen/5678129755587594bb1dc53e25e6007b to your computer and use it in GitHub Desktop.
The hooks.js file for Cucumber, part of an article on Medium
// Dependencies
const { After, Before, AfterAll } = require('cucumber');
const scope = require('./support/scope');
Before(async () => {
// You can clean up database models here
});
After(async () => {
// Here we check if a scenario has instantiated a browser and a current page
if (scope.browser && scope.context.currentPage) {
// if it has, find all the cookies, and delete them
const cookies = await scope.context.currentPage.cookies();
if (cookies && cookies.length > 0) {
await scope.context.currentPage.deleteCookie(...cookies);
}
// close the web page down
await scope.context.currentPage.close();
// wipe the context's currentPage value
scope.context.currentPage = null;
}
});
AfterAll(async () => {
// If there is a browser window open, then close it
if (scope.browser) await scope.browser.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment