Reading Data from JSON File Using Protractor



Reading Data from JSON File Using Protractor

As we all are aware, data-driven testing is one of the important aspects we need to take care of while designing any testing framework. There could be different approaches to Data-Driven Testing like

  •         Reading Test Data from a properties file
  •         Reading Test Data from JSON File
  •         Reading Test Data from excel

We already have seen in another blog, how to read data from a properties file and use it in our automation framework. Please refer to this link to know about it.

In this blog, we will learn how to read data from JSON File and use them in our protractor automation test.

Before proceeding further to this blog, I assume you have basic knowledge about protractor and how to find XPath and execute protractor automation tests.


For this purpose lets first create one JSON file in our project lets name it as object.json


In this JSON File I will store my Application URL along with few data locator
Please Note that JSON file will always store data in Key-value pair inside { } braces for e.g “ElementName”:”Xpath”

Below is sample JSON File


Now to Read data from this JSON File inside our automation script we need to provide the path of this JSON File using require and then we can read data from this JSON File using JSONfileName.ElementName and can execute our automation script

Below is my runner class to execute this automation scripts



SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post
Purushotham
June 6, 2019 at 3:04 PM

Thank you for the information Ankur. It's really informative, appreciate your efforts. Good Job!

Reply
avatar
September 11, 2019 at 10:37 PM

On the off chance that the identifiers of the two rooms are unique, open their regular entryway and bring together their identifiers and furthermore their rundowns. ExcelR Data Science Courses

Reply
avatar
ravali
October 30, 2019 at 3:40 PM

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
ExcelR data science course in mumbai

Reply
avatar
April 29, 2020 at 4:05 PM

no working E/launcher - Error: Error: Cannot find module '../Data/testdata.json'
Require stack:
- C:\Users\hemas\Documents\Protractor TypeScript\JSFile\stepDefination\steps.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\cucumber\lib\cli\index.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\cucumber\lib\index.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\protractor-cucumber-framework\lib\cucumberLoader.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\protractor-cucumber-framework\index.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\protractor\built\runner.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\protractor\built\launcher.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\protractor\built\cli.js
- C:\Users\hemas\Documents\Protractor TypeScript\node_modules\protractor\bin\protractor
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
at Function.Module._load (internal/modules/cjs/loader.js:687:27)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (C:\Users\hemas\Documents\Protractor TypeScript\stepDefination\steps.ts:11:14)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19)
at require (internal/modules/cjs/helpers.js:74:18)
at C:\Users\hemas\Documents\Protractor TypeScript\node_modules\cucumber\lib\cli\index.js:119:42
at Array.forEach ()
at Cli.getSupportCodeLibrary (C:\Users\hemas\Documents\Protractor TypeScript\node_modules\cucumber\lib\cli\index.js:119:22)
at Cli.run (C:\Users\hemas\Documents\Protractor TypeScript\node_modules\cucumber\lib\cli\index.js:141:37)
[16:00:39] E/launcher - Process exited with error code 100
npm ERR! code ELIFECYCLE
npm ERR! errno 100
npm ERR! protractor-typescript@1.0.0 cucumbertest: `protractor JSFile/cucumberconfig.js`
npm ERR! Exit status 100
npm ERR!
npm ERR! Failed at the protractor-typescript@1.0.0 cucumbertest script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\hemas\AppData\Roaming\npm-cache\_logs\2020-04-29T10_30

Reply
avatar
June 16, 2020 at 3:43 PM

This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post. buy instagram spread likes cheap

Reply
avatar
July 3, 2020 at 2:08 PM

Such a very useful article. Very interesting to read this article. I would like to thank you for the efforts you had made for writing this awesome article.
Data Science Course in Pune
Data Science Training in Pune

Reply
avatar
July 3, 2020 at 8:11 PM

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.
Data Science Institute in Bangalore

Reply
avatar
July 3, 2020 at 8:12 PM

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one.
Data Science Certification in Bangalore

Reply
avatar
July 5, 2020 at 10:13 AM

You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!
Data Science Course in Bangalore

Reply
avatar
July 5, 2020 at 10:14 AM

I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…
Data Science Training in Bangalore

Reply
avatar
July 9, 2020 at 4:51 PM

I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
Data Analytics Course in Pune
Data Analytics Training in Pune

Reply
avatar
July 9, 2020 at 5:14 PM

I am impressed by the information that you have on this blog. It shows how well you understand this subject.
Business Analytics Course in Pune
Business Analytics Training in Pune

Reply
avatar
July 31, 2020 at 11:10 PM

I am impressed by the information that you have on this blog. Thanks for Sharing
Ethical Hacking in Bangalore

Reply
avatar
August 7, 2020 at 11:10 AM

Thanks for sharing great information. I like your blog and highly recommend. We also offer best data science training in Hyderabaddata scientist courses

Reply
avatar