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