Skip to content

Commit

Permalink
chore: setup playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
tctc91 committed Aug 1, 2023
1 parent 4b56ab5 commit 2a53e5a
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 42 deletions.
41 changes: 1 addition & 40 deletions .github/workflows/build-test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,52 +90,13 @@ jobs:
junitxml-path: ./junit.xml
title: Unit tests coverage

##############################################################################
# Cypress tests
##############################################################################

# TODO: Restore once flakiness in pipeline is resolved

cypress-tests:
name: Cypress tests
needs: [build]
runs-on: ubuntu-latest
strategy:
fail-fast: false # https://github.com/cypress-io/github-action/issues/48
matrix:
containers: [1, 2] # Uses 2 parallel instances
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-node
- uses: ./.github/actions/install-cache
- uses: ./.github/actions/build-cache
- name: Start mock server
run: npm run dev:mock-server &
- name: Wait for Mock Server
shell: sh
run: ./.github/scripts/wait-for-service.sh http://localhost:3005
- name: Start app
run: NODE_ENV=test npm run start &
- uses: ./.github/actions/run-cypress-tests
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
record-key: ${{ secrets.CYPRESS_RECORD_KEY }}
cache-folder: cypress/cache

cypress-tests-success:
name: Cypress tests check
needs: [cypress-tests]
runs-on: ubuntu-latest
steps:
- run: echo 'All Cypress tests passed ✅'

###############################################################################
# Deploy
###############################################################################

deploy:
name: ECR Image Push
needs: [quality-checks, unit-tests, cypress-tests-success]
needs: [quality-checks, unit-tests]
runs-on: ubuntu-latest
if: ${{ github.ref == 'refs/heads/main' }}

Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Playwright Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts
/test-results/
/playwright-report/
/playwright/.cache/
48 changes: 48 additions & 0 deletions e2e/smoke.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect, test } from '@playwright/test'

test('Home page', async ({ page }) => {
await page.goto('/')
await expect(page).toHaveTitle(/Respiratory Viruses | UKHSA data dashboard/)
await page.getByRole('heading', { name: 'Respiratory viruses', level: 1 })
})

test.describe('Topic pages', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/')
})

test('COVID-19', async ({ page }) => {
await page.getByRole('navigation', { name: 'Menu' }).getByRole('link', { name: 'COVID-19' }).click()
await expect(page).toHaveTitle(/COVID-19 | UKHSA data dashboard/)
await page.getByRole('heading', { name: 'COVID-19', level: 1 })
})

test('Influenza', async ({ page }) => {
await page.getByRole('navigation', { name: 'Menu' }).getByRole('link', { name: 'Influenza' }).click()
await expect(page).toHaveTitle(/Influenza | UKHSA data dashboard/)
await page.getByRole('heading', { name: 'Influenza', level: 1 })
})

test('Other respiratory viruses', async ({ page }) => {
await page
.getByRole('navigation', { name: 'Menu' })
.getByRole('link', { name: 'Other respiratory viruses' })
.click()
await expect(page).toHaveTitle(/Other respiratory viruses | UKHSA data dashboard/)
await page.getByRole('heading', { name: 'Other respiratory viruses', level: 1 })
})
})

test.describe('Common pages', () => {
test('About', async ({ page }) => {
await page.goto('/about')
await expect(page).toHaveTitle(/About | UKHSA data dashboard/)
await page.getByRole('heading', { name: 'About', level: 1 })
})

test('Whats new', async ({ page }) => {
await page.goto('/whats-new')
await expect(page).toHaveTitle(/What's new | UKHSA data dashboard/)
await page.getByRole('heading', { name: "What's new", level: 1 })
})
})
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"export": "next export",
"test": "jest --watch",
"test:ci": "jest --ci --coverage",
"test:e2e": "cypress open --e2e",
"test:e2e:ci": "cypress run --e2e",
"test:e2e": "npx playwright test --ui",
"test:e2e:ci": "npx playwright test",
"test:e2e:smoke": "cypress run --spec \"cypress/smoke/*\"",
"prepare": "husky install"
},
Expand All @@ -38,6 +38,7 @@
"zod-error": "^1.5.0"
},
"devDependencies": {
"@playwright/test": "^1.36.2",
"@testing-library/cypress": "^9.0.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
Expand Down
78 changes: 78 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { defineConfig, devices } from '@playwright/test'

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testIgnore: '**/src/**',
testDir: './e2e',
/* 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://localhost:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
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' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'NODE_ENV=test npm run build && NODE_ENV=test npm run start',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
})

0 comments on commit 2a53e5a

Please sign in to comment.