Playwright with GitHub Actions: Step-by-Step Guide

 


You have written your first Playwright test. It opens the application, finds the right button and checks the expected result. Now you want that same check to run when somebody opens a pull request, without asking a teammate to run it manually.

This Playwright GitHub Actions guide takes you from that local test to an automated workflow. The example uses a local application, one browser and downloadable failure evidence. Once that works, you can adapt it to your repository rather than starting with a complicated matrix.

How do you run Playwright with GitHub Actions?

Create a workflow in .github/workflows/ that checks out your repository, sets up Node.js, installs locked dependencies and Playwright browsers, then runs the tests. Use Playwright's webServer configuration for a local application. Upload configured reports after failures, and preserve the test step's exit status so the job accurately reports unsuccessful runs.

Step 1: Understand the workflow vocabulary

A workflow is an automation file. An event starts it, such as a push or pull request. A job groups work on a runner, the machine executing the instructions. Steps inside the job run commands or reusable actions.

Here, one job installs the project and runs Chromium tests on Ubuntu 24.04. Keeping the first version small makes it easier to identify whether a problem belongs to installation, application startup or testing.

The examples target GitHub.com with a hosted runner. If you use GitHub Enterprise Server or a self-hosted runner, check action compatibility and required runner versions separately. A working hosted example does not prove that your organization's older agent supports the same action release.

Step 2: Prepare the files and application

Commit package.json, package-lock.json, playwright.config.ts and your tests/ directory. Install @playwright/test as a development dependency first if the project does not already use it, and commit the resulting dependency changes.

This tutorial assumes an existing application whose npm run start:test script serves port 3000. The page at / must contain a visible level-one heading reading “Task Board.” Change that assertion to match your actual application.

If the application needs building, define and run its build command before tests. The workflow below assumes startup can run directly after dependency installation. Node.js 24 is the example release; validate your application's dependencies against it before adopting the workflow.

Step 3: Add the matching configuration

Create playwright.config.ts at the repository root:

import { defineConfig, devices } from '@playwright/test';


const localURL = 'http://127.0.0.1:3000';


export default defineConfig({

  testDir: './tests',

  forbidOnly: !!process.env.CI,

  workers: process.env.CI ? 1 : undefined,

  retries: 0,

  outputDir: 'test-results',

  reporter: [

    ['list'],

    ['html', { outputFolder: 'playwright-report', open: 'never' }],

  ],

  use: {

    baseURL: localURL,

    trace: 'retain-on-failure',

    screenshot: 'only-on-failure',

    video: 'retain-on-failure',

  },

  projects: [

    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },

  ],

  webServer: {

    command: 'npm run start:test',

    url: localURL,

    reuseExistingServer: !process.env.CI,

    timeout: 120_000,

  },

});

This configuration waits for the application before executing tests. It permits reuse of a development server locally, but expects a new server in CI. Reports and attachments have explicit locations that the workflow will upload later.

The one-worker setting prioritizes a simple first run over maximum speed. Adjust it after measuring your runner. The example does not use retries, so intermittent failures remain visible immediately. See Playwright's configuration and web server guide.

Step 4: Add a small browser test

Create tests/home.spec.ts:

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


test('task board opens with its main heading', async ({ page }) => {

  await page.goto('/');

  await expect(

    page.getByRole('heading', { name: 'Task Board', level: 1 })

  ).toBeVisible();

});

The test navigates relative to baseURL and checks a user-visible outcome. The assertion waits for the expected state within its configured timeout, so a fixed sleep is unnecessary. It deliberately avoids login and shared writable data to keep the initial workflow easy to diagnose.

This is a startup smoke test. It does not establish that every task-board feature works. Add meaningful journeys after the installation and artifact paths are proven.

Step 5: Create the GitHub Actions workflow

Save this file as .github/workflows/playwright.yml:

name: Playwright browser tests


on:

  push:

    branches: [main]

  pull_request:

    branches: [main]

  workflow_dispatch:


permissions:

  contents: read


jobs:

  test:

    runs-on: ubuntu-24.04

    timeout-minutes: 30

    env:

      CI: 'true'


    steps:

      - name: Check out repository

        uses: actions/checkout@v7


      - name: Set up Node.js

        uses: actions/setup-node@v7

        with:

          node-version: '24'

          package-manager-cache: false


      - name: Install project dependencies

        run: npm ci


      - name: Install Chromium and system dependencies

        run: npx playwright install --with-deps chromium


      - name: Run Playwright tests

        run: npx playwright test


      - name: Upload reports and debugging evidence

        if: ${{ !cancelled() }}

        uses: actions/upload-artifact@v7

        with:

          name: playwright-evidence

          path: |

            playwright-report/

            test-results/

          retention-days: 7

          if-no-files-found: warn

The official action repositories currently document checkout@v7, setup-node@v7 and upload-artifact@v7. Consult their release requirements before updating a self-hosted runner: checkout, setup-node and upload-artifact.

The event section runs checks for pushes to main, pull requests targeting main and manual dispatch. The manual option becomes available through GitHub's workflow interface when the workflow is on the default branch.

contents: read grants repository-reading permission. Setup installs the selected Node.js release. Automatic package-manager caching is disabled here to keep the beginner installation explicit; caching can be added later after understanding what is stored.

The browser-install step matches the Chromium project. The test command keeps its normal exit status. The upload condition permits evidence collection after a failed step while skipping cancelled runs. Seven days is an example retention choice, subject to repository and organization policy.

Step 6: Run the workflow and inspect evidence

Push the files through your normal development process. Open the repository's Actions tab, select Playwright browser tests, then open a run. Select the test job to inspect individual step logs.

On the run's summary page, look for Artifacts and download playwright-evidence. Extract it while preserving the folder structure. From the extracted root, open the report with:

npx playwright show-report playwright-report

Use a local environment with Playwright available. Select a failed test, inspect its assertion and follow available trace attachments. The trace viewer documentation explains how to investigate recorded actions.

If installation fails before testing, reports may not exist. The upload step warns about missing files, but the original installation failure still makes the job unsuccessful. Find the earliest failed step before searching for browser evidence.

Step 7: Add deployed URLs or credentials carefully

For a deployed application, store a nonsecret URL as a GitHub variable and pass it as BASE_URL. Update configuration so that this value becomes baseURL and disables webServer; merely adding the variable does not change the local-only configuration above.

Store passwords or tokens as secrets and expose them only to the steps that require them. Do not print them, write them into public reports or commit generated authentication files.

Secrets are generally unavailable to pull requests from forks. Keep those runs useful with checks that do not require credentials, or use a separately controlled trusted process. Do not execute untrusted pull-request code in a privileged workflow to bypass the restriction. Review GitHub's secrets documentation.

Troubleshoot before adding more complexity

Symptom

What to check first

npm ci fails

Lockfile presence, dependency agreement and private-package access

Browser executable missing

Browser installation step and selected project

Server readiness times out

Startup script, port, build output and startup logs

Test passes locally only

Clean installation, data assumptions and Linux path casing

Artifact is missing

Whether tests ran and whether configured paths exist

A browser matrix or sharding can reduce feedback time later. Give each parallel job a unique artifact name and ensure accounts and records are independent. Avoid browser-binary caching as an initial shortcut; compatibility and restore time can outweigh the benefit.

Learners who want structured practice can explore the practical Playwright automation course and review the available learning options. A useful exercise is making one assertion fail intentionally and confirming that GitHub preserves both the failed status and its report.

Frequently asked questions

1. Where should the workflow file be saved?

Save it inside .github/workflows/ with a .yml or .yaml extension. In this example, the filename is playwright.yml.

2. Why does the workflow use npm ci?

It installs from the committed lockfile and identifies dependency mismatches, helping a clean runner reproduce the intended dependency set.

3. Will artifact uploading turn a failed test job green?

No. The upload runs after failure, but the test step's nonzero exit status remains. This workflow does not use continue-on-error.

4. Can I run the workflow manually?

Yes. workflow_dispatch enables manual execution through the workflow interface when GitHub's branch and access requirements are satisfied.

5. Should I install Firefox and WebKit immediately?

Add them when your coverage requires them. Update both browser installation and Playwright projects, then measure runtime and resource needs.


Comments

Popular posts from this blog

How to Run Only Smoke Tests in Playwright: Step-by-Step Guide

How to Test REST APIs Using Playwright

Playwright Multiple Tabs and Windows Explained: A Practical Guide