Protractor: Include test data file in protractor

Created on 26 Jun 2014  Â·  15Comments  Â·  Source: angular/protractor

Is there a way i can put my test data in a json file and use that file in my specs to get the test data. I need to use a lot of hard coded data which i want to put inside the data file. I know karma has the functionality for unit tests but not sure of using in protractor. Any suggestions.

question

Most helpful comment

Hi Kalyan,

  1. create test data in json object format and save it as .json(example: testdata.json) in your project folder
  2. include that test data file in your test scripts pages by using 'var data=require('../testdata.json')"
  3. now you can directly read data using test data file reference variable 'data'

Example:
testdata.json:
{
"Name":"Json Test Data",
"Purpose":"Storing test data",
"Date":"11/21/2014"
}

Usage:
"data.Name" retrieved value is"Json Test Data"
"data.Purpose" retrieved value is "Storing test data"
"data.Date" retrieved value is "11/21/2014"

Hope it will help you...

All 15 comments

I think this is what you're looking for: https://github.com/angular/protractor/issues/620

My requirement is to read a json file to find the key value pairs based on the key and use the value in my tests. #620 talks about csv and parallel tests.

A simple require to the json file will do, i do this in a onPrepare function:

browser.params.users  = require('./shared.params.' + countryCode + '.json').users;

For example, i have a shared.params.uk.json with the hard-coded test data.

Thanks.This worked like a charm.

Hi,

I am looking for read a json file to find the key value pairs based on the key and use the value in my protractor tests.
I didn't understand the above mentioned example.Can you explain a bit more about this?

Hi Kalyan,

  1. create test data in json object format and save it as .json(example: testdata.json) in your project folder
  2. include that test data file in your test scripts pages by using 'var data=require('../testdata.json')"
  3. now you can directly read data using test data file reference variable 'data'

Example:
testdata.json:
{
"Name":"Json Test Data",
"Purpose":"Storing test data",
"Date":"11/21/2014"
}

Usage:
"data.Name" retrieved value is"Json Test Data"
"data.Purpose" retrieved value is "Storing test data"
"data.Date" retrieved value is "11/21/2014"

Hope it will help you...

Hi Suresh,
Thanks,This example is working fine.I will use the same in my specs.

Thanks Suresh, this helped me out big time man!!! I am now able to create my data driven tests in Protractor. One day, I hope to contribute something as helpful as this. Thanks.

I am attempting to create the data file that was referenced Suresh. However I am having issues with protractor when I got to execute. I am getting unexpected token. Something isn't correct in my syntax. What does the whole data.json look like.
My Example:

projectData.json:
{
"url":"https://google.com",
"projectCode":"5c5d3ca2",
"memberId":"1",
"externalId":"1"
}

Then I save this off and reference the json file.
var testdata = require('../data/projectdata.json');

Can you put the json in jsonlint to verify if it is valid or not. That will
give you idea on where the issue is.

Thanks.
On May 27, 2015 8:48 AM, "meneghia" [email protected] wrote:

I am attempting to create the data file that was referenced Suresh.
However I am having issues with protractor when I got to execute. I am
getting unexpected token. Something isn't correct in my syntax. What does
the whole data.json look like.
My Example:

projectData.json:
{
"url":"https://google.com",
"projectCode":"5c5d3ca2",
"memberId":"1",
"externalId":"1"
}

Then I save this off and reference the json file.
var testdata = require('../data/projectdata.json');

—
Reply to this email directly or view it on GitHub
https://github.com/angular/protractor/issues/978#issuecomment-105969012.

I followed Suresh's approach and It doesn't work for me either. My json is a valid file. I checked it in the json validator

{
"user1":"[email protected]",
"user2":"[email protected]"

}

In my test,

var diff = require('../helper/differentlogin.json');

  it('Should click on Login button', function() {

    browser.get('/#/');

    element(by.name('username')).sendKeys(diff.user1);
    element(by.name('password')).clear().sendKeys('pass');
    element(by.id("login"))..click();;

 //});
  });

});

I need to pass the different userID in the send keys and password is the same for all our test users. When i run it, I am getting declaration exception message

@sautomation : Can you give a try by referencing the values as diff[user1] or diff['user1'] inside sendkeys.

Thanks for Every one, Iam very new to protractor this helped me a lot and also can any one send the links to learn protractor?

Hi,
The solution from Suresh worked perfectly fine.
Next thing I need to try is write some text using protractor to the json file from which we read the values.
e.g.
If the original json file is this:
{
"user1":"[email protected]",
"user2":"[email protected]"
}

I want to add an extra field through protractor code to json file. So the file should be like.
{
"user1":"[email protected]",
"user2":"[email protected]",
"user3":"[email protected]"
}

Please help.

Is there a way to run the "it" for several times based on array size in the JSON.
My JSON file looks like this:

[{
        "titleText": "U1"
    },

    {
        "titleText": "U2"
    },
    {
        "titleText": "U3"
    }

]
Was this page helpful?
0 / 5 - 0 ratings