|
| 1 | +--- |
| 2 | +title: Testing with Playwright |
| 3 | +description: Use Playwright to write end-to-end tests with Clerk. |
| 4 | +--- |
| 5 | + |
| 6 | +# Testing with Playwright |
| 7 | + |
| 8 | +Playwright is a well-established JavaScript testing framework. This guide aims to help you set up your environment for creating authenticated tests with Clerk. This guide will assume you're somewhat familiar with Clerk and Playwright. |
| 9 | + |
| 10 | +Before diving in, you might want to start by visiting the [Playwright starter](https://github.com/clerk/playwright-clerk-nextjs-example) for an example repo that tests a Clerk-powered application using [Testing Tokens](/docs/testing/overview#testing-tokens). |
| 11 | + |
| 12 | +<Steps> |
| 13 | +### Install `@clerk/testing` |
| 14 | + |
| 15 | +Clerk's testing package provides integration helpers for popular testing frameworks. |
| 16 | + |
| 17 | +```bash filename="terminal" |
| 18 | +npm install @clerk/testing --save-dev |
| 19 | +``` |
| 20 | + |
| 21 | +### Set your API keys |
| 22 | + |
| 23 | +Set your publishable and secret keys in your test runner, as the `CLERK_PUBLISHABLE_KEY` and `CLERK_SECRET_KEY` environment variables respectively. |
| 24 | + |
| 25 | +<Callout type="warning"> |
| 26 | + Ensure you provide the secret key in a secure manner, to avoid leaking it to third parties. For example, if you are using GitHub Actions, refer to [*Using secrets in GitHub Actions*](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions). |
| 27 | +</Callout> |
| 28 | + |
| 29 | +### Global setup |
| 30 | + |
| 31 | +In your [global setup file](https://playwright.dev/docs/test-global-setup-teardown), call the `clerkSetup` function: |
| 32 | + |
| 33 | +```tsx filename="global-setup.ts" |
| 34 | +import { clerkSetup } from '@clerk/testing/playwright'; |
| 35 | +import { test as setup } from '@playwright/test'; |
| 36 | + |
| 37 | +setup('global setup', async ({ }) => { |
| 38 | + await clerkSetup(); |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +`clerkSetup` will obtain a [Testing Token](/docs/testing/overview#testing-tokens) when your test suite starts, making it available for all subsequent tests to use. |
| 43 | + |
| 44 | +<Callout type="info"> |
| 45 | + You can set the Testing Token yourself as opposed to calling `clerkSetup`, by |
| 46 | + setting it in the `CLERK_TESTING_TOKEN` environment variable. |
| 47 | +</Callout> |
| 48 | + |
| 49 | +### Use `setupClerkTestingToken` |
| 50 | + |
| 51 | +Then, in your tests use the `setupClerkTestingToken` function to augment your requests with the token: |
| 52 | + |
| 53 | +```tsx filename="my-test.spec.ts" |
| 54 | +import { setupClerkTestingToken } from "@clerk/testing/playwright"; |
| 55 | +import { test } from "@playwright/test"; |
| 56 | + |
| 57 | +test("sign up", async ({ page }) => { |
| 58 | + await setupClerkTestingToken({ page }); |
| 59 | + |
| 60 | + await page.goto("/sign-up"); |
| 61 | + // ... |
| 62 | +}); |
| 63 | +``` |
| 64 | + |
| 65 | +</Steps> |
| 66 | + |
| 67 | +For more information, feedback or issues, visit the [`@clerk/testing`](https://github.com/clerk/javascript/tree/main/packages/testing) package. |
0 commit comments