How to read data from JSON file, Then Pass these values on web application form?



  • Steps :

    Can you suggest the approach or share some sample script

    1.Read data from JSON file - Method to Provide Data (JsonReader.getdata) - Get file location & File Name

    2.In @Test(dataprovider='JSON File Data') -> Pass All values as Method Argument - Send all values one by One ( Xpath , Elementname , Sendkeys) - Fill the UI Header form with Values from JSON and Clickon Save

    3.Write it in Excel file



  • 1 - Read data from JSON

    Google has a very good Java library to parse JSON in a structured way: GSON

    The types you would use the most are JSONObject and JSONElement.

    2 - Use the data in a test

    I would suggest having in your dataprovide only the name of the JSON file. In the test, you can call a factory that would read the JSON file and generate a POJO object.

    E.g.,

    @Test(dataprovider='JSON File Data')
    public .... myTest(String jsonFileName) {
       TestDataModel testData = TestDataFactory.createTestDataFromJSON(jsonFileName);
       fillUserName(testData.getUserName());
       fillUserName(testData.getPassword());
       ...
       ExcelLogger.logTestDataInExcel(testData);
    }
    

    TestDataModel will represent the JSON file:

    JSON:

    {
      "userName": "Mario",
      "password": "Luigi"
    }
    

    POJO with a bit of Lombok:

    public class TestDataModel {
    
    @Getter @Setter
    public final String userName;
    
    @Getter @Setter
    public final String password;
    
    }
    

    TestDataFactory and ExcelLogger will do the dirty work of interacting with the file system and performing the serialization/deserialization part.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2