是否可以在conf.js中创建函数?

时间:2018-11-22 07:00:21

标签: javascript testing protractor automated-tests

是否可以创建用于每个规格测试的功能,例如conf.js文件中的登录功能,或者直接从sendKeys的{​​{1}}到conf.js的登录功能。

1 个答案:

答案 0 :(得分:1)

通过nodejs语法,它支持如下操作:

// conf.js
exports.config = {
   // put config at here as usual
};

exports.login = function(username, password) {

  browser.get('');
  element.sendKeys('');
};

// spec.js

var login = require('./conf.js').login;
// actually, you can move the login function to another .js file to keep
// conf.js more dedicated on configuration purpose.

describe("A spec (with setup and tear-down)", function() {

   it('', function(){
       login('username', 'password')
   });

});

但这不是一个好习惯。因为主测试框架支持在每个测试用例之前执行某些操作。对于茉莉花,它提供beforeEach

describe("A spec (with setup and tear-down)", function() {
  var foo;

  beforeEach(function() {
    foo = 0;
    foo += 1;
  });

  afterEach(function() {
    foo = 0;
  });

  it("is just a function, so it can contain any code", function() {
    expect(foo).toEqual(1);
  });

  it("can have more than one expectation", function() {
    expect(foo).toEqual(1);
    expect(true).toEqual(true);
  });
});

最后,我看不到您需要做的好处/优点。