Testcafe 可访问性测试作为一个模块

Testcafe Accessibility test as a module

我正在尝试将 Testcafe ax 测试作为一个模块包含在内,如下所示:

// 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 })可以解决吗?如果是这样,我应该在哪里插入该代码?

您需要将 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);
});