Test.before Method

Defines the before test hook. Before hooks run before the main test body, but after the browser loads the test page.


A test.before declaration may cause a hook conflict.

test.before( fn(t) ) → this
Parameter Type Description
fn Function An asynchronous hook function that contains the hook code.
t Object The test controller with access to the test run API.

Test hooks can include test actions and other Test Controller API methods.

TestCafe executes test hooks in the same browsers as the test - before and after the test itself.

Use the test.after method to run hook code after the test.

Conflicting Hooks

TestCafe executes the global before hook before it executes other before hooks.

Test-specific test.before hooks override the fixture-wide beforeEach hook.

See: Hook Order and Priority.



    .before(async t => {
        await t.click('#preferred-interface');
    })('Click on checkbox', async () => {
        /* ... */