refactor(e2e): create editor helpers (#54701)

This commit is contained in:
Oliver Eyton-Williams
2024-05-08 20:21:56 +02:00
committed by GitHub
parent c7d3b1303e
commit ca4e5db8b1
10 changed files with 93 additions and 82 deletions

48
e2e/utils/editor.ts Normal file
View File

@@ -0,0 +1,48 @@
import { type Page } from '@playwright/test';
/**
* Retrieves any editor elements on the page.
* @param page - The Playwright page object.
* @returns The editor elements.
*/
export function getEditors(page: Page) {
return page.getByLabel(
'Editor content;Press Alt+F1 for Accessibility Options'
);
}
export async function focusEditor({
page,
isMobile,
browserName
}: {
page: Page;
isMobile: boolean;
browserName: string;
}) {
// The editor has an overlay div, which prevents the click event from bubbling up in iOS Safari.
// This is a quirk in this browser-OS combination, and the workaround here is to use `.focus()`
// in place of `.click()` to focus on the editor.
// Ref: https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
if (isMobile && browserName === 'webkit') {
await getEditors(page).focus();
} else {
await getEditors(page).click();
}
}
export async function clearEditor({
page,
browserName
}: {
page: Page;
browserName: string;
}) {
// TODO: replace with ControlOrMeta when it's supported
if (browserName === 'webkit') {
await page.keyboard.press('Meta+a');
} else {
await page.keyboard.press('Control+a');
}
await page.keyboard.press('Backspace');
}