如何在一个For循环量角器中创建多个it语句

时间:2018-03-27 20:11:19

标签: node.js for-loop jasmine protractor

我在一个describe {}中有多个it语句。在一个声明中,我正在从excel读取数据并执行循环以执行工作表中用户数量的测试脚本。我需要创建页面模型,因此每个页面都要考虑创建一个it语句。我们怎么做到这一点。这是我的实际脚本

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

1 个答案:

答案 0 :(得分:0)

使用两种方法提供代码示例:

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