Testcafe无障碍测试作为一个模块

问题描述 投票:0回答:1

我试图将Testcafe aXe测试作为一个模块,如下所示。

// 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 })? 如果是,我在哪里插入这些代码?

testing webdriver automated-tests testcafe browser-automation
1个回答
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);
});
© www.soinside.com 2019 - 2024. All rights reserved.