import { LogicError } from '@serenity-js/core'; import type { SwitchableOrigin } from '@serenity-js/web'; import { PageElement, SelectOption } from '@serenity-js/web'; import * as scripts from '@serenity-js/web/scripts'; import type * as playwright from 'playwright-core'; import { ensure, isDefined } from 'tiny-types'; import type { PlaywrightLocator } from './locators/index.js'; /** * Playwright-specific implementation of [`PageElement`](https://serenity-js.org/api/web/class/PageElement/). * * @group Models */ export class PlaywrightPageElement extends PageElement { of(parent: PageElement): PageElement { return new PlaywrightPageElement(this.locator.of(parent.locator)); } closestTo(child: PageElement): PageElement { return new PlaywrightPageElement(this.locator.closestTo(child.locator)); } async enterValue(value: string | number | Array): Promise { const text = [].concat(value).join(''); const element = await this.nativeElement(); return element.fill(text); } async clearValue(): Promise { try { const element = await this.nativeElement(); await element.fill(''); } catch(error) { throw new LogicError(`The input field doesn't seem to have a 'value' attribute that could be cleared`, error); } } async click(): Promise { const element = await this.nativeElement(); return element.click(); } async doubleClick(): Promise { const element = await this.nativeElement(); return element.dblclick(); } async scrollIntoView(): Promise { const element = await this.nativeElement(); return element.scrollIntoViewIfNeeded(); } async hoverOver(): Promise { const element = await this.nativeElement(); return element.hover(); } async rightClick(): Promise { const element = await this.nativeElement(); return element.click({ button: 'right' }); } async selectOptions(...options: Array): Promise { const element = await this.nativeElement(); const optionsToSelect = options.map(option => ({ value: option.value, label: option.label, }) ); await element.selectOption(optionsToSelect); } async selectedOptions(): Promise> { const element = await this.nativeElement(); /* c8 ignore start */ const options = await element.locator('option').evaluateAll( (optionNodes: Array) => optionNodes.map((optionNode: HTMLOptionElement) => { return { selected: optionNode.selected, disabled: optionNode.disabled, label: optionNode.label, value: optionNode.value, } }) ); /* c8 ignore stop */ return options.map(option => new SelectOption(option.label, option.value, option.selected, option.disabled) ); } async dragTo(destination: PageElement): Promise { const elementBeingDragged = await this.nativeElement(); const destinationElement = await destination.nativeElement(); // Use force: true to bypass actionability checks on the target element. // This is necessary for drop zones that dynamically enable pointer-events // only when a drag operation is in progress. await elementBeingDragged.dragTo(destinationElement, { force: true }); } async attribute(name: string): Promise { const element = await this.nativeElement(); return element.getAttribute(name); } async text(): Promise { const element = await this.nativeElement(); return element.innerText(); } async value(): Promise { const element = await this.nativeElement(); return element.inputValue(); } async html(): Promise { const element = await this.nativeElement(); return element.evaluate(nativeElement => nativeElement.outerHTML); } async switchTo(): Promise { try { const nativeLocator = await this.nativeElement(); const element = await nativeLocator.elementHandle(); const frame = await element.contentFrame(); if (frame) { const locator = (this.locator as PlaywrightLocator); await locator.switchToFrame(nativeLocator); return { switchBack: async (): Promise => { await locator.switchToParentFrame(); } } } /* c8 ignore start */ const previouslyFocusedElement = await nativeLocator.evaluateHandle( (domNode: HTMLElement) => { const currentlyFocusedElement = document.activeElement; domNode.focus(); return currentlyFocusedElement; } ); /* c8 ignore stop */ return new PreviouslyFocusedElementSwitcher(previouslyFocusedElement); } catch(error) { throw new LogicError(`Couldn't switch to page element located ${ this.locator }`, error); } } async isActive(): Promise { try { const element = await this.nativeElement(); return element.evaluate( domNode => domNode === document.activeElement ); } catch { return false; } } async isClickable(): Promise { try { const element = await this.nativeElement(); await element.click({ trial: true }); return true; } catch { return false; } } async isEnabled(): Promise { try { const element = await this.nativeElement(); return element.isEnabled(); } catch { return false; } } async isSelected(): Promise { try { const element: playwright.Locator = await this.nativeElement(); // works for