PlaywrightHelperfunctions

Install

yarn add @ntds/playwright-helperfunctions

Import

import { test, expect } from '@ntds/playwright-helperfunctions';

First time setup of Playwright

After installing omniweb and have that build running you can start with playwright.

First, add playwright in your package.json, like this:

{
"scripts": {
"playwright": "playwright"
}
}

Then you need to install the browsers:

yarn playwright install

Environment Variables

Playwright uses several environment variables that can be configured in a playwright.env file or set directly in your environment:

Legacy/Cypress Compatibility

The following Cypress environment variables are also supported for compatibility:

  • PLAYWRIGHT_BASE_URL - Falls back to CYPRESS_baseUrl if not set
  • PLAYWRIGHT_API_URL - Falls back to CYPRESS_apiUrl if not set

Required Environment Variables

  • PLAYWRIGHT_BASE_URL - The base URL for the application under test

    • Default: http://localhost:3000
    • Example: PLAYWRIGHT_BASE_URL="http://localhost:3000"
  • PLAYWRIGHT_API_URL - The API URL for backend services

    • Default: https://api.qa01.norsk-tipping.no (based on NT_ENVDOMAIN)
    • Example: PLAYWRIGHT_API_URL="https://api.qa01.norsk-tipping.no"

Optional Environment Variables

  • NT_ENVDOMAIN - The environment domain for Norsk Tipping services

    • Default: qa01
    • Used to construct API URLs and authentication endpoints
    • Example: NT_ENVDOMAIN="qa01"
  • PLAYWRIGHT_USER_BUYPASS_ID - Specific user ID for testing

    • Default: Empty (will fetch from user pool)
    • If not set, tests will fetch a user from the test user pool
    • Example: PLAYWRIGHT_USER_BUYPASS_ID="123456789"

Usage

The test fixture automatically:

  • Acquires a test user account for authenticated tests
  • Handles authentication
  • Sets up proper storage state with cookies accepted
  • Configures modal interception
import { test, expect } from '@ntds/playwright-helperfunctions';
test('should access a protected page', async ({ page }) => {
await page.goto('/min-side');
// The page will automatically be authenticated
// ...assertions
});

Authenticated and guest tests

For authenticated tests, use SomeTestName.spec.ts.

For tests without authenticated, use SomeTestName.guest.spec.ts, and import defaultStorageState:

import { defaultStorageState } from '@ntds/playwright-helperfunctions';
test.use(defaultStorageState);

Playwright automatically handles authentication for tests by:

  1. Fetching a test user from the user pool (unless PLAYWRIGHT_USER_BUYPASS_ID is specified)
  2. Obtaining authentication tokens from the test authentication service
  3. Setting up storage state with the required tokens and localStorage defaults

Desktop tests (baseConfig)

If you use baseConfig you can run tests only on desktop project by naming the tests SomeTestName.desktop.spec.ts or SomeTestName.guest.desktop.spec.ts for non-authenticated tests (if you also import defaultStorageState).

Change test path

If you use the baseConfig as your standard playwright configuration and want to override the test path, you can do:

"pw:extended": "PW_TEST_DIR=playwright/e2e-extended playwright test",

(supports --ui and filters like the one described under "Pipeline tets")

This guide is based on version: 2.0.4