@agnos-ui/base-po
Version:
Base class to build page objects for end-to-end tests with Playwright.
89 lines (88 loc) • 2.85 kB
JavaScript
var f = Object.defineProperty;
var u = (o) => {
throw TypeError(o);
};
var m = (o, t, e) => t in o ? f(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
var g = (o, t, e) => m(o, typeof t != "symbol" ? t + "" : t, e), R = (o, t, e) => t.has(o) || u("Cannot " + e);
var l = (o, t, e) => (R(o, t, "read from private field"), e ? e.call(o) : t.get(o)), s = (o, t, e) => t.has(o) ? u("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(o) : t.set(o, e), n = (o, t, e, i) => (R(o, t, "write to private field"), i ? i.call(o, e) : t.set(o, e), e);
function h(o) {
return "waitFor" in o;
}
var r, a, c;
class w {
/**
*
* @param container - Container locator (page if there is no container) where the component is located
* @param index - If you have multiple components **inside the container**, you must provide the index to select the component to work with. You can omit this parameter if you have a single component.
*/
constructor(t, e = null) {
s(this, r);
s(this, a);
s(this, c);
/**
* Playwright page object from which the page object has been created.
* It can be convenient for situation where you can't work (internally) with the locatorRoot
*/
g(this, "_page");
n(this, r, t), n(this, a, e), this._page = h(t) ? t.page() : t;
}
/**
* The root locator of your page object.
* It can be used to be chain with internal locators.
*
* It is built with the provided `container` from the constructor, the `getComponentSelector` (one of them must be provided, at least) and the optional `index` parameter from the contructor.
*
* @example
*
* ```typescript
*
* // Button locator in this component
* get locatorButton() {
* return this.locatorRoot().locator('button').nth(0);
* }
* ```
*/
get locatorRoot() {
let t = l(this, c);
if (!t) {
const e = this.getComponentSelector();
let i = l(this, r);
if (e) {
i = i.locator(e);
const p = l(this, a);
p !== null && (i = i.nth(p));
}
t = h(i) ? i : i.locator("html"), n(this, c, t);
}
return t;
}
/**
* Returns the root selector for the component
* This method must be usually overriden to return the css selector of your component
*
* @example
*
* ```typescript
* getComponentSelector(): string {
* return 'app-component';
* }
* ```
* Will target the html component:
* ```html
* <app-component>...</app-component>
* ```
*/
getComponentSelector() {
return "";
}
/**
* Wait for the locatorRoot to be displayed in the page
*/
async waitLoaded() {
h(this.locatorRoot) && await this.locatorRoot.waitFor();
}
}
r = new WeakMap(), a = new WeakMap(), c = new WeakMap();
export {
w as BasePO
};