One common problem when writing some e2e tests is how to test one feature multiple times with different sets of data. In many other similar tools (TestCafe, Cypress, perhaps more) and many not-so-similar tools (Robot Framework, pytest, ...) there're ways to define data-driven approach to testing other than simply iterating over a collection. An example of this could be from TestCafe:
testData.credentials.forEach(credentials => {
test
.meta({ author: 'Pavel Saman', creationDate: '27/05/2020',
env: process.env.TESTCAFE_ENV, url: baseUrl
})
('Log Into User Account', async t => {
await LogIn.logIn(credentials.username, credentials.password);
await t
.expect(PageMsg.pageMsg.innerText).eql(PageMsg.successfulLogIn)
.expect(Selector(ProfileForm.inputObj.name.input).value).eql(credentials.name);
});
});
In nightwatch.js, there's this npm package nightwatch-data-driven, which looks promising, but when I tried it, it most likely doesn't work with the current version of nightwatch.js. When I follow the example on the npm page, no tests are executed and it gives "Cannot use import statement outside a module" error. Others have described the very same problem on the github page of the project. The last commit in the repository is from May last year, so the date doesn't suggest there's much activity on this project either.
Another approach is, as I mentioned, simply iterating over a collection:
const testData = require('../Resources/userData.json');
// ...
// ...
'Change User Data': (browser) => {
for (data of testData.userData) {
browser.page.profileHomePage()
.section.input
.fillInUserData(data.name, data.surname, data.street, data.phone, data.city, data.zip)
.click('@saveButton');
browser.page.success()
.expect.element('@successEle').text.to.equal('Údaje byly uloženy!');
};
}
// ...
But this doesn't really look like a data-driven approach. It's true I'm driving tests based on data. But when run, reports will show I've run only a single test case. No matter how many elements I had in my data structure. That is not practical. Some other tools I know can in one way or the other parametrize test function in a way that when run on different sets of data, it will look like multiple test cases (in e.g. reports).
How do I define test cases in a data-driven approach in nightwatch.js?
To summarise what I've tried:
- searching on the web, official docs, here, ...
- nightwatch-data-driven npm package and experimenting with it
- define test cases in a similar way as I did in TestCafe
import
s already? If not, try usingrequire
instead.forEach
(like in TestCafe or Cypress) and with simple for (like I have just outside of the test case), and both options give syntax error withit nightwatch. It doesn't seem like a solution. How else would you do it then?