import {Frame, Locator, Page, expect} from '@playwright/test'; import {getPadBody, getPadOuter, goToNewPad, goToPad} from 'ep_etherpad-lite/tests/frontend-new/helper/padHelper'; // Reproduces the legacy `utils.aNewPad`: open a new pad, then wait for // ep_comments_page's postAceInit to finish (initDone is a Promise that // resolves once plugin init is complete). export const aNewCommentsPad = async (page: Page): Promise => { const padId = await goToNewPad(page); await waitForCommentsInit(page); return padId; }; export const reopenCommentsPad = async (page: Page, padId: string): Promise => { await goToPad(page, padId); await waitForCommentsInit(page); }; // Reopen the same pad as a fresh user so a new authorId is allocated. Used // by the delete/edit "other user" specs which assert that one user cannot // modify another user's comment. Plain reopenCommentsPad reuses the same // session cookies (token / express_sid), so Etherpad keeps the original // authorId and the auth-check on delete/edit short-circuits to success. export const reopenCommentsPadAsFreshUser = async ( page: Page, padId: string, ): Promise => { await page.context().clearCookies(); await page.evaluate(() => { try { window.localStorage.clear(); } catch {} try { window.sessionStorage.clear(); } catch {} }).catch(() => {}); await goToPad(page, padId); await waitForCommentsInit(page); }; export const waitForCommentsInit = async (page: Page): Promise => { await expect.poll(async () => page.evaluate(async () => { const w = window as any; const ep = w.pad && w.pad.plugins && w.pad.plugins.ep_comments_page; if (!ep || !ep.initDone) return false; await ep.initDone; return true; }), {timeout: 60_000}).toBe(true); }; // Returns the inner pad body (Locator). export const inner = async (page: Page): Promise => getPadBody(page); // Returns the outer iframe Frame (where #commentIcons / sidebar comments live). export const outer = async (page: Page): Promise => getPadOuter(page); export const enlargeScreen = async (page: Page, maxWidth = '3000px'): Promise => { await page.evaluate((mw) => { const ifr = document.querySelector('#iframe-container iframe'); if (ifr) ifr.style.maxWidth = mw; }, maxWidth); }; export const restoreScreen = async (page: Page): Promise => { await page.evaluate(() => { const ifr = document.querySelector('#iframe-container iframe'); if (ifr) ifr.style.maxWidth = ''; }); }; // Replace pad content with provided lines (joined with \n). Mirrors the // legacy cleanPad + sendkeys pattern. export const setPadLines = async (page: Page, lines: string[]): Promise => { const body = await getPadBody(page); await body.click(); await page.keyboard.press('Control+A'); await page.keyboard.press('Delete'); // Wait for first line to be empty. await expect.poll(async () => body.locator('div').count()).toBeGreaterThan(0); for (let i = 0; i < lines.length; i++) { await page.keyboard.type(lines[i]); if (i < lines.length - 1) await page.keyboard.press('Enter'); } await expect.poll(async () => body.locator('div').count()) .toBeGreaterThanOrEqual(lines.length); }; // Select all text on a given line index (0-based). Implemented by triple-click // on the corresponding div, which selects the line content. export const selectLine = async (page: Page, lineIndex: number): Promise => { const body = await getPadBody(page); const line = body.locator('div').nth(lineIndex); await line.click(); // Triple-click to select the entire line content. await line.click({clickCount: 3}); }; // Clicks the toolbar Add Comment button (lives in the chrome page). export const clickAddCommentButton = async (page: Page): Promise => { await page.locator('.addComment').first().click(); }; export const fillCommentForm = async ( page: Page, commentText: string, suggestion?: string, ): Promise => { const field = page.locator('textarea.comment-content'); await field.fill(commentText); if (suggestion !== undefined) { // Click the label (not the input): the adjacent