import { expect, type Page } from "@playwright/test"; const continueInFullscreenName = "Продолжить в полном экране"; /** * Resizes the viewport the way a user would while taking a real attempt. * * A real attempt holds the page in fullscreen, and the browser refuses to * resize a fullscreen window. The helper leaves fullscreen first. The page then * covers the task, as it does for a candidate who pressed Escape, so the helper * returns through the cover button, which also asks for fullscreen again, and * waits for the task to be back. Pages that never entered fullscreen are * resized directly. */ export async function resizeViewport( page: Page, size: { width: number; height: number }, ): Promise { const wasFullscreen = await page.evaluate(() => document.fullscreenElement !== null); if (wasFullscreen) { await page.evaluate(() => document.exitFullscreen()); await expect.poll(() => page.evaluate(() => document.fullscreenElement)).toBeNull(); // The window can stay in the fullscreen state for a moment after the // document left it, and the browser refuses the resize until it is restored. await expect(async () => { await page.setViewportSize(size); }).toPass({ timeout: 10_000 }); } else { await page.setViewportSize(size); } if (!wasFullscreen) return; const continueButton = page.getByRole("button", { name: continueInFullscreenName }); await expect(continueButton).toBeVisible(); await continueButton.click(); await expect(continueButton).toHaveCount(0); await expect.poll(() => page.evaluate(() => document.fullscreenElement !== null)).toBe(true); } export async function expectNoHorizontalOverflow(page: Page, width: number): Promise { await resizeViewport(page, { width, height: 900 }); const overflow = await page.evaluate( () => document.documentElement.scrollWidth > window.innerWidth, ); expect(overflow, `no horizontal overflow at ${width}px`).toBe(false); }