5

Data Driven testing is an important aspect of writing automated test cases for any tool. I have been experimenting with testcafe lately and haven't been able to find a convincing way of doing data-driven tests i.e. executing a test for different inputs.

I came across this example: https://testcafe-discuss.devexpress.com/t/multiple-execution-of-one-test-with-different-data/219 but in the above example, we are dealing with different login usernames as inputs. If I imagine a scenario where I have to check a list of elements appear on the page or not, I would surely have some steps leading to the validation; in which case I may not want to execute the leading steps each time a new input is passed. In the above example looks like the input is at a test case level and not at a test step level because we put the test case inside the for loop and therefore all the validation/navigational points will be executed whether I want to repeat them or not

Since, I am new to testcafe, and going over scattered documentation, my question is - for data-driven testing is that the only approach we have in test cafe? or there is something more convincing, non-verbose approach in testcafe- if yes could someone point me to the documentation for it?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
Monnie_tester
  • 439
  • 1
  • 6
  • 20

2 Answers2

3

The main concept of the data-driven testing is that you pass some data contained parameters and test the expected values to the.

The example provided in the Multiple execution of one test with different data topic's comment is a good start point:

const users = [
    { login: 'System', password: 'System' }, { login: 'Admin', password: 'Admin' }
]
for (let i = 0; i < users.length; i++) {
    let user = users[i];    test(`Login with user '${user.login}'`, async t => {
        await t.typeText(page.login.userEdit, user.login);
        // ...
    });
}

Next, you may be required to load your test data from a database, a csv file or anything else. In this case, you can use an appropriate standard Node.js module (see FAQ).

To give any further recommendations, please clarify your requirements and the task you are trying to accomplish in greater detail. Also, I've created an issue in the TestCafe repository to extend its documentation with an example of the data-driven testing.

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
2

The concept of step level does not exist in TestCafe. You have only two levels : the fixture level and the test level. If you want to do data-driven testing at the step level, you should have a look to the BDD frameworks that integrates with TestCafe.

hdorgeval
  • 3,000
  • 8
  • 17
  • I have experimented with couple of BDD integration with testCafe frameworks but failed each time to even launch a testcase. I will post exact issue, it will be great if you can help. However, on the forums, I came across articles that said BDD as a feature is still in works and the custom frameworks that exist at this point have their own kinks and may not work. Could you please clarify if these frameworks are usable? These are ones, I am talking about 1) https://github.com/rquellh/testcafe-cucumber and https://github.com/kiwigrid/gherkin-testcafe – Monnie_tester Jan 01 '19 at 18:32
  • @Monnie_tester: both repo are great, but you should be aware that there is no official support by the TestCafe Team (at the time of writing, this may change in the future) for CucumberJS integration. In CucumberJS, data-driven testing is implemented by using the `Scenario Outline` (see [gherkin Reference](https://docs.cucumber.io/gherkin/reference/)). At the end, `Scenario Outline` is converted to as many TestCafe tests as examples given in the Scenario - which is the same as doing a simple `for` loop as explained by @AlexSkorkin. I suggest you follow his way because it is the simplest path – hdorgeval Jan 01 '19 at 20:08