0

我将 Testcafe 与具有数据库驱动后端(没有 API,但 MVC)的应用程序一起使用。

我想在运行测试之前设置和拆除数据库中的设置。这可以通过在我的应用程序上调用 URL 来关闭(例如https://myapp.local/setup?foo=barhttps://myapp.local/teardown?foo=bar

在运行测试之前,我想从 Testcafe 中调用这些 URL。我查看了模块testcafe-once-hookhttps://github.com/DevExpress/testcafe-once-hook)和随附的示例(https://github.com/AlexKamaev/testcafe-once-hook-example

import { oncePerFixture } from 'testcafe-once-hook';

const setupDb = oncePerFixture(async t => {
    await t.navigateTo('https://myapp.local/setup?foo=bar');
});


fixture `Setup db`
    .before(() => {
        setupDb;
    })

    test('Test1', async t => {
        /* Test 1 Code */
    });

但是从我的 Apache 日志文件中可以看出,setupDb 中的 URL 从未被调用过。

我还查看了https://github.com/DevExpress/testcafe/issues/1345#issuecomment-338796731,但从我的 Apache 日志文件中可以看到,这里也从未调用过 URL。

fixture `Setup db`
    .before(async ctx => {
        ctx.hasSetup = false;
        // Server-side actions
    })
    .beforeEach( async t => {
        if (!t.fixtureCtx.hasSetup) {
            await t.navigateTo('https://myapp.local/setup?foo=bar');
            t.fixtureCtx.hasSetup = true;
        }
        else {
            return t;
        }
    })

我怎样才能实现预先调用https://myapp.local/setup?foo=bar

4

2 回答 2

1

我检查了您的代码,发现了一些可能导致错误行为的错误。我修改了你的代码,请检查:

import { oncePerFixture } from 'testcafe-once-hook';

const setupDb = oncePerFixture(async t => {
    await t.navigateTo('https://myapp.local/setup?foo=bar');
});


fixture `Setup db`
    .beforeEach(setupDb);


或者

fixture `Setup db`
    .beforeEach(async t => {
        await setupDb(t);
     });

于 2021-08-02T08:14:48.497 回答
0

目前我正在跑步 testcafe@6.14.11testcafe-once-hook@0.0.4 这解决了我的问题。

于 2022-02-21T15:48:04.353 回答