diff --git a/.github/workflows/playwright.yml b/.github/workflows/playwright.yml new file mode 100644 index 0000000000..7a84e5b07e --- /dev/null +++ b/.github/workflows/playwright.yml @@ -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@v4 + - uses: actions/setup-node@v4 + with: + node-version: lts/* + - name: Install dependencies + run: npm ci + - name: Install Playwright Browsers + run: npx playwright install --with-deps + - name: Run Playwright tests + run: npm run test:e2e:headless + - uses: actions/upload-artifact@v4 + if: ${{ !cancelled() }} + with: + name: playwright-report + path: playwright-report/ + retention-days: 30 diff --git a/.gitignore b/.gitignore index febbb5c964..86463a2582 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ /node_modules /build + +# Playwright +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ diff --git a/package-lock.json b/package-lock.json index 63fbde8b95..220cf25fa7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,6 +46,7 @@ }, "devDependencies": { "@opencast/eslint-config-ts-react": "^0.3.0", + "@playwright/test": "^1.52.0", "@redux-devtools/extension": "^3.3.0", "@types/lodash": "^4.17.16", "@types/node": "^22.15.18", @@ -1127,6 +1128,22 @@ "url": "https://opencollective.com/parcel" } }, + "node_modules/@playwright/test": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.52.0.tgz", + "integrity": "sha512-uh6W7sb55hl7D6vsAeA+V2p5JnlAqzhqFyF0VcJkKZXkgnFcVG9PziERRHQfPLfNGx1C292a4JqbWzhR8L4R1g==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.52.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/@popperjs/core": { "version": "2.11.8", "license": "MIT", @@ -5044,6 +5061,53 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/playwright": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.52.0.tgz", + "integrity": "sha512-JAwMNMBlxJ2oD1kce4KPtMkDeKGHQstdpFPcPH3maElAXon/QZeTvtsfXmTMRyO9TslfoYOXkSsvao2nE1ilTw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.52.0" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, + "node_modules/playwright-core": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.52.0.tgz", + "integrity": "sha512-l2osTgLXSMeuLZOML9qYODUQoPPnUsKsb5/P6LJ2e6uPKXUdPK5WYhN4z03G+YNbWmGDY4YENauNu4ZKczreHg==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/playwright/node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/pkce-challenge": { "version": "5.0.0", "dev": true, diff --git a/package.json b/package.json index 784ad387d8..99091e2da1 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,10 @@ "start": "vite", "build": "tsc && eslint . --max-warnings=0 && vite build", "serve": "vite preview", - "test": "vitest" + "test": "vitest", + "test:serve": "vite build && vite preview", + "test:e2e": "playwright test --ui", + "test:e2e:headless": "playwright test" }, "browserslist": { "production": [ @@ -61,6 +64,7 @@ }, "devDependencies": { "@opencast/eslint-config-ts-react": "^0.3.0", + "@playwright/test": "^1.52.0", "@redux-devtools/extension": "^3.3.0", "@types/lodash": "^4.17.16", "@types/node": "^22.15.18", diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000000..4d6a259edf --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,78 @@ +import { defineConfig, devices } from "@playwright/test"; + +/** + * Read environment variables from file. + * https://github.com/motdotla/dotenv + */ +// import dotenv from 'dotenv'; +// import path from 'path'; +// dotenv.config({ path: path.resolve(__dirname, '.env') }); + +/** + * See https://playwright.dev/docs/test-configuration. + */ +export default defineConfig({ + testDir: "./webtests", + /* 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: process.env.PLAYWRIGHT_TEST_BASE_URL || "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: "npm start", + timeout: 120 * 1000, // increase the timeout to 120s (from default 60s) for the server to start + }, +}); diff --git a/webtests/header.spec.ts b/webtests/header.spec.ts new file mode 100644 index 0000000000..48599af338 --- /dev/null +++ b/webtests/header.spec.ts @@ -0,0 +1,26 @@ +import { test, expect, type Page } from '@playwright/test'; + +test.beforeEach(async ({ page }) => { + await page.goto('/'); +}); +test.describe("Header", () => { + test('check existence of header items', async ({ page }) => { + await expect(page.getByRole('banner')).toBeVisible(); + await expect(page.locator('#lang-dd')).toBeVisible(); + await expect(page.locator('div:nth-child(2)').first()).toBeVisible(); + await expect(page.locator('#nav-dd-container > div:nth-child(3)')).toBeVisible(); + await expect(page.locator('#info-dd')).toBeVisible(); + await expect(page.locator('#help-dd')).toBeVisible(); + }); + + test('check language change dropdown', async ({ page }) => { + test.setTimeout(120_000); + await page.goto("/#/recordings/recordings"); + await page.locator('#lang-dd').click(); + await page.getByRole('button', { name: 'English (US)' }).click(); + await expect(page.locator('h1')).toContainText('Locations', {timeout: 120_000}); // the full page reload after changing the color takes much longer on the firefox browser + await page.locator('#lang-dd').click(); + await page.getByRole('button', { name: 'Deutsch' }).click(); + await expect(page.locator('h1')).toContainText('Standorte', {timeout: 120_000}); + }); +}); diff --git a/webtests/writing_tests.md b/webtests/writing_tests.md new file mode 100644 index 0000000000..b6b25d0f5c --- /dev/null +++ b/webtests/writing_tests.md @@ -0,0 +1,53 @@ +# Frontend End-to-End Testing with Playwright + +## Overview + +Playwright is a testing framework that allows automated testing of web applications across multiple browsers (Chromium, Firefox, WebKit). + +## Setup Browsers + +There are multiple browsers setup in `../playwright.config.ts`: + +General browsers: + +- Desktop Chromium +- Desktop Firefox +- Desktop Safari + +Branded Browsers: + +- Microsoft Edge +- Google Chrome + +## Basic Structure + +1. Import Playwright test modules +2. Define test scenarios with `test()` function +3. Use page interactions (click, fill, etc.) +4. Assert expected outcomes + +## Developing tests + +### Installation + +Besides the dependencies in the ```package.json``` file which need to be install to run the webserver at all, playwright has browser dependencies as well. To install the extra broser dependencies, run: ```npx playwright install --with-deps``` + +### Run tests locally + +To execute the tests locally, there are multiple options: + +**Running the tests headless:** +Execute `npm run test:e2e:headless` or `npx playwright test` in the terminal. + +**Running the tests in the UI:** +Execute `npm run test:e2e` or `npx playwright test --ui` in the terminal. + +In both cases you can add `--trace on` to get a trace view of the tests. + +### Developing + +Please have a look at the official playwright documentation on how to develop new tests. + +## References + +- [Playwright Documentation](https://playwright.dev/docs/intro)