Warm tip: This article is reproduced from stackoverflow.com, please click
json automation automated-tests e2e-testing testcafe

Can Data Driven tests in TestCafe using JSON work with JSON having multiple attributes for each test

发布于 2020-03-27 15:44:57

I have plenty of data-driven tests already written in testcafe until now based on this example.

https://devexpress.github.io/testcafe/documentation/recipes/create-data-driven-tests.html

Has anyone attempted to do a JSON data-driven test w/testcafe with a JSON file like the sample below?

[
       {
            "testcasename": "Check for Rate Classes -1",
            "rateclasses": "{
                   "classname": "SC",
                   "classvalue": 1
             }
        },

]

This is the code I use to iterate through the JSON file. Now my dilemma is can there be a multi-level data set routine written?

dataSet.forEach(userdata => {
    test(`Enter '${userdata.testcasename}'`, async t => {
        my code here
    });
});

Would it look like

dataSet.forEach(userdata => {
    test(`Enter '${userdata.testcasename}'`, async t => {
              some code here for the 1st level attributes
               dataSet.forEach(userdatasubattributes => {
                some code here for the repeating attributes for each test case
                }

       });
});

Any pointers would be helpful.

UPDATE

found that the construct needs to work like this:

[
       {
            "testcasename": "Check for Rate Classes -1",
            "rateclasses": " [
             {
                   "classname": "SC",
                   "classvalue": 1
             }
          ] 
        }
]
Questioner
Anjana
Viewed
50
Anjana 2020-02-05 10:42

This is how I solved the problem:

dataSet.forEach(userdata => {
        test(`Enter '${userdata.testcasename}'`, async t => {  
            for(let i = 0, l = userdata.rateclasses.length; i < l; i++) {
                console.log ("Class Name", userdata.rateclasses[i].classname) 
                console.log ("Class Value", userdata.rateclasses[i].classvalue) 

            }
        });
    });