Testcafe辅助功能测试作为模块

时间:2020-06-02 23:09:08

标签: testing webdriver automated-tests testcafe browser-automation

我正尝试将Testcafe斧头测试作为一个模块包括如下:

// a11y.js
const { axeCheck, createReport } = require('axe-testcafe');

const a11y = async t => {
  const { error, violations } = await axeCheck(t);
  await t.expect(violations.length === 0).ok(createReport(violations));
};

module.exports = {
  a11y
};

然后按如下所示导入我的测试文件:

// mytest.js
const myModule = require('a11y.js');

fixture `TestCafe tests with Axe`
    .page `http://example.com`;

test('Automated accessibility testing', async t => {
    await a11y();
});

目标是集中该模块中的所有测试(有一堆文件和测试),并公开要在其他地方使用的功能。

但是,我收到以下错误,基于读取是因为axeCheck(t)必须在测试中。

Automated accessibility testing

   1) with cannot implicitly resolve the test run in context of which it should be executed. If you need to call with from the Node.js API
      callback, pass the test controller manually via with's `.with({ boundTestRun: t })` method first. Note that you cannot execute with outside
      the test code.

可以通过致电.with({ boundTestRun: t })解决此问题吗?如果是这样,我应该在哪里插入该代码?

1 个答案:

答案 0 :(得分:1)

您需要将TestController对象作为a11()函数的参数传递。 因此,您的代码将如下所示:

// a11y.js

const { axeCheck, createReport } = require('axe-testcafe');

const a11y = async t => {
    const { violations } = await axeCheck(t);

    await t.expect(violations.length === 0).ok(createReport(violations));
};

module.exports = a11y;

// test.js
const a11y = require('./a11y.js');

fixture `Fixture`
    .page('http://example.com');

test('test', async t => {
    await a11y(t);
});