5

I am new to protractor. Can anyone please guide me for data driven testing using protractor. Below is the code, config file and testdata.json file.

'use strict';

var testData = require('../example/Test Data/Test.json');

describe('LoginPage', function() {

var loginData = require('../example/Test Data/Test.json');

testData.forEach(function (data) {
    it("data.description", function (data) {
        browser.get("http://127.0.0.1:8080/#/login");
element(by.model("username")).sendKeys(data.username);
element(by.model("password")).sendKeys(data.passwordField); 
element(by.buttonText("Authenticate")).click();

});
});
});  

Config file:

 // An example configuration file.
exports.config = {
directConnect: true,

//seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'

},

// Framework to use. Jasmine is recommended.
framework: 'jasmine',

// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['Testpage.js'],

// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
};

Json File:

[
{
 "username": "admin",
 "passwordField": "admin"
},

{
"username": "admin1",
"passwordField": "admin2"
}
]

Issue is that instead of taking data , it is writing undefined in all input boxes. Please Help

Sahil Sehgal
  • 151
  • 2
  • 5
  • 11

5 Answers5

7

We have jasmine-data-provider package which will helps us in doing data driven testing with Protractor.

Code Snippet:

var using = require(‘jasmine-data-provider);
var loginData = require('../example/Test Data/Test.json');


 describe('Data driven test spec', function () { /*define sets of input data as array in method called arrayOfData*/
     function arrayOfData() {
       return [
              {
                "username": "admin",
                "passwordField": "admin"
              },

             {
              "username": "admin1",
              "passwordField": "admin2"
              }
          ]
         //or return loginData json object here
   } /*below one will loop the test case based on data size and pass single data set every time till complete the end of array*/

using(arrayofData, function (inputData) {
    it('test case logic to be executed for each set of data', function () {
        browser.get("http://127.0.0.1:8080/#/login");
        element(by.model("username")).sendKeys(inputData.username);
        element(by.model("password")).sendKeys(inputData.passwordField); 
        element(by.buttonText("Authenticate")).click();
    });
  });
 });

NOTE: If jasmine-data-provider package NOT yet installed in your machine, please install it by running below command before going to run test script.

 npm install jasmine-data-provider
Optimworks
  • 2,537
  • 17
  • 20
  • Hi Suresh, It is giving me error 'Module: jasmine-data-provider not found'. Can you please help – Sahil Sehgal Jul 06 '16 at 18:15
  • @SahilSehgal, Error says that jasmine-data-provider package is not yet installed in your machine. Install jasmine-data-provider by running command "_npm install jasmine-data-provider_" from command prompt or terminal. It will work. – Optimworks Jul 07 '16 at 01:45
  • 1
    @SureshSalloju nice package, did not know it exists. Now we've switched from using `Array.map()` to using `using`. Thanks! – alecxe Jul 07 '16 at 04:12
  • @SureshSalloju thanks a lot man. It's really useful for my current framework – Vikas Jul 07 '16 at 09:25
2

I am assuming its an Array of objects, you can iterate each array element and directly access its contents and you don't need testdata.forEach(), you could try something like this -

 'use strict';

var testData = require('../example/Test Data/Test.json');

describe('LoginPage', function() {

it("data.description", function () {
    browser.get("http://127.0.0.1:8080/#/login");
    element(by.model("username")).sendKeys(testData[0].username);
    element(by.model("password")).sendKeys(testData[0].passwordField); 
    element(by.buttonText("Authenticate")).click();

   });
  });
 });  

I haven't tested the above code and you should use Page Objects rather than directly using them in your tests!

Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • Yes , obviously Page Object should be the approach for any test. But my focus was to have data driven approach in my tests, so created a simple one. Well, thanks a lot it worked for me. Can you please guide me from where can i get some stuff regarding protractor – Sahil Sehgal Jul 06 '16 at 18:13
  • Why are you reading the same data file in 2 variables testData & loginData? – Vishal Aggarwal Oct 28 '18 at 12:36
  • @V.A. lol thats a copy paste, the same is there in the posted question :P. I will edit it out. – Ram Pasala Oct 29 '18 at 07:19
  • Ok Ram , thanks.Just pointed out so that newbies might not get confused. – Vishal Aggarwal Oct 29 '18 at 09:49
1

A simpler approach using map function:

var testParams = testConfig.testArray;
testParams.map(function(testdata) {
        it('write your test here', function() {
          console.log('Username: ', testData.username);
         });
 });
Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23
0

I think your approach is quite reasonable. The reason you are getting undefined is because you put data in the 'done' parameter. It is setting data to the 'done' object that is passed when the 'it' function calls the function you are defining.

testData.forEach(function (data) { it("data.description", function (data) {

should be

testData.forEach(function (data) { it("data.description", function () {

Chuck Brown
  • 353
  • 1
  • 5
0

The 2nd answer is more prominent. Just to add here that if you want to read data from Excel sheet and then perform Data Driven Testing then this video is very helpful: https://www.youtube.com/watch?v=vzvC4dYE84Q

Pankaj Dubey
  • 279
  • 2
  • 18