0

I'm using Testcafe with an application that has a database driven backend (no API, but MVC).

I would like to Setup and Teardown settings in the database prior to running the tests. This can be down by calling an URL on my application (e.g. https://myapp.local/setup?foo=bar or https://myapp.local/teardown?foo=bar)

I would like to call these URLS from within Testcafe, prior to running the test. I have looked at the module testcafe-once-hook(https://github.com/DevExpress/testcafe-once-hook) and the accompanying examples (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 */
    });

but the URL in setupDb is never called as I can see from my Apache logfiles.

I also looked at https://github.com/DevExpress/testcafe/issues/1345#issuecomment-338796731, but here also the URL is never called as I can see from my Apache logfiles.

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;
        }
    })

How can I achieve that https://myapp.local/setup?foo=bar is called beforehand?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47

2 Answers2

1

I examined your code and found some mistakes that can lead to incorrect behavior. I modified your code, please check it:

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);


or

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

Alex Kamaev
  • 6,198
  • 1
  • 14
  • 29
  • Hi @alex-kamaev, Thanks for the reply. I have implemented above code (both example 1 and example 2), but still the page is not called. In VSCode I'm getting a warning on the import line: `Could not find a declaration file for module 'testcafe-once-hook'. '/path/to/node_modules/testcafe-once-hook/index.js' implicitly has an 'any' type. Try `npm i --save-dev @types/testcafe-once-hook` if it exists or add a new declaration (.d.ts) file containing `declare module 'testcafe-once-hook';`ts(7016)` I'm not sure if it's relevant. Sorry for the late reply, had a short holiday – Joost van der Drift Aug 09 '21 at 12:41
  • 1
    I tried the `testcafe-once-hook` module with my example, and it looks like something changed in TestCafe, so the `oncePerFixture` function does not work as expected. Please create a separate issue in the TestCafe github repository using the following link: [https://github.com/DevExpress/testcafe/issues/new?assignees=&labels=&template=bug-report.md](https://github.com/DevExpress/testcafe/issues/new?assignees=&labels=&template=bug-report.md). As for the warning, it is caused by your TypeScript settings, so it cannot be the cause of the described issue. – Alex Kamaev Aug 10 '21 at 08:26
  • 1
    Thanks. See https://github.com/DevExpress/testcafe/issues/6453 – Joost van der Drift Aug 11 '21 at 09:22
  • Do you know I which version of Testcafe it should work? So I can at least continu with building tests, awaiting the fix? – Joost van der Drift Aug 12 '21 at 12:31
  • It looks like the module works correctly in v1.14.2. – Alex Kamaev Aug 13 '21 at 08:13
  • any idea why Testcafe waits a couple of minutes after calling the Setup url – Joost van der Drift Aug 27 '21 at 12:19
  • > I would like to Setup and Teardown settings in the database prior to running the tests. Do you want to do it before each fixture or before the whole test run? – mlosev Aug 31 '21 at 08:19
  • @mlosev both are options for me. Before/after each fixture I can setup/teardown for that specific fixture, before the whole test run I can set up everything at once. I have no preference. – Joost van der Drift Sep 03 '21 at 09:38
  • We are working on the functionality for setting a global before/after hooks for fixtures and tests, however, at this moment it's difficult to say precisely when it will be released. Please follow our announcements. – Alex Kamaev Sep 06 '21 at 07:22
  • Where can I subscribe to the announcements @alex-kamaev? – Joost van der Drift Sep 06 '21 at 07:27
  • For detailed information about major releases, you can track the following page: [https://testcafe.io/release-notes](https://testcafe.io/release-notes) To track patches and minor releases, you can visit the [https://github.com/DevExpress/testcafe/releases](https://github.com/DevExpress/testcafe/releases) page. You can also visit the [https://github.com/DevExpress/testcafe/issues/745](https://github.com/DevExpress/testcafe/issues/745) issue to track our progress on this functionality. – Alex Kamaev Sep 07 '21 at 07:39
0

At the moment I'm running testcafe@6.14.11 and testcafe-once-hook@0.0.4 and this has solved my issues.