From 24700dca2a79eb4693dc5edda8ceabbc7bc121d5 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 6 Nov 2026 20:44:00 +0000 Subject: [PATCH] test(e2e): retry clipboard reads rejected by Firefox (#40155) The Firefox mermaid e2e test fails intermittently with `Clipboard read operation is not allowed.`, for example in https://github.com/go-gitea/gitea/actions/runs/41281998298/job/124776072700. Firefox rejects `readText()` when it overlaps the page's still pending clipboard write, and `expect.poll` does not retry errors thrown by its callback, so one overlapping read fails the test. https://github.com/go-gitea/gitea/pull/39838 only removed the paste prompt, not this rejection. - A new `assertClipboardText` helper treats a rejected read as a mismatch so it is polled again - The bot token copy test had the same race and uses it too _Written by Claude (Opus 5.5)._ --- tests/e2e/admin.test.ts | 6 +++--- tests/e2e/mermaid.test.ts | 4 ++-- tests/e2e/utils.ts | 11 +++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/e2e/admin.test.ts b/tests/e2e/admin.test.ts index 60cf9fe6a58..e2c0a1af173 100644 --- a/tests/e2e/admin.test.ts +++ b/tests/e2e/admin.test.ts @@ -1,6 +1,6 @@ import {env} from 'node:process'; import {test, expect} from '@playwright/test'; -import {clickDropdownItem, login, randomString} from './utils.ts'; +import {assertClipboardText, clickDropdownItem, login, randomString} from './utils.ts'; test('create a bot and manage its access token', async ({page, request}) => { const botName = `e2e-bot-${randomString(8)}`; @@ -15,9 +15,9 @@ test('create a bot and manage its access token', async ({page, request}) => { await page.getByLabel('Token Name').fill('e2e-token'); await page.getByRole('row', {name: /^user /}).getByRole('radio', {name: 'Read', exact: true}).check(); await page.getByRole('button', {name: 'Generate Token'}).click(); - const token = await page.getByRole('code').textContent(); + const token = (await page.getByRole('code').textContent())!; await page.getByRole('button', {name: 'Copy', exact: true}).click(); - await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(token); + await assertClipboardText(page, token); const response = await request.get('/api/v1/user', {headers: {Authorization: `token ${token}`}}); expect(await response.json()).toMatchObject({login: botName, type: 'Bot'}); diff --git a/tests/e2e/mermaid.test.ts b/tests/e2e/mermaid.test.ts index eb1d8567e69..118646b782b 100644 --- a/tests/e2e/mermaid.test.ts +++ b/tests/e2e/mermaid.test.ts @@ -1,6 +1,6 @@ import {env} from 'node:process'; import {expect, test} from '@playwright/test'; -import {apiCreateRepo, apiCreateIssue, assertNoJsError, randomString} from './utils.ts'; +import {apiCreateRepo, apiCreateIssue, assertClipboardText, assertNoJsError, randomString} from './utils.ts'; test('mermaid diagram in issue', async ({page, request}) => { const repoName = `e2e-mermaid-${randomString(8)}`; @@ -22,7 +22,7 @@ test('mermaid diagram in issue', async ({page, request}) => { await svg.hover(); await page.locator('.code-copy').click(); - await expect.poll(() => page.evaluate(() => navigator.clipboard.readText())).toBe(source); + await assertClipboardText(page, source); await assertNoJsError(page); }); diff --git a/tests/e2e/utils.ts b/tests/e2e/utils.ts index 26201363042..7d330c51bf4 100644 --- a/tests/e2e/utils.ts +++ b/tests/e2e/utils.ts @@ -247,6 +247,17 @@ export async function login(page: Page, username = env.GITEA_TEST_E2E_USER, pass if (status !== 302 && status !== 303) throw new Error(`login as ${username} failed: HTTP ${status}`); } +export async function assertClipboardText(page: Page, text: string) { + // Firefox rejects a read that overlaps the page's still pending clipboard write + await expect.poll(() => page.evaluate(async () => { + try { + return await navigator.clipboard.readText(); + } catch { + return null; + } + })).toBe(text); +} + export async function assertNoJsError(page: Page) { await expect(page.locator('.js-global-error')).toHaveCount(0); }