Skip to content

Instantly share code, notes, and snippets.

@kenotron
Last active October 18, 2023 03:17
Show Gist options
  • Save kenotron/fa5a9c5b479072e744aa6a7c712756c6 to your computer and use it in GitHub Desktop.
Save kenotron/fa5a9c5b479072e744aa6a7c712756c6 to your computer and use it in GitHub Desktop.
Playwright Github Workflow
import { defineConfig, devices } from "@playwright/test";
import { generateTestDatabaseUrl } from "~/db/generate-test-url";
import 'dotenv/config';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: "./tests",
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: "html",
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: "http://127.0.0.1:3000",
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: "on-first-retry",
screenshot: "only-on-failure"
},
/* Configure projects for major browsers */
projects: [
// Setup project
{ name: "db-setup", testMatch: /db.setup\.ts/, teardown: "db-teardown" },
{ name: "db-teardown", testMatch: /db.teardown\.ts/ },
// Unrelated to the database, but use this for speeding up flows with a prewarmed auth
{
name: "auth-setup",
testMatch: /auth.setup\.ts/,
dependencies: ["db-setup"],
},
{
name: "chromium",
use: { ...devices["Desktop Chrome"] },
testMatch: /.*\.spec\.ts/,
dependencies: ["auth-setup"],
},
// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },
// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },
/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
globalSetup: "./tests/global-setup",
/* Run your local dev server before starting the tests */
webServer: {
command: "yarn dev",
url: "http://127.0.0.1:3000",
reuseExistingServer: !process.env.CI,
env: {
...process.env,
DATABASE_URL: process.env.DATABASE_URL,
},
stdout: "pipe",
},
});
name: Playwright Tests
on:
workflow_dispatch:
push:
paths-ignore:
- "**/README.md"
- "docs/**"
branches: [main, master]
pull_request:
paths-ignore:
- "**/README.md"
- "docs/**"
branches: [main, master]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_DB: dev
POSTGRES_USER: dbadmin
POSTGRES_PASSWORD: dbpassword
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: yarn
- name: Install Playwright Browsers
run: yarn playwright install --with-deps
- name: Run Playwright tests
run: |
cp .env.example .env
yarn playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment