@serenity-js/web
Version:
Serenity/JS Screenplay Pattern library offering a flexible, web driver-agnostic approach for interacting with web-based user interfaces and components, suitable for various testing contexts
93 lines • 3.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.PageElement = void 0;
const core_1 = require("@serenity-js/core");
const tiny_types_1 = require("tiny-types");
const abilities_1 = require("../abilities");
/**
* Uses the [actor's](https://serenity-js.org/api/core/class/Actor/) [ability](https://serenity-js.org/api/core/class/Ability/) to [`BrowseTheWeb`](https://serenity-js.org/api/web/class/BrowseTheWeb/) to identify
* a single Web element located by [`Selector`](https://serenity-js.org/api/web/class/Selector/).
*
* ## Learn more
* - [Page Element Query Language](https://serenity-js.org/handbook/web-testing/page-element-query-language)
* - [`Optional`](https://serenity-js.org/api/core/interface/Optional/)
* - [`Switchable`](https://serenity-js.org/api/web/interface/Switchable/)
*
* @group Models
*/
class PageElement {
locator;
static from(nativeElement) {
return core_1.Question.about(`native page element`, async (actor) => {
const currentPage = await abilities_1.BrowseTheWeb.as(actor).currentPage();
return currentPage.createPageElement(nativeElement);
});
}
static located(selector) {
return core_1.Question.about((0, core_1.the) `page element located ${selector}`, async (actor) => {
const bySelector = await actor.answer(selector);
const currentPage = await abilities_1.BrowseTheWeb.as(actor).currentPage();
return currentPage.locate(bySelector);
});
}
static of(childElement, parentElement) {
return core_1.Question.about((0, core_1.the) `${childElement} of ${parentElement}`, async (actor) => {
const parent = await actor.answer(parentElement);
const child = childElement.of(parent);
return actor.answer(child);
});
}
/**
* A static method producing a [`MetaQuestion`](https://serenity-js.org/api/core/interface/MetaQuestion/) that can be used with [`PageElements.eachMappedTo`](https://serenity-js.org/api/web/class/PageElements/#eachMappedTo) method
* to extract the HTML of each element in a collection.
*
* #### Example
*
* ```typescript
* import { actorCalled, Log } from '@serenity-js/core'
* import { Navigate, PageElement, By, Text } from '@serenity-js/web'
* import { includes } from '@serenity-js/assertions'
*
* await actorCalled('Debbie').attemptsTo(
* Navigate.to('https://serenity-js.org'),
*
* Log.the(
* PageElements.located(By.css('a'))
* .where(Text, includes('modular'))
* .eachMappedTo(PageElement.html())
* ),
* )
* ```
*/
static html() {
return {
of: (pageElement) => core_1.Question.about(`outer HTML of ${pageElement}`, async (actor) => {
const element = await actor.answer(pageElement);
return element.html();
})
};
}
constructor(locator) {
this.locator = locator;
(0, tiny_types_1.ensure)('native element locator', locator, (0, tiny_types_1.isDefined)());
}
/**
* An "escape hatch" providing access to the integration tool-specific implementation of a Web element.
*/
async nativeElement() {
return this.locator.nativeElement();
}
toString() {
return `PageElement located ${this.locator.toString()}`;
}
/**
* Returns a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves to `true` when the element
* is present in the [Document Object Model (DOM)](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model),
* `false` otherwise.
*/
async isPresent() {
return this.locator.isPresent();
}
}
exports.PageElement = PageElement;
//# sourceMappingURL=PageElement.js.map
;