Skip to main content
When several tests repeat the same steps, move those steps into shared code.
  • Helper functions wrap one flow, such as signing in.
  • Page objects group every action for one screen in a class.
Both are plain JavaScript modules. They take the testdriver client as an argument, so they work in any test.

Write a helper function

Put the shared steps in a function that takes the client. This helper signs in to the TestDriver demo store:
tests/helpers/auth.js
Import it and call it with your test’s client:
tests/cart.test.mjs
Pass values that change between tests, like the username, as arguments. Keep them out of the descriptions you pass to find(), so the cache can match them on every run.

Group actions with a page object

A page object is a class with one method per action on a screen. Use one when many tests work with the same screen.
tests/pages/login-page.js
tests/login.test.mjs
Start with helper functions. Move to page objects when several tests repeat the same actions on one screen.

Organize your files

A layout that keeps shared code apart from tests:
By default, Vitest runs only files named *.test.* or *.spec.*, so helpers, page objects, and fixtures aren’t run as tests.

Next steps

Use variables and test data

Feed helpers with environment variables and fixtures.

Use secrets

Keep the credentials your helpers type out of logs.

Locate elements

Write descriptions that find the right element.

Click an icon example

A complete test that uses a login helper.