So, I have a globals.js file where I have mentioned beforeEach and afterEach, but what I could understand from this link Nightwatch Globals, the beforeEach and afterEach are called once before and after a Test Suite (A single Js file). But in my framework I have multiple test cases in the a single js file (or Test Suite) and I want to call beforeEach and afterEach before and after every test case. Anyway to achieve that? Below is my globals.js file:
module.exports = {
asyncHookTimeout: 40000,
beforeEach: function (browser, done) {
// browser.maximizeWindow();
// browser.deleteCookies();
browser.perform(function () {
console.log('Inside BeforeEach');
done();
});
}
afterEach: function (browser, done) {
browser.end(function () {
console.log("Inside After Each");
done();
});
},
};
So, I have a globals.js file where I have mentioned beforeEach and afterEach, but what I could understand from this link Nightwatch Globals, the beforeEach and afterEach are called once before and after a Test Suite (A single Js file). But in my framework I have multiple test cases in the a single js file (or Test Suite) and I want to call beforeEach and afterEach before and after every test case. Anyway to achieve that? Below is my globals.js file:
module.exports = {
asyncHookTimeout: 40000,
beforeEach: function (browser, done) {
// browser.maximizeWindow();
// browser.deleteCookies();
browser.perform(function () {
console.log('Inside BeforeEach');
done();
});
}
afterEach: function (browser, done) {
browser.end(function () {
console.log("Inside After Each");
done();
});
},
};
Share
Improve this question
asked Dec 14, 2018 at 15:43
Alapan DasAlapan Das
18.5k5 gold badges37 silver badges62 bronze badges
1 Answer
Reset to default 8Of course there is! Just make use of the notorious Nightwatch test hooks.
- If you want to filter your test-suites, then as you pointed out, we'll use the global test hooks.
- If you want to filter your test-cases, then we'll use the test hooks
Example (your test-file should look like this):
module.exports = {
before(browser) {
// > this will get run only ONCE, before all the tests <
},
beforeEach(browser) {
// > this will get run before every test case <
}
tags: ['your', 'tags', 'go', 'here'],
'Test Case No.1': (browser) => {
// > this test does something here <
},
'Test Case No.2': (browser) => {
// > this test does something else here <
},
'Test Case No.3': (browser) => {
// > this test does something else here <
},
afterEach(browser) {
// > this will get run after every test case <
},
after(browser) {
// > this will get run ONCE, after all tests have run <
}
};
Lastly, quoting the DOCs:
The before and after will run before and after the execution of the test suite respectively (in our case, the test-file), while beforeEach and afterEach are ran before and after each test case (test step).
LE: What @AlapanDas wants is to custom-tailor the way the Nightwatch test-runner handles test-level hooks. This is of course doable, but dirty. You have to re-write the hooking logic from the following files:
[email protected]:
- testcase.js (path:
/nightwatch/lib/runner/testcase.js
); - testsuite.js (path:
/nightwatch/lib/runner/testsuite.js
);
[email protected]:
- every {hookName}.js file from the
/hooks
folder (path:/nightwatch/lib/testsuite/hooks/*.js
);
Still, a promise can be made here! Just try to find the mon, recurring steps/instructions from your before
, after
, etc. hooks and extract that logic inside a /custom_mands
file. That would condense your test files, as well as decoupling the login from your hooks. On the long run, this will also grant the advantage of a single-point-of-change when maintaining the hooks.