UNPKG

@serenity-js/playwright

Version:

Adapter that integrates @serenity-js/web with Playwright, enabling Serenity/JS reporting and using the Screenplay Pattern to write component and end-to-end test scenarios

118 lines 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlaywrightExistingElementLocator = exports.PlaywrightLocator = void 0; const core_1 = require("@serenity-js/core"); const web_1 = require("@serenity-js/web"); const selector_engines_1 = require("../../../selector-engines"); const promised_1 = require("../../promised"); const PlaywrightPageElement_1 = require("../PlaywrightPageElement"); /** * Playwright-specific implementation of [`Locator`](https://serenity-js.org/api/web/class/Locator/). * * @group Models */ class PlaywrightLocator extends web_1.Locator { constructor(parent, selector) { super(parent, selector); } // todo: refactor; replace with a map and some more generic lookup mechanism nativeSelector() { if (this.selector instanceof web_1.ByCss) { return `:light(${this.selector.value})`; } if (this.selector instanceof web_1.ByDeepCss) { return this.selector.value; } if (this.selector instanceof web_1.ByCssContainingText) { return `:light(${this.selector.value}):has-text("${this.selector.text}")`; } if (this.selector instanceof web_1.ById) { return `id=${this.selector.value}`; } if (this.selector instanceof web_1.ByTagName) { return `:light(${this.selector.value})`; } if (this.selector instanceof web_1.ByXPath) { return `xpath=${this.selector.value}`; } throw new core_1.LogicError((0, core_1.f) `${this.selector} is not supported by ${this.constructor.name}`); } async isPresent() { try { const parentPresent = await this.parent.isPresent(); if (!parentPresent) { return false; } const parent = await this.parent.nativeElement(); await parent.locator(this.nativeSelector()).first().waitFor({ state: 'attached', timeout: 250 }); return true; } catch (error) { if (error.name === 'TimeoutError') { return false; } throw error; } } async nativeElement() { const parent = await this.parent.nativeElement(); return (0, promised_1.promised)(parent.locator(this.nativeSelector())); } async allNativeElements() { const parent = await this.parent.nativeElement(); if (!parent) { return []; } return (0, promised_1.promised)(parent.locator(this.nativeSelector()).all()); } of(parent) { return new PlaywrightLocator(parent, this.selector); } closestTo(child) { return new PlaywrightParentElementLocator(this.parent, this.selector, child); } locate(child) { return new PlaywrightLocator(this, child.selector); } element() { return new PlaywrightPageElement_1.PlaywrightPageElement(this); } async allElements() { const elements = await this.allNativeElements(); return elements.map(childElement => new PlaywrightPageElement_1.PlaywrightPageElement(new PlaywrightExistingElementLocator(this.parent, this.selector, childElement))); } } exports.PlaywrightLocator = PlaywrightLocator; /** * @internal */ class PlaywrightExistingElementLocator extends PlaywrightLocator { existingNativeElement; constructor(parent, selector, existingNativeElement) { super(parent, selector); this.existingNativeElement = existingNativeElement; } async nativeElement() { return this.existingNativeElement; } async allNativeElements() { return [this.existingNativeElement]; } } exports.PlaywrightExistingElementLocator = PlaywrightExistingElementLocator; class PlaywrightParentElementLocator extends PlaywrightLocator { child; constructor(parent, selector, child) { super(parent, selector); this.child = child; } async nativeElement() { const cssSelector = this.asCssSelector(this.selector); const child = await this.child.nativeElement(); return child.locator(`${selector_engines_1.SerenitySelectorEngines.engineIdOf('closest')}=${cssSelector.value}`); } async allNativeElements() { return [await this.nativeElement()]; } } //# sourceMappingURL=PlaywrightLocator.js.map