@lariat/playwright
Version:
Page object framework for end-to-end testing in Playwright.
63 lines (59 loc) • 1.88 kB
JavaScript
// src/enhance.ts
function enhance(collection, root, instance) {
const inst = instance;
inst.nth = (index) => new collection(root.nth(index));
inst.first = () => inst.nth(0);
inst.last = () => inst.nth(-1);
return inst;
}
// src/utils.ts
var isLocator = (handle) => "_frame" in handle;
// src/Collection.ts
var Collection = class {
constructor(root) {
this.root = root;
this.getByAltText = this.enhanceMethod("getByAltText");
this.getByLabel = this.enhanceMethod("getByLabel");
this.getByPlaceholder = this.enhanceMethod("getByPlaceholder");
this.getByRole = this.enhanceMethod("getByRole");
this.getByTestId = this.enhanceMethod("getByTestId");
this.getByText = this.enhanceMethod("getByText");
this.getByTitle = this.enhanceMethod("getByTitle");
}
getByAltText;
getByLabel;
getByPlaceholder;
getByRole;
getByTestId;
getByText;
getByTitle;
el(selector, { frame, portal, ...options } = {}) {
return this.getParent(frame, portal).locator(selector, options);
}
nest(collection, root) {
const rootElement = typeof root === "string" ? this.el(root) : root;
const instance = new collection(rootElement);
return isLocator(rootElement) ? enhance(collection, rootElement, instance) : instance;
}
get frame() {
return isLocator(this.root) ? this.root._frame : "mainFrame" in this.root ? this.root.mainFrame() : this.root;
}
get page() {
return this.frame.page();
}
getParent(frame, portal = false) {
const root = portal ? this.frame : this.root;
return frame ? root.frameLocator(frame) : root;
}
enhanceMethod(method) {
const enhanced = (arg, { frame, portal, ...options } = {}) => {
return this.getParent(frame, portal)[method](arg, options);
};
return enhanced;
}
};
// src/index.ts
var src_default = Collection;
export {
src_default as default
};