|
| 1 | +/* |
| 2 | + * Keyman is copyright (C) SIL Global. MIT License. |
| 3 | + */ |
| 4 | + |
| 5 | +import { type Locator, type Page } from "@playwright/test"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Expands the keyboard selection menu and returns the text content of the |
| 9 | + * currently selected keyboard. |
| 10 | + */ |
| 11 | +export async function getSelectedKeyboardMenuText(page: Page): Promise<string | undefined> { |
| 12 | + const watchDog = page.waitForFunction(() => !!document.getElementById('KeymanWeb_KbdList')); |
| 13 | + await page.getByRole('img', { name: 'Use Web Keyboard' }).click(); |
| 14 | + await watchDog; |
| 15 | + return page.evaluate(() => { |
| 16 | + const selectedKbd = document.querySelector('#kmwico .selected'); |
| 17 | + return selectedKbd?.textContent; |
| 18 | + }); |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Expands the keyboard selection menu and returns the menu items as an array |
| 23 | + */ |
| 24 | +export async function getAllKeyboardMenuText(page: Page): Promise<(string|undefined)[]> { |
| 25 | + const watchDog = page.waitForFunction(() => !!document.getElementById('KeymanWeb_KbdList')); |
| 26 | + await page.getByRole('img', { name: 'Use Web Keyboard' }).hover(); |
| 27 | + await watchDog; |
| 28 | + return page.evaluate(() => { |
| 29 | + const menuItems = []; |
| 30 | + const menuDiv = document.querySelector('#kmwico'); |
| 31 | + const kbdList = menuDiv?.lastElementChild; |
| 32 | + for (let i = 0; i < (kbdList ? kbdList.children.length : 0); i++) { |
| 33 | + const item = kbdList?.children[i]; |
| 34 | + menuItems.push(item?.textContent); |
| 35 | + } |
| 36 | + return menuItems; |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Loads the specified URL and waits for the page load event. |
| 42 | + */ |
| 43 | +export async function loadPage(page: Page, url: string): Promise<Page> { |
| 44 | + const loadPromise = page.waitForEvent('load'); |
| 45 | + await page.goto(url); |
| 46 | + return loadPromise; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Clicks the specified field and waits for the OSK to be shown, returning a |
| 51 | + * locator for the OSK title bar. |
| 52 | + */ |
| 53 | +export async function clickFieldAndWaitForOSK(page: Page, fieldLocator: Locator): Promise<Locator> { |
| 54 | + const keyboardchangePromise = page.evaluate(async () => { |
| 55 | + return new Promise((resolve) => { |
| 56 | + keyman.addEventListener('keyboardchange', function (kbd) { |
| 57 | + resolve(kbd); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + await fieldLocator.click(); |
| 62 | + await keyboardchangePromise; |
| 63 | + return page.locator('#keymanweb_title_bar'); |
| 64 | +} |
0 commit comments