6

What I would like to achieve is for the Protractor tests to extract data from a separate data file (e.g CSV, JSON, etc) so that I could change the data without having to touch the test script code.

Is this possible with Protractor?

3 Answers 3

8

You can use browser.params to read custom test data.

To read from a JSON file simply add params to your config file

exports.config = {
  params: require('./your-params-file.json'),
};

NodeJS will automatically convert the JSON file to a Javascript object that is easily accessible from any of your tests through browser.params.whateverYourJSONHas.

If you really need to use CSV then try some parser like csvtojson or google / open another question about "NodeJS convert CSV file to array of POJOs"

7
  • 2
    @RustyWizard if this is a solution for you, you can flag it answered. it turns green, other users know somebody confirmed it's working, and elgalu gets some pixel points ;)
    – nilsK
    Commented Aug 27, 2014 at 13:54
  • @elgalu I will definitely do so. Shortly after I posted this question I got a slightly higher priority task assigned to me. I will look into this as soon as I'm done with that. Commented Aug 28, 2014 at 10:32
  • 2
    My previous was in reply to @nilsK; sorry if it cause any confusion. That said, thanks @elgalu. Your answer did help. However instead of specifying it on the config file, I specified it in the spec file as browser.params = require('../data.json'); as I felt this will give me the flexibility of having different files associated to different spec files rather than storing everything in one file and reading that the whole of that particular file. Is there by any chance a better approach to this? Commented Aug 28, 2014 at 10:50
  • @RustyWizard: Your approach is seemingly beneficial to me. However, when I try to use browser.params = require('../data.json'); in my test file, I can't really get it working for different data files for different tests! Did you work with different data files for different tests? Commented Jan 1, 2015 at 11:01
  • Hey..is it possible to read multiple json files in require? Something like: params: require('./file1.json','./file2.json'), Commented Feb 4, 2015 at 6:44
1

I'm not sure if you were able to figure this out or not. however, I was able to accomplish just this.

Here's what your json might look like:

[ 
    {
        "someId": "signInInput",
        "sendSomeKeys": "[email protected]"
    },
    {
        "someId": "passwordInput",
        "sendSomeKeys": "password"
    }
]

And here's how to implement it in your test:

'use strict';

var testData = require('./path/to/json.json');

describe('your test', function() {
    testData.forEach( function (data) {
        it('should read from an external json', function(){
            element(by.id(data.someId)).sendKeys(data.sendSomeKeys);
        });
    });
});
0

You may simply iterate using javascript "map" function without any loops:

var testParams = testConfig.testArray;

testParams.map(function(testSpec) {
    it('write your test here', function() {
        //test code here
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.