- 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.
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.
Create a Hook
This adds the before each or after each item to the fixture.
Add Actions to a Hook
Move Actions From a Test
Select actions in the Test Editor (hold Cmd/Ctrl to select multiple actions) and click Move to 'before each' hook or Move to 'after each' hook on the toolbar.
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 Copy actions to 'before each' hook or Copy actions to 'after each' hook:
Add Actions in the Test Editor
Test hooks are displayed in the Explorer panel below a fixture name. Double-click before each or after each to open the hook.
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.
The Fixture Editor also displays the before each and after each items if this fixture has a corresponding hook.