AFFiNE/tests/kit/utils/keyboard.ts

66 lines
1.7 KiB
TypeScript
Raw Normal View History

import type { Page } from '@playwright/test';
const IS_MAC = process.platform === 'darwin';
async function keyDownCtrlOrMeta(page: Page) {
if (IS_MAC) {
await page.keyboard.down('Meta');
} else {
await page.keyboard.down('Control');
}
}
async function keyUpCtrlOrMeta(page: Page) {
if (IS_MAC) {
await page.keyboard.up('Meta');
} else {
await page.keyboard.up('Control');
}
}
// It's not good enough, but better than calling keyDownCtrlOrMeta and keyUpCtrlOrMeta separately
export const withCtrlOrMeta = async (page: Page, fn: () => Promise<void>) => {
await keyDownCtrlOrMeta(page);
await fn();
await keyUpCtrlOrMeta(page);
};
export async function pressEnter(page: Page) {
// avoid flaky test by simulate real user input
await page.keyboard.press('Enter', { delay: 50 });
}
export async function pressTab(page: Page) {
await page.keyboard.press('Tab', { delay: 50 });
}
export async function pressShiftTab(page: Page) {
await page.keyboard.down('Shift');
await page.keyboard.press('Tab', { delay: 50 });
await page.keyboard.up('Shift');
}
export async function pressShiftEnter(page: Page) {
await page.keyboard.down('Shift');
await page.keyboard.press('Enter', { delay: 50 });
await page.keyboard.up('Shift');
}
export async function copyByKeyboard(page: Page) {
await keyDownCtrlOrMeta(page);
await page.keyboard.press('c', { delay: 50 });
await keyUpCtrlOrMeta(page);
}
export async function cutByKeyboard(page: Page) {
await keyDownCtrlOrMeta(page);
await page.keyboard.press('x', { delay: 50 });
await keyUpCtrlOrMeta(page);
}
export async function pasteByKeyboard(page: Page) {
await keyDownCtrlOrMeta(page);
await page.keyboard.press('v', { delay: 50 });
await keyUpCtrlOrMeta(page);
}