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();
});
},
};
beforeEach() is run before each test in a describe. afterEach() is run after each test in a describe.28-Jan-2014.
beforeEach(fn) # This is often useful if you want to reset some global state that will be used by many tests. Here the beforeEach ensures that the database is reset for each test. If beforeEach is inside a describe block, it runs for each test in the describe block.
The beforeEach code block runs before each unit test and re-initializes the props that will be passed to the custom Block to a default, empty state.
Just like the name suggests, it runs before each test in your test suite. For example, the beforeEach method will run four times in a suite with four tests. Each execution will happen just before the actual test function.
Of course there is! Just make use of the notorious Nightwatch 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:
/nightwatch/lib/runner/testcase.js
);/nightwatch/lib/runner/testsuite.js
);/hooks
folder (path: /nightwatch/lib/testsuite/hooks/*.js
);Still, a compromise can be made here! Just try to find the common, recurring steps/instructions from your before
, after
, etc. hooks and extract that logic inside a /custom_commands
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With