diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index fe7788994a..2d5e6137fb 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -73,6 +73,18 @@ jobs: # The release notes page reads releases from the GitHub API, and the # script fails in CI without a token. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: 🎭 Install Playwright Browsers + run: npx playwright install --with-deps chromium + # Runs against the site built above. + - name: 🎭 Run E2E Tests + run: npm run test:e2e + - name: 📤 Upload Playwright Report + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + if: ${{ !cancelled() }} + with: + name: playwright-report + path: playwright-report/ + retention-days: 14 cross-platform: name: Cross-platform on ${{ matrix.os }} diff --git a/.gitignore b/.gitignore index 37f55c23be..4e3906368a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,12 @@ .cspellcache src/components/page/reference/ReleaseNotes/release-notes.json +# Playwright +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ + # Misc .DS_Store .env.local diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8a47e8f2b7..dc8538349b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,6 +87,18 @@ npm run build:preview This builds the English site only, the same build as a Vercel preview. +### Browser Tests + +[Playwright](https://playwright.dev/) tests in `e2e/` check the playgrounds and framework tabs in a browser. They run against the built site, so build it first: + +```sh +npm run build:preview +npx playwright install chromium +npm run test:e2e +``` + +A failed run opens the HTML report. Run `npx playwright show-report` to open it after any run. + ### Linting Documentation This repository uses [ESLint](https://eslint.org/) to catch markup the build cannot check, such as raw links and headings and misaligned JSDoc comments, and [Prettier](https://prettier.io/), an opinionated code formatter, to keep consistent formatting throughout the documentation. Run the following command to report ESLint errors and automatically fix all formatting, and then push any changes: diff --git a/e2e/playground.e2e.ts b/e2e/playground.e2e.ts new file mode 100644 index 0000000000..493991dd73 --- /dev/null +++ b/e2e/playground.e2e.ts @@ -0,0 +1,186 @@ +import { readFileSync } from 'node:fs'; +import { expect, test, type Locator, type Page } from '@playwright/test'; + +/** The tab lists of the docs' framework tabs. Only their tabs control a panel, which tells them apart from a playground's. */ +const docsFrameworkTabs = (page: Page) => page.locator('[role=tablist]:has([role=tab][aria-controls])'); + +const frameworkTab = (scope: Locator, name: string) => + scope.getByRole('tablist', { name: 'Framework' }).getByRole('tab', { name, exact: true }); + +/** + * The modal page's "child state" playground, which has several files per framework and no JavaScript code. + * + * Its demo only loads once it scrolls into view, and its files change with the framework, so it is found + * by position rather than by what it contains. + */ +async function openChildStatePlayground(page: Page) { + await page.goto('api/modal'); + const playgrounds = page.locator('.playground'); + const childState = playgrounds.filter({ has: page.getByRole('tab', { name: 'src/app/child.component.ts' }) }); + await expect(childState).toHaveCount(1); + + const index = await playgrounds.evaluateAll((elements) => + elements.findIndex((element) => element.textContent?.includes('src/app/child.component.ts')) + ); + return playgrounds.nth(index); +} + +/** The source of an example file, as written in its Markdown code fence. */ +function readExampleFile(path: string) { + const markdown = readFileSync(`static/usage/v9/modal/can-dismiss/child-state/${path}`, 'utf8'); + return markdown.replace(/^```\w*\n/, '').replace(/\n```\n*$/, ''); +} + +test.describe('framework choice', () => { + test('is shared between the docs tabs and playgrounds, and kept across reloads', async ({ page }) => { + await page.goto('api/alert'); + const docsTabs = docsFrameworkTabs(page).first(); + const playground = page.locator('.playground').first(); + + await docsTabs.getByRole('tab', { name: 'React', exact: true }).click(); + await expect(frameworkTab(playground, 'React')).toHaveAttribute('aria-selected', 'true'); + + await frameworkTab(playground, 'Vue').click(); + await expect(docsTabs.getByRole('tab', { name: 'Vue', exact: true })).toHaveAttribute('aria-selected', 'true'); + + await page.reload(); + await expect(frameworkTab(playground, 'Vue')).toHaveAttribute('aria-selected', 'true'); + await expect(docsTabs.getByRole('tab', { name: 'Vue', exact: true })).toHaveAttribute('aria-selected', 'true'); + }); + + test('shows an Angular (Standalone) pick as Angular where there is no Standalone code', async ({ page }) => { + await page.addInitScript(() => localStorage.setItem('docusaurus.tab.framework', 'angular-standalone')); + await page.goto('api/alert'); + + await expect(frameworkTab(page.locator('.playground').first(), 'Angular')).toHaveAttribute('aria-selected', 'true'); + }); + + test('disables a framework the playground has no code for, and skips it with the arrow keys', async ({ page }) => { + const playground = await openChildStatePlayground(page); + + await expect(frameworkTab(playground, 'JavaScript')).toBeDisabled(); + + await frameworkTab(playground, 'Angular').focus(); + await page.keyboard.press('ArrowRight'); + await expect(frameworkTab(playground, 'React')).toBeFocused(); + + await page.keyboard.press('Enter'); + await expect(frameworkTab(playground, 'React')).toHaveAttribute('aria-selected', 'true'); + }); +}); + +test.describe('file tabs', () => { + test('show one file at a time', async ({ page }) => { + const playground = await openChildStatePlayground(page); + const fileTabs = playground.locator('.playground__tabs'); + + await expect(fileTabs.getByRole('tab')).toHaveText([ + 'src/app/example.component.html', + 'src/app/example.component.ts', + 'src/app/child.component.html', + 'src/app/child.component.ts', + ]); + await expect(playground.locator('#playground_angular_src_app_example_component_html')).toBeVisible(); + + await fileTabs.getByRole('tab', { name: 'src/app/child.component.ts' }).click(); + await expect(playground.locator('#playground_angular_src_app_child_component_ts')).toBeVisible(); + await expect(playground.locator('#playground_angular_src_app_example_component_html')).toBeHidden(); + }); + + test('select the first file after switching frameworks', async ({ page }) => { + const playground = await openChildStatePlayground(page); + const fileTabs = playground.locator('.playground__tabs'); + + await fileTabs.getByRole('tab', { name: 'src/app/child.component.ts' }).click(); + await frameworkTab(playground, 'Vue').click(); + + await expect(fileTabs.getByRole('tab', { name: 'src/components/Example.vue' })).toHaveAttribute( + 'aria-selected', + 'true' + ); + await expect(playground.locator('#playground_vue_src_components_Example_vue')).toBeVisible(); + }); + + test('move focus with the arrow keys and select with Enter', async ({ page }) => { + const playground = await openChildStatePlayground(page); + const fileTabs = playground.locator('.playground__tabs'); + const secondTab = fileTabs.getByRole('tab', { name: 'src/app/example.component.ts' }); + + await fileTabs.getByRole('tab', { name: 'src/app/example.component.html' }).focus(); + await page.keyboard.press('ArrowRight'); + await expect(secondTab).toBeFocused(); + await expect(secondTab).toHaveAttribute('aria-selected', 'false'); + + await page.keyboard.press('Enter'); + await expect(secondTab).toHaveAttribute('aria-selected', 'true'); + }); +}); + +test('StackBlitz receives every file of a multi-file example', async ({ page, context }) => { + // The SDK posts the project to StackBlitz in a new window. Answer it here, so the test stays offline. + const request = new Promise((resolve) => { + context.route('https://stackblitz.com/**', (route) => { + resolve(new URLSearchParams(route.request().postData() ?? '')); + route.fulfill({ body: '' }); + }); + }); + + const playground = await openChildStatePlayground(page); + await expect(playground.locator('.playground__tabs')).toBeVisible(); + + // The StackBlitz button is the first icon button, and has no accessible name to find it by. + await playground.locator('.playground__control-group--end button').first().click(); + const form = await request; + + for (const [file, source] of [ + ['src/app/example.component.html', 'angular/example_component_html.md'], + ['src/app/example.component.ts', 'angular/example_component_ts.md'], + ['src/app/child.component.html', 'angular/child_component_html.md'], + ['src/app/child.component.ts', 'angular/child_component_ts.md'], + ]) { + // A form post sends every line break as CRLF. + expect(form.get(`project[files][${file}]`)?.replace(/\r\n/g, '\n'), file).toBe(readExampleFile(source)); + } +}); + +test.describe('mode', () => { + test('switches the demo and is kept by every playground on the page', async ({ page }) => { + await page.goto('api/alert'); + const playgrounds = page.locator('.playground'); + const first = playgrounds.first(); + const last = playgrounds.last(); + + await first.getByRole('button', { name: 'MD', exact: true }).click(); + await expect(first.locator('iframe[src*="ionic:mode=md"]')).toBeVisible(); + await expect(first.locator('iframe[src*="ionic:mode=ios"]')).toBeHidden(); + + // A playground out of view picks up the mode once it scrolls into view. + await last.scrollIntoViewIfNeeded(); + await expect(last.locator('iframe[src*="ionic:mode=md"]')).toBeVisible(); + + await page.reload(); + await first.scrollIntoViewIfNeeded(); + await expect(first.locator('iframe[src*="ionic:mode=md"]')).toBeVisible(); + }); + + test('disables the other mode for a playground restricted to one, and skips it with the Tab key', async ({ + page, + }) => { + // A stored iOS pick must not override a playground that only runs in MD. + await page.addInitScript(() => localStorage.setItem('playground_mode', 'ios')); + await page.goto('api/input'); + + // The "fill" playground is the input page's only MD-only playground. + const playground = page + .locator('.playground') + .filter({ has: page.getByRole('button', { name: 'iOS', exact: true, disabled: true }) }); + await playground.scrollIntoViewIfNeeded(); + + await expect(playground.locator('iframe[src*="ionic:mode=md"]')).toBeVisible(); + await expect(playground.locator('iframe[src*="ionic:mode=ios"]')).toBeHidden(); + + await playground.getByRole('tablist', { name: 'Framework' }).getByRole('tab', { selected: true }).focus(); + await page.keyboard.press('Tab'); + await expect(playground.getByRole('button', { name: 'MD', exact: true })).toBeFocused(); + }); +}); diff --git a/package-lock.json b/package-lock.json index b088ce2181..f952d2e615 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38,6 +38,7 @@ "@docusaurus/tsconfig": "^3.10.2", "@docusaurus/types": "^3.10.2", "@ionic/prettier-config": "^4.0.0", + "@playwright/test": "^1.65.1", "@types/react": "^19.2.18", "@typescript-eslint/parser": "^8.72.1", "cspell": "^10.0.1", @@ -6913,6 +6914,22 @@ "url": "https://opencollective.com/pkgr" } }, + "node_modules/@playwright/test": { + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.65.1.tgz", + "integrity": "sha512-y+NCIRm2LvY4slUVWvGh1nujJ8RF/hEisIsq2mtUPgdQC4L701bq5hZGMhrpeXjACvwvO9tj5dJqhvM6TP9lbw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.65.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/@pnpm/config.env-replace": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@pnpm/config.env-replace/-/config.env-replace-1.1.0.tgz", @@ -19078,6 +19095,35 @@ "node": ">=16.0.0" } }, + "node_modules/playwright": { + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.65.1.tgz", + "integrity": "sha512-vJ1FuoOrNwOKHUIVEvSt3jT1L+wjjDpwhwIstKtfvTh8XIG4FG4bIFuvNdMxi81LGfAhUgUZleVFaOLY4RIUUw==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.65.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.65.1.tgz", + "integrity": "sha512-hqPpz7blSMPxWVdoeE1yiBSzXsKiVOd2k+kjCvmUywD2GPO2Dd9HfcWnq9NN0GHbNZDFgZ9JfBFr+X5/BLo0qw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/postcss": { "version": "8.5.25", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.25.tgz", diff --git a/package.json b/package.json index c9e7045aab..a9dd92c318 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "prettier": "prettier \"./**/*.{html,ts,tsx,js,jsx,md,mdx}\" --cache", "start": "docusaurus start", "test": "vitest run", + "test:e2e": "playwright test", "typecheck": "tsc --noEmit", "swizzle": "docusaurus swizzle", "spellcheck": "cspell --no-progress --cache --gitignore \"**/*.{md,mdx}\"" @@ -69,6 +70,7 @@ "@docusaurus/tsconfig": "^3.10.2", "@docusaurus/types": "^3.10.2", "@ionic/prettier-config": "^4.0.0", + "@playwright/test": "^1.65.1", "@types/react": "^19.2.18", "@typescript-eslint/parser": "^8.72.1", "cspell": "^10.0.1", diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000000..a3a33ddc2c --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,36 @@ +/** + * Browser tests for the site's interactive components, run against the built + * site. Run `npm run build:preview` first, then `npm run test:e2e`. + * + * Tests end in `.e2e.ts`, as in ionic-framework, because Vitest picks up every + * `.spec.ts` and `.test.ts`. + */ + +import { defineConfig, devices } from '@playwright/test'; + +const baseURL = 'http://localhost:3100/docs/'; + +export default defineConfig({ + testDir: './e2e', + testMatch: '*.e2e.ts', + fullyParallel: true, + forbidOnly: !!process.env.CI, + retries: process.env.CI ? 2 : 0, + reporter: process.env.CI ? [['github'], ['html', { open: 'never' }]] : [['list'], ['html']], + use: { + baseURL, + trace: 'on-first-retry', + }, + projects: [ + { + name: 'Desktop Chrome', + use: { ...devices['Desktop Chrome'] }, + }, + ], + webServer: { + command: 'npm run serve -- --port 3100 --no-open', + url: baseURL, + reuseExistingServer: !process.env.CI, + timeout: 120_000, + }, +}); diff --git a/tsconfig.json b/tsconfig.json index f231245b60..15a6ca48f2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -29,10 +29,10 @@ "strict": true }, /* - * `src` holds every TypeScript file in the project, and `index.d.ts` declares the - * `*.module.scss` imports those files rely on. Listing them explicitly also restores - * TypeScript's default excludes, notably `node_modules`, which the previous - * `exclude`-only config silently overrode. + * `src`, `e2e` and the Playwright config hold every TypeScript file in the project, + * and `index.d.ts` declares the `*.module.scss` imports those files rely on. Listing + * them explicitly also restores TypeScript's default excludes, notably `node_modules`, + * which the previous `exclude`-only config silently overrode. */ - "include": ["src", "index.d.ts"] + "include": ["src", "e2e", "playwright.config.ts", "index.d.ts"] }