UNPKG

playwright-fluent

Version:
52 lines (51 loc) 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectOptionsByValueInHandle = void 0; const hover_on_handle_1 = require("../hover-on-handle"); const utils_1 = require("../../../utils"); const is_handle_enabled_1 = require("../is-handle-enabled"); const get_all_options_of_handle_1 = require("../get-all-options-of-handle"); const are_options_by_value_already_selected_in_handle_1 = require("../are-options-by-value-already-selected-in-handle"); const are_options_by_value_available_in_handle_1 = require("../are-options-by-value-available-in-handle"); async function selectOptionsByValueInHandle(selector, name, values, page, options) { if (!page) { throw new Error(`Cannot select options '${values}' because no browser has been launched`); } if (!selector) { throw new Error(`Cannot select options '${values}' in '${name}' because selector was not found in DOM`); } const hoverOptions = { ...hover_on_handle_1.defaultHoverOptions, verbose: options.verbose, stabilityInMilliseconds: options.stabilityInMilliseconds, timeoutInMilliseconds: options.timeoutInMilliseconds, }; await (0, hover_on_handle_1.hoverOnHandle)(selector, name, page, hoverOptions); const areOptionsAlreadySelected = await (0, are_options_by_value_already_selected_in_handle_1.areOptionsByValueAlreadySelectedInHandle)(selector, name, values); if (areOptionsAlreadySelected) { return; } (0, utils_1.report)('waiting for the selector to be enabled ...', options.verbose); await (0, utils_1.waitUntil)(() => (0, is_handle_enabled_1.isHandleEnabled)(selector, { verbose: false }), `Cannot select options '${values}' in '${name}' because this selector is disabled`, { ...options, throwOnTimeout: true, wrapPredicateExecutionInsideTryCatch: true, }); (0, utils_1.report)('waiting for the options to be available ...', options.verbose); await (0, utils_1.waitUntil)(() => (0, are_options_by_value_available_in_handle_1.areOptionsByValueAvailableInHandle)(selector, name, values), async () => { const existingOptions = await (0, get_all_options_of_handle_1.getAllOptionsOfHandle)(selector, name); const existingValues = existingOptions.map((o) => o.value); return `Cannot select options '${values}' in '${name}' because this selector has only options '${existingValues}'`; }, { ...options, throwOnTimeout: true, wrapPredicateExecutionInsideTryCatch: true, }); const byValues = values.map((value) => { return { value, }; }); await selector.selectOption(byValues); } exports.selectOptionsByValueInHandle = selectOptionsByValueInHandle;