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.

55 lines (54 loc) 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toContainText = void 0; const utils_1 = require("../utils/utils"); /** * Use `toContainText` function when you want to check that an element's text contains the expected string or substring * * @example * ```typescript * // could be used with Promise<ElementHandle> * await expect(page.$('.alert')).toContainText('Success'); * * // or with ElementHandle * const toastElement = await page.$('.alert); * await expect(toastElement).toContainText('Success'); * * // or using an array of page and selector * await expect([page, '.alert']).toContain('Success'); * * // also you can check text ignoring case sensitive * await expect(page.$('.alert')).toContain('success', {ignoreCase: true}) * ``` * * @param this * @param element * @param expectedText * @param options * @returns */ async function toContainText(element, expectedText, options) { try { const elementHandle = await utils_1.getElementHandle(element, options); if (!elementHandle) throw new Error(`Element ${elementHandle} wasn't found`); let actualText = await utils_1.getText(elementHandle, options === null || options === void 0 ? void 0 : options.textMethod); if (options === null || options === void 0 ? void 0 : options.ignoreCase) { actualText = utils_1.formatText(actualText, { ignoreCase: true }); expectedText = utils_1.formatText(expectedText, { ignoreCase: true }); } return { pass: actualText.includes(expectedText), message: () => utils_1.getErrorMessage(this, 'toContainText', expectedText, actualText), }; } catch (error) { return { pass: false, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore message: () => error.message, }; } } exports.toContainText = toContainText;