UNPKG

playwright-pom-materials

Version:

Playwright POM materials

89 lines (88 loc) 3.83 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SelectTag = void 0; const Clickable_1 = require("./Clickable"); const playwright_1 = require("../helpers/playwright"); class SelectTag extends Clickable_1.Clickable { select(label, options) { return __awaiter(this, void 0, void 0, function* () { if (typeof label === 'string') { yield this.locator.selectOption({ label }, options); } else { if (label instanceof RegExp) { const regExpOpt = [ label.ignoreCase ? 'i' : null, label.multiline ? 'm' : null, label.global ? 'g' : null, ] .filter(i => !!i) .join(''); const optHandle = yield this.locator .locator(`option:text-matches("${label.source}", "${regExpOpt}")`) .elementHandle(); yield this.locator.selectOption(optHandle, options); } else { if (label.contains) { const optHandle = yield this.locator .locator(`option:has-text("${label.contains}")`) .elementHandle(); yield this.locator.selectOption(optHandle, options); } else { yield this.locator.selectOption(label, options); } } } }); } label() { return __awaiter(this, void 0, void 0, function* () { const value = yield this.locator.inputValue(); const l = this.locator.locator(`option[value="${value}"]`); return (yield l.textContent()) || undefined; }); } set(label, options) { const fn = typeof label === 'string' ? (s) => s === label : label instanceof RegExp ? (s) => (s ? !!s.match(label) : false) : (s) => (s ? s.includes(label.contains) : false); return (0, playwright_1.pwRetry)(this.locator, () => __awaiter(this, void 0, void 0, function* () { yield this.select(label, options); const value = yield this.label(); return fn(value); })); } locatorWithOption(label) { return this.locator.filter({ has: this.locator.page().locator(`option:text("${label}")`), }); } hasOption(label) { return this.locatorWithOption(label).isVisible(); } shouldHaveOption(label, options) { options = options || {}; options.timeout = options.timeout || 120000; return this.locatorWithOption(label).waitFor(Object.assign({ state: 'visible' }, options)); } selectOption(label, options) { return __awaiter(this, void 0, void 0, function* () { yield this.shouldHaveOption(label, options); yield this.select(label); }); } } exports.SelectTag = SelectTag;