每个测试用例的 beforeEach 和 AfterEach 使用 Globals.js

beforeEach and AfterEach for every test case using Globals.js

所以,我有一个 globals.js 文件,我在其中提到了 beforeEach 和 afterEach,但是我可以从这个 link Nightwatch Globals 中理解,beforeEach 和 afterEach 在之前被调用了一次在测试套件(单个 Js 文件)之后。但是在我的框架中,我在单个 js 文件(或测试套件)中有多个测试用例,我想在每个测试用例之前和之后调用 beforeEach 和 afterEach。无论如何要实现这一目标?下面是我的 globals.js 文件:

module.exports = {
    asyncHookTimeout: 40000,

    beforeEach: function (browser, done) {
        // browser.maximizeWindow();
        // browser.deleteCookies();
        browser.perform(function () {
            console.log('Inside BeforeEach');
            done();
        });
    }

    afterEach: function (browser, done) {
        browser.end(function () {
            console.log("Inside After Each");
            done();
        });
    },
};

当然有!只需利用臭名昭著的 Nightwatch test hooks.

  • 如果您想过滤 测试套件 ,那么正如您所指出的, 我们将使用 全局测试 钩子.
  • 如果您想过滤 测试用例 ,那么我们将使用 测试 钩子

示例您的测试文件应如下所示):

module.exports = {

  before(browser) {
    // > this will get run only ONCE, before all the tests <
  },
  beforeEach(browser) {
    // > this will get run before every test case <
  }

  tags: ['your', 'tags', 'go', 'here'],
  'Test Case No.1': (browser) => {
     // > this test does something here <
  },
  'Test Case No.2': (browser) => {
     // > this test does something else here <
  },
  'Test Case No.3': (browser) => {
     // > this test does something else here <
  },

  afterEach(browser) {
    // > this will get run after every test case <
  },
  after(browser) {
    // > this will get run ONCE, after all tests have run <
  }
};

最后引用文档:

The before and after will run before and after the execution of the test suite respectively (in our case, the test-file), while beforeEach and afterEach are ran before and after each test case (test step).


LE:@AlapanDas 想要的是定制 Nightwatch 测试的方式-运行ner 处理测试级挂钩。这当然是可行的,但是很脏。您必须从以下文件中重新编写挂钩逻辑:

守夜人@v0.9.x:

  • testcase.js(路径:/nightwatch/lib/runner/testcase.js);
  • testsuite.js(路径:/nightwatch/lib/runner/testsuite.js);

守夜人@v1.0.x:

  • 每个 {hookName}.js 文件来自 /hooks 文件夹(路径:/nightwatch/lib/testsuite/hooks/*.js);

不过,还是可以在这里做出妥协!试着从你的 steps/instructions =15=]、after 等钩子并将该逻辑提取到 /custom_commands 文件中。这将压缩您的测试文件,并将登录与挂钩分离。在长 运行 上,这也将在维护挂钩时提供 单点更改 的优势。