0

I have multiple it statements in one describe {}. In one it statement I am reading data from excel and doing for loop to execute test scripts for number of users in the sheet. I need to create page model and so for each page thinking on creating one it statement. How can we do that. Here is my actual script

var login = require('./login');
var utility = require('./utility');
var exRead = require('./readExcel');

describe('MSIX Smoke', function () {
  var userList = [];

  beforeAll(function(done) {

    browser.get('https://url');  //Dev url
    expect(browser.getTitle()).toEqual('test');
    exRead().then(function(excelData) {
      userList = prepData(excelData);
      done();
    });

  });

  it('should login users', function() {
    var loopCount = userList.length
    loopCount=1;
    for (var i = 0; i<loopCount; i++) {
      var data = userList[i];
      loginScript(data[0], data[1], data[2]);
      login.clickSignOut();
     }
  })

   it('search for a student', function () {
     console.log(data[3]);
   });
it ('click on my account', function () {
//some code
})
});

function prepData(data) {
  var formattedData = [];
  var counter = data.length / 5;
  for (var i = 0; i < counter; i++) {
    formattedData.push(data.splice(0,5))
  }
  return formattedData;
}  

function loginScript(username, password, userType) {
  var loginName = element(by.css('.nameP'));
  console.log(username, password);
  login.fillUsername(username);
  login.fillPassword(password);
  login.clickSignup();
  login.clickPrivacy();
  console.log("User " + username + " with user type "+userType+" logged in successfully");
  loginName.isPresent;
  utility.getStringText("Logged in user name is: ", loginName);
  return loginName;
};
Vin
  • 165
  • 2
  • 12
  • 1
    try this https://stackoverflow.com/questions/38221397/data-driven-testing-in-protractor – Sumedha Gamage Mar 27 '18 at 20:19
  • Hey @SumedhaGamage, thanks for the reply. I have checked it but that is not something I am looking for. My data will be read from excel sheet and will need to parse the data in to multiple it statements and iterate through those it statements based on the number of rows in my data sheet – Vin Mar 28 '18 at 15:35
  • Hi @SumedhaGamage, Thanks for the reference. I finally endded up in using the same code in that reference and able to use multiple it statements – Vin Apr 02 '18 at 15:27

1 Answers1

0

Give code examples with two approaches:

describe('xxx', function() {

    browser.get('https://www.npmjs.com');

    var datas = [1, 2, 3];

    // Option 1, use for loop and javascript closure
    for (var i = 0, len = datas.length; i < len; i++) {

        (function(i) {
            return it('yyy ' + i, function() {
                console.log('data[' + i + '] = ' + datas[i]);
                browser.getTitle().then(function(title) {
                    console.log('title[' + i + '] = ' + title);
                });
            });
        })(i);
    }

    // option 2, use Array.forEach()
    datas.forEach(function(data, i) {
        it('yyy ' + i, function() {
            console.log('data[' + i + '] = ' + data);

            browser.getTitle().then(function(title) {
                console.log('title[' + i + '] = ' + title);
            });
        });
    })
});
yong
  • 13,357
  • 1
  • 16
  • 27