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.
56 lines (55 loc) • 2.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.toHaveText = void 0;
const utils_1 = require("../utils/utils");
/**
* Use `toHaveText` function when you want to check that an element's text is equal to the expected text
*
* @example
* ```typescript
* // could be used with Promise<ElementHandle>
* await expect(page.$('.alert')).toHaveText('Success message');
*
* // or with ElementHandle
* const toastElement = await page.$('.alert);
* await expect(toastElement).toHaveText('Success message');
*
* // or using an array of page and selector
* await expect([page, '.alert']).toHaveText('Success message');
*
* // also you can check text ignoring case sensitive
* await expect(page.$('.alert')).toHaveText('success message', {ignoreCase: true})
* ```
*
* @param this
* @param element
* @param expectedText
* @param options
* @returns
*/
async function toHaveText(element, expectedText, options) {
try {
const elementHandle = await utils_1.getElementHandle(element, options);
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 });
}
else if (options === null || options === void 0 ? void 0 : options.trim) {
actualText = utils_1.formatText(actualText, { trim: true });
}
return {
pass: actualText === expectedText,
message: () => utils_1.getErrorMessage(this, 'toHaveText', expectedText, actualText),
};
}
catch (error) {
return {
pass: false,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
message: () => error.toString(),
};
}
}
exports.toHaveText = toHaveText;
;