0

Hi i'm having difficulty in reading data from Json file as I want to read different data from json file and pass it to 1 test case. Json file :

              {
              "allCaes":
                    {
                   "TestCase1":
                            {
                          "originInput": "",
                         "destinationInput": ""
                            },
                   "TestCase2":
                            {
                           "originInput": "",
                           "destinationInput": "",
 
                            },
                     }
                  }

My code: import { allCases } from '../abcData.json';

       Object.keys(allCases).forEach(data => {
       test('close selected origin', async ({ page }) => {
       const homepage = new abcHomePage(page);
       await homepage.goto();
       await homepage.closeSelectedOrigin();
       await homepage.enterOriginInput(allCases.TestCase1.originInput);
       await homepage.enterDestinationInput();
       await homepage.selectStartDate();
       await homepage.selectEndDate();

} I want to make it generic for all test data present in json file await homepage.enterOriginInput(allCases.TestCase1.originInput); dont want to specify testCase1

Nadia Ali
  • 1
  • 1
  • 2

1 Answers1

1

First you need an array in the json-file:

{
    "persons":[
        {
            "name":"Alex",
            "lastname":"Test"
        },
        {
            "name":"John",
            "lastname":"Test"
        }
    ]
}

And then use this code to import and read the data:

const { persons } = require('../data.json'); // or use: import { persons } from '../data.json';

persons.forEach((item, index) => {
  test(`Some text - Iteration: ${index+1}`, async ({ page }) => {
    await page.locator('#element').fill(persons[index].name);
    await page.locator('#element2').fill(persons[index].lastname);
  });
})
Test Auto
  • 11
  • 2