Skip to main content
A newer version of this page is available. .

Hooks

  • 3 minutes to read

TestCafe Studio allows you to specify test actions that are executed before and after each test in a fixture. These action sequences are called test hooks (the before each and after each hook).

Hooks allow you to extract repetitive actions, like logging in or opening the tested view, and reuse them within the fixture.

Hooks

Hooks include the same types of actions as the test body.

Hooks

NOTE

The browsing context is preserved between hooks and the test body. If you switch the browsing context in the before each hook, the test starts in this context. The after each hook runs in the same browsing context that was used in the last test action.

Element selectors and functions are not shared between hooks and the test body.

Create a Hook

NOTE

TestCafe Studio creates hooks automatically when you move or copy actions from a test.

To create an empty hook, invoke the context menu for a fixture or .testcafe file in the Explorer panel and click Before each hook icon Add 'before each' hook or After each hook icon Add 'after each' hook:

Add an empty hook

This adds the before each or after each item to the fixture.

Navigate to a hook from the Explorer panel

Add Actions to a Hook

You can move or copy actions from any test in the fixture to a hook. TestCafe Studio appends the selected actions to an existing hook or creates a new hook if none exists.

Alternatively, you can open the hook in the Test Editor and add actions from the Actions panel.

Move Actions From a Test

Select actions in the Test Editor (hold Cmd/Ctrl to select multiple actions) and click Before each hook icon Move to 'before each' hook or After each hook icon Move to 'after each' hook on the toolbar.

Move actions to a hook

This removes the selected actions from the test and adds them to the hook with the same parameters.

If you are moving actions that execute in an iframe, ensure that you pick the Switch to Iframe action as well.

Copy All Actions From a Test

To copy all actions from a test, invoke the context menu for this test in the Explorer panel and click Before each hook icon Copy actions to 'before each' hook or After each hook icon Copy actions to 'after each' hook:

Copy actions to a hook

Add Actions in the Test Editor

You can open the hook in the Test Editor and add actions from the Actions panel.

Add actions to a hook in Test Editor

Edit Hooks

Test hooks are displayed in the Explorer panel below a fixture name. Double-click before each or after each to open the hook.

Navigate to a hook from the Explorer panel

You can also navigate to hooks when you edit tests. The Test Editor displays the before each and after each steps in each test whose fixture contains a hook. Click these steps to open the hook.

Navigate to a hook from the Test Editor

The Fixture Editor also displays the before each and after each items if this fixture has a corresponding hook.

Navigate to a hook from the Fixture Editor

Hooks are opened in the Test Editor. You can add actions from the Actions panel, modify action parameters, re-order, and delete actions.

Hooks