In my Selenium JS / Cucumber framework, I have Before()
& After()
functions that create & tear down my webdriver.
I have put them in my customSteps.js
file along with the other cucumber steps like so:
const { When, Then, Before, After } = require('@cucumber/cucumber')
const webdriver = require('selenium-webdriver')
let driver;
Before(() => {
driver = new webdriver.Builder()
.forBrowser('chrome')
.build();
})
After(() => {
driver.quit();
})
When('I visit {string}', {timeout: 2 * 5000}, async url => {
await driver.get(url)
});
Then('the page header {string} is displayed', async expectedPageHeader => {});
When('I click the {string} button', async text => {});
Then('I am brought to the {string} page', async text => {});
When('the intro header text is {string}', async expectedPageHeader => {});
When('the intro subheader text is {string}', async expectedSubHeader => {});
Here are my scripts
in package.json
:
"scripts": {
"test": "node_modules/.bin/cucumber-js -f @cucumber/pretty-formatter features/test.feature"
}
My client has told me that only the Given
, When
, & Then
steps should be in my customSteps.js
, & that Before()
& After()
should be in hooks.js
in a support
folder.
I have tried to find Selenium JavaScript solutions online that do this, but I haven't been able to find one yet.
Can someone please tell me if my approach is the better approach, or should I be putting Before()
& After()
in a hooks.js
file?
Also, some advice on how to do so would be appreciated, as those functions interact with my driver
variable, which is needed in the Given
step to visit the URL.
Finally, here's a picture of my current folder structure in case it's useful: