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)
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.
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!
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).
Hi Sunil,
Firstly all your videos are awesome and gives more insight. Thank you for great work.
I implemented your example and it working absolutely fine. I was wondering is there a way to loop my IT test with multiple test data.
For Example:
In your video you are adding a single customer. Can we add 2 or 3 customers by running same IT by giving an Array of test data in the JSON file?
Hello, glad that these tutorials helps!
Jasmine do have a data provider like we have in testNG, here is the library that you can install and use, this link do have few examples to get started with
https://www.npmjs.com/package/jasmine-data-provider
Hi,
Thank you for the response and the link. It’s definitely go way to create a loop around the IT but I wanted to have more control over the data i’m passing into the IT. I found this article on medium. I just wanted to know your thoughts on this: https://medium.com/@nicklee1/making-your-protractor-tests-data-driven-e3c9e2a5e4e7
Are there any drawbacks going over this approach over jasmine data provider. I didn’t got it completely working with my Typescript.