protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Include test data file in protractor

Open priyabrat1801 opened this issue 12 years ago • 15 comments

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.

priyabrat1801 avatar Jun 26 '14 00:06 priyabrat1801

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

hankduan avatar Jun 26 '14 00:06 hankduan

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.

priyabrat1801 avatar Jun 26 '14 16:06 priyabrat1801

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.

elgalu avatar Jun 26 '14 16:06 elgalu

Thanks.This worked like a charm.

priyabrat1801 avatar Jun 26 '14 22:06 priyabrat1801

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?

kalyan542 avatar Nov 20 '14 00:11 kalyan542

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...

sallojusuresh avatar Nov 21 '14 04:11 sallojusuresh

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

kalyan542 avatar Nov 21 '14 16:11 kalyan542

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.

nattimorgan avatar Feb 09 '15 16:02 nattimorgan

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');

urbugly avatar May 27 '15 15:05 urbugly

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.

priyabrat1801 avatar May 28 '15 06:05 priyabrat1801

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 avatar Jul 01 '15 23:07 sautomation

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

nagadinesh49 avatar Jul 08 '15 20:07 nagadinesh49

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?

pavangkumar avatar Aug 20 '15 11:08 pavangkumar

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.

bhamaresanket avatar Feb 10 '16 01:02 bhamaresanket

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"
    }

]

gksangepu avatar Apr 11 '19 21:04 gksangepu