playwright-fluent
Version:
Fluent API around playwright
58 lines (57 loc) • 2.4 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isHandleVisible = exports.defaultVerboseOptions = void 0;
const utils_1 = require("../../../utils");
exports.defaultVerboseOptions = {
verbose: false,
};
async function isHandleVisible(selector, options) {
if (selector === undefined || selector === null) {
return false;
}
try {
const isOutOfScreenOrTransparent = await selector.evaluate((el) => {
try {
const style = window.getComputedStyle(el);
if (style && style.opacity && style.opacity === '0') {
return true;
}
const rect = el.getBoundingClientRect();
if (rect.top + rect.height < 0 && (style === null || style === void 0 ? void 0 : style.position) === 'absolute') {
return true;
}
if (rect.left + rect.width < 0 && (style === null || style === void 0 ? void 0 : style.position) === 'absolute') {
return true;
}
return false;
}
catch (error) {
return false;
}
});
if (isOutOfScreenOrTransparent) {
(0, utils_1.report)(`Selector is not visible because it is either transparent or out of screen`, options.verbose);
return false;
}
}
catch (error) {
// Element has been removed from DOM while or just before selector.evaluate execution
return false;
}
try {
const result = await selector.isVisible();
return result;
}
catch (error) {
const errorAsError = error;
const isError = typeof (errorAsError === null || errorAsError === void 0 ? void 0 : errorAsError.message) === 'string';
const errorMessage = isError
? `Playwright execution of 'ElementHandle.isVisible()' failed with error: ${errorAsError.message}`
: `Playwright execution of 'ElementHandle.isVisible()' failed: maybe the element was detached from the DOM during execution.`;
// eslint-disable-next-line no-console
console.warn(errorMessage);
(0, utils_1.report)(`Selector is not visible because it has been detached from DOM.`, options.verbose);
return false;
}
}
exports.isHandleVisible = isHandleVisible;
;