AFFiNE/tests/kit/utils/page-logic.ts

100 lines
3.0 KiB
TypeScript
Raw Normal View History

import type { Locator, Page } from '@playwright/test';
import { expect } from '@playwright/test';
2023-01-03 16:57:33 +03:00
export async function waitForEditorLoad(page: Page) {
await page.waitForSelector('v-line', {
timeout: 20000,
});
2023-03-28 08:25:16 +03:00
}
2023-08-11 09:59:06 +03:00
export async function waitForAllPagesLoad(page: Page) {
// if filters tag is rendered, we believe all_pages is ready
await page.waitForSelector('[data-testid="create-first-filter"]', {
timeout: 1000,
});
}
2023-09-02 08:57:04 +03:00
export async function clickNewPageButton(page: Page) {
2024-02-21 15:57:18 +03:00
//FiXME: when the page is in edgeless mode, clickNewPageButton will create a new edgeless page
const edgelessPage = page.locator('edgeless-editor');
if (await edgelessPage.isVisible()) {
await page.getByTestId('switch-page-mode-button').click({
delay: 100,
});
}
// fixme(himself65): if too fast, the page will crash
await page.getByTestId('sidebar-new-page-button').click({
delay: 100,
});
2024-02-06 06:20:54 +03:00
await waitForEmptyEditor(page);
}
export async function waitForEmptyEditor(page: Page) {
await expect(page.locator('.doc-title-container-empty')).toBeVisible();
}
export function getBlockSuiteEditorTitle(page: Page) {
return page.locator('doc-title .inline-editor').nth(0);
}
export async function type(page: Page, content: string, delay = 50) {
await page.keyboard.type(content, { delay });
}
export const createLinkedPage = async (page: Page, pageName?: string) => {
await page.keyboard.type('@', { delay: 50 });
2024-01-18 12:26:31 +03:00
const linkedPagePopover = page.locator('.linked-doc-popover');
await expect(linkedPagePopover).toBeVisible();
if (pageName) {
await type(page, pageName);
} else {
pageName = 'Untitled';
}
await page.keyboard.press('ArrowUp');
await page.keyboard.press('ArrowUp');
await page.keyboard.press('Enter', { delay: 50 });
};
2023-01-03 16:57:33 +03:00
export async function clickPageMoreActions(page: Page) {
return page
.getByTestId('header')
2023-08-08 20:14:24 +03:00
.getByTestId('header-dropDownButton')
.click();
}
export const getPageOperationButton = (page: Page, id: string) => {
return getPageItem(page, id).getByTestId('page-list-operation-button');
};
export const getPageItem = (page: Page, id: string) => {
return page.locator(`[data-page-id="${id}"][data-testid="page-list-item"]`);
};
export const getPageByTitle = (page: Page, title: string) => {
return page.getByTestId('page-list-item').getByText(title);
};
export const dragTo = async (page: Page, locator: Locator, target: Locator) => {
await locator.hover();
await page.mouse.down();
await page.waitForTimeout(1000);
const targetElement = await target.boundingBox();
if (!targetElement) {
throw new Error('target element not found');
}
await page.mouse.move(targetElement.x, targetElement.y);
await target.hover();
await page.mouse.up();
};
2024-03-25 05:46:31 +03:00
// sometimes editor loses focus, this function is to focus the editor
export const focusInlineEditor = async (page: Page) => {
await page
.locator(
`.affine-paragraph-rich-text-wrapper:has(.visible):has-text("Type '/' for commands")`
)
.locator('.inline-editor')
.focus();
};