运行 在验收测试之前设置一次

Run setup once before acceptance tests

我有以下使用 CodeceptJS 编写的验收测试套件,它运行良好。我在每个步骤之前使用 Before 登录。问题是 Before 函数等同于 Mocha 的 beforeEach,而不是 Mocha 的 before,而我 真正 想要的只是一些代码运行 一次,因为此登录步骤需要一些时间才能完成。

我希望最终得到一个函数,该函数将 运行 一次并从 javascript 获取一个 loginToken,然后我可以使用 Before 函数在每个函数之前设置它测试如下。关于如何执行此操作的任何线索?

Feature('Top level-routes');

Before((I) => {
  const loginBtnText = 'LOG IN';

  I.amOnPage('/login');
  I.fillField('username', 'root');
  I.fillField('password', 'mypass');
  I.see(loginBtnText);
  I.seeElementInDOM('[name=loginBtn]');
  I.click('loginBtn');

  // I don't understand why I need to wait for the click to register
  // but nothing happens unless I do ...
  I.wait(1);
  I.dontSee(loginBtnText);
});

Scenario('a page to add a new encounter should be rendered', (I) => {
  I.amOnPage('/encounter/new');
  I.wait(2);
  I.see('First');
  I.see('Last');
});

Scenario('a page to add a new user should be rendered', (I) => {
  I.amOnPage('/user/new');
  I.wait(2);
  I.see('Personalia');
})

P.S。我确实意识到我可能可以使用一些外部状态来解决这个问题,以检查函数之前是否有 运行,但还没有做到。支持最少的 hacky 答案:)

你可以有一个自定义的 CodeceptJS 帮助器,在那里你可以使用以下这些方法,

_init - 在所有测试之前, _before - 测试前

参考:http://codecept.io/helpers/