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.
46 lines (45 loc) • 1.51 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.toMatchText = void 0;
const utils_1 = require("../utils/utils");
/**
* Use `toMatchText` function when you want to check that an element's text matches the expected text with regular expression
*
* @example
* ```typescript
* // could be used with Promise<ElementHandle>
* await expect(page.$('.alert')).toMatchText(/[S|s]uccess/);
*
* // or with ElementHandle
* const toastElement = await page.$('.alert);
* await expect(toastElement).toMatchText(/[S|s]uccess/);
*
* // or using an array of page and selector
* await expect([page, '.alert']).toMatchText(/[S|s]uccess/);
* ```
*
* @param this
* @param element
* @param expectedPattern
* @param options
* @returns
*/
async function toMatchText(element, expectedPattern, options) {
try {
const elementHandle = await utils_1.getElementHandle(element, options);
const actualText = await utils_1.getText(elementHandle, options === null || options === void 0 ? void 0 : options.textMethod);
return {
pass: !!actualText.match(expectedPattern),
message: () => utils_1.getErrorMessage(this, 'toMatchText', expectedPattern, actualText),
};
}
catch (error) {
return {
pass: false,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
message: () => error.toString(),
};
}
}
exports.toMatchText = toMatchText;
;