Helpers are any code that you find yourself reusing in your tests, typically, any functions or processes you need to repeat.

In Pup, test helpers are broken into four different files:

  • /tests/helpers/e2e.js for storing your end-to-end test helpers.
  • /tests/helpers/integration.js for storing your integration test helpers.
  • /tests/helpers/shared.js for storing your shared test helpers that can be used across all test types.
  • /tests/helpers/unit.js for storing your unit test helpers.

By default, Pup ships with two helpers, both located in the /tests/helpers/e2e.js file: one for helping to fill out the login form in Pup (to simulate a user performing that behavior) and a getPageUrl() function for retrieving the browsers URL during a test (e.g., for testing whether or not a redirect works as expected).

import { ClientFunction, Selector } from 'testcafe';

export const login = async ({ email, password, browser }) => {
  await browser.typeText('[data-test="emailAddress"]', email);
  await browser.typeText('[data-test="password"]', password);
  await browser.click('button[type=submit]');
  await Selector('[data-test="user-nav-dropdown"]')(); // NOTE: If this exists, users was logged in.

export const getPageUrl = ClientFunction(() => window.location.href);