playwright-fluent
Version:
Fluent API around playwright
55 lines (54 loc) • 2.65 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.hoverOnHandle = exports.defaultHoverOptions = void 0;
const utils_1 = require("../../../utils");
const scroll_to_handle_1 = require("../scroll-to-handle");
const is_handle_visible_1 = require("../is-handle-visible");
const is_handle_moving_1 = require("../is-handle-moving");
const get_client_rectangle_of_handle_1 = require("../get-client-rectangle-of-handle");
exports.defaultHoverOptions = {
timeoutInMilliseconds: 30000,
stabilityInMilliseconds: 300,
steps: 10,
verbose: false,
};
async function hoverOnHandle(selector, name, pageOrFrame, options) {
if (!pageOrFrame) {
throw new Error(`Cannot hover on '${name}' because no browser has been launched`);
}
if (!selector) {
throw new Error(`Cannot hover on '${name}' because selector was not found in DOM`);
}
(0, utils_1.report)('scrolling to selector ...', options.verbose);
await (0, scroll_to_handle_1.scrollToHandle)(selector);
(0, utils_1.report)('waiting for the selector to be visible ...', options.verbose);
await (0, utils_1.waitUntil)(() => (0, is_handle_visible_1.isHandleVisible)(selector, { verbose: options.verbose }), `Cannot hover on '${name}' because this selector is not visible`, {
...options,
throwOnTimeout: true,
wrapPredicateExecutionInsideTryCatch: true,
});
(0, utils_1.report)('waiting for the selector to stop moving ...', options.verbose);
await (0, utils_1.waitUntil)(async () => (await (0, is_handle_moving_1.isHandleMoving)(selector)) === false, `Cannot hover on '${name}' because this selector is always moving`, {
...options,
throwOnTimeout: true,
wrapPredicateExecutionInsideTryCatch: true,
});
const page = pageOrFrame;
for (let index = 0; index < 3; index++) {
await (0, utils_1.sleep)(options.stabilityInMilliseconds / 10);
const clientRectangle = await (0, get_client_rectangle_of_handle_1.getClientRectangleOfHandle)(selector);
if (clientRectangle === null) {
(0, utils_1.report)(`Cannot get ClientRectangle of selector '${name}'`, options.verbose);
continue;
}
const x = clientRectangle.left + clientRectangle.width / 2;
const y = clientRectangle.top + clientRectangle.height / 2;
(0, utils_1.report)(`moving the mouse to x=${x} y=${y}`, options.verbose);
if (page.mouse) {
await page.mouse.move(x, y, { steps: options.steps });
continue;
}
await selector.hover();
}
}
exports.hoverOnHandle = hoverOnHandle;
;