Use JSON & Typescript file as test data source for protractor

Every moment an automation engineer think to make the framework more flexible, using an external source file as test data is one step towards making your framework more flexible, so that you can change your test data any time without changing your script.

Problem

If you do not maintain an external source file for test data and hard code the values you use in your script, so when you want different set of data, then you need to change in each script file if you want to change the data before running.

Solution

If you have an external source file, then you can read the file, basically it’s a key value pair, just read the values by using respective key, so you need not to go each script file and change the hard coded data.

We have several ways to achieve this, we can use excel sheet, json file, a typescript file etc..

in this post, we will see how to use json and ts file as external test data source.

Watch details here –

 

Code base

Find the code sample on ProtractorPOM github 

Pull the project to your local drive and open in VSCode to practice. (Read the readme file for the Protractor setup)

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

3 Responses

  1. Utsav says:

    Hi, I Found this article helpful, Could you please add example for run time text fetching and storing the value in variable and then used that variable value in some other run time scenario.
    condition will be if same variable key is already existing then it should overwrite the key value . And actually rewrite the new data with same variable key.

  2. sunilpatro1985 says:

    Hi Utsav,

    Yes, you can update the json data, but can you let us know a scenario where you need to update the json data especially test data.
    You can update the json data like this –
    “FIle – readjson.js”
    var details = require(‘fullJsonFIlePath.json’);
    console.log(details.customers);
    details.customers.firstname = “ASDR”;
    console.log(details.customers);

    To run the code, open terminal and enter node readjson.js
    Output –
    { firstname: ‘ABC’, lastname: ‘XYZ’, postalcode: ‘12345’ }
    { firstname: ‘ASDR’, lastname: ‘XYZ’, postalcode: ‘12345’ }

    Hope that helps!

  3. Utsav says:

    Thanks for the help..
    But from this method..if I look back my json data file. I will see the same values :-
    { firstname: ‘ABC’, lastname: ‘XYZ’, postalcode: ‘12345’ }
    It won’t overwrite value in firstname.
    I have to store run time text and then I have to use that text.
    My scenario:-1
    1.U r on Google page
    2.Get the text of of Gmail.
    My scenario:-2
    1. U r on Google page
    2. Now in search box ..I have to type same value which we get the scenario 1 of 2nd step (Gmail).

Leave a Reply

Your email address will not be published. Required fields are marked *