Repository with code example for Automated end-to-end tests on Simple Frontend docs
- Start by installing Playwright and follow the CLI:
pnpm create playwright --install-depsWhere to put your end-to-end tests? e2e Add a Github Actions workflow? y Install Playwright browsers? y
The CLI added folders to our .gitignore configuration, packages to our dev dependencies and created the following files:
playwright.config.tswhich is Playwright's configuration filedemo-todo-app.spec.tsintest-exampleswhich contains an advanced use case for a todo app covering most of large apps use cases. We will ignore this file but you can use it as a quick reference laterexample.spec.tsin e2e-tests which is where we will write our main tests for this demo
- Update
playwright.config.ts
For starters, let's focus on the simplest setup to make sure our website is up and running with its core functionality so we will remove tests for firefox and webkit for now under projects, comment them out.
Add a BASE_URL env variable, under use add the following:
use: {
baseURL: process.env.BASE_URL;
}- Prepare your first test, update
example.spec.tswith only one test case:
test("is up and running", async ({ page }) => {
const pageNavigation = await page.goto("/");
expect(pageNavigation?.status()).toBe(200);
await expect(page).toHaveTitle(/%YOUR_REGEX_TITLE%/);
});Because we will be using process.env.BASE_URL - "/" will directly hit your base url path.
In my case, it will go to https://automated-end-to-end-tests-example.vercel.app/ and I configured my title regex as /Automated End-to-End Tests example/
- Test that everything is working:
Add a script to your package.json:
"e2e": "playwright test"Run the script command using your preferred package manager:
BASE_URL=%YOUR_BASE_URL% pnpm run e2eThere are many different setups you can go for such as running a local server to validate your end-to-end tests against but this is not what I would recommend. What will really bring value to you is running your end-to-end tests against your preview deployments before merging your changes to your main production branch.
So here it depends on your hosting setup and provider, I will cover the most popular ones.
If you synced your github repository with a vercel project, you should already have preview deployments running out of the box.
There is only a few more steps to take:
- In your Vercel project settings, go to
Deployment Protectionand in the sectionProtection Bypass for Automation, clickAdd Secretand in the pop-up which opens immediately clickSave
This will let Vercel know you are the one running automated tests against your preview environment and authorize the requests.
Copy the value of the secret, you will need it for the next step
- In your github repository settings, go to
Secrets and variables>Actionsand add your secret value under the nameVERCEL_AUTOMATION_BYPASS_SECRET
- Let's update our github action yaml file which should be under
.github/workflows/playwright.ymland copy the following, adapting with your favorite package manager:
name: End-to-End Tests
on:
deployment_status:
jobs:
e2e-tests:
if: github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success' && github.event.deployment.environment == 'Preview'
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: lts/*
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps chromium
- name: Environment URL
run: echo ${{ github.event.deployment_status.environment_url }}
- name: Run Playwright tests
run: pnpm run e2e
env:
BASE_URL: ${{ github.event.deployment_status.environment_url }}
VERCEL_AUTOMATION_BYPASS_SECRET: ${{ secrets.VERCEL_AUTOMATION_BYPASS_SECRET }}
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: playwright-report
path: playwright-report/
retention-days: 1This will run on every Vercel preview deployments and it will:
- Checkout your repository
- Install pnpm
- Setup nodeJS
- Install dependencies
- Install Playwright browsers (in this example, I explicitely only require chromium)
- Display the value of the environment URL against which the end-to-end tests will run
- Run the tests
- Upload their results so you can download and view the report HTML in case of failures
on Step 7 (Run Playwright tests), we are setting 2 environment variables, the BASE_URL for the preview deployment which Vercel gives us and the github action secret we just created
- The final thing to do is to update our
playwright.config.tsto send thex-vercel-protection-bypassheader to Vercel, so we update the value ofuseto:
use: {
trace: "on-first-retry",
baseURL: process.env.BASE_URL,
extraHTTPHeaders: {
"x-vercel-protection-bypass": process.env.VERCEL_AUTOMATION_BYPASS_SECRET!,
"x-vercel-set-bypass-cookie": "true",
},
},Also set x-vercel-set-bypass-cookie to true so the header value is also sent on subsequent requests should we need it. Read more about Protection Bypass for Automation on Vercel
- Test everything
Merge the changes to your main branch, create a small changeset on any branch and create a pull request against your main branch and you should see your new workflow running:
It will run again on every push (like on this demo branch!)


