UNPKG

playwright-expect

Version:

The playwright-expect is an assertion library for TypeScript and JavaScript intended for use with a test runner such as Jest or Playwright Test.

47 lines (46 loc) 1.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toBeFocused = void 0; const utils_1 = require("../utils/utils"); /** * Use `toBeFocused` function when you want to check that an element is focused * * @example * ```typescript * // could be used with Promise<ElementHandle> * await expect(page.$('.btn')).toBeFocused(true); * * // or with ElementHandle * const toastElement = await page.$('.btn); * await expect(toastElement).toBeFocused(true); * * // or using an array of page and selector * await expect([page, '.btn']).toBeFocused(true); * * ``` * * @param this * @param element * @param expectedState * @param options * @returns */ async function toBeFocused(element, expectedState = true, options) { try { const elementHandle = await utils_1.getElementHandle(element, options); const actualState = await elementHandle.evaluate((el) => el === document.activeElement); return { pass: actualState === expectedState, message: () => utils_1.getErrorMessage(this, 'toBeFocused', expectedState, actualState), }; } catch (error) { return { pass: false, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore message: () => error.toString(), }; } } exports.toBeFocused = toBeFocused;