@vibe/testkit
Version:
Vibe e2e testing toolkit
86 lines • 3.77 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { test } from "@playwright/test";
import { ListItem } from "./ListItem";
import { BaseElement } from "./BaseElement";
/**
* Class representing a List element.
* Extends the BaseElement class.
*/
export class List extends BaseElement {
/**
* Create a List.
* @param {Page} page - The Playwright page object.
* @param {Locator} locator - The locator for the List element.
* @param {string} elementReportName - The name for reporting purposes.
*/
constructor(page, locator, elementReportName) {
super(page, locator, elementReportName);
this.items = [];
this.itemsInitialized = false;
}
/**
* Initialize list items if they are not already initialized.
* @returns {Promise<void>}
*/
initializeItemsIfNeeded() {
return __awaiter(this, void 0, void 0, function* () {
yield test.step(`Initialize ${this.elementReportName} if needed`, () => __awaiter(this, void 0, void 0, function* () {
if (!this.itemsInitialized) {
yield this.initializeItems();
this.itemsInitialized = true;
}
}));
});
}
/**
* Initialize the list items by locating all list elements.
* @returns {Promise<void>}
*/
initializeItems() {
return __awaiter(this, void 0, void 0, function* () {
yield test.step(`Initialize ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () {
const listItemLocator = this.locator.locator("li");
yield this.waitForAndVerifyElements(listItemLocator);
const listElements = yield listItemLocator.all();
this.items = yield Promise.all(listElements.map((locator) => __awaiter(this, void 0, void 0, function* () {
const itemName = yield locator.innerText();
return new ListItem(this.page, locator, `List Item: ${itemName}`);
})));
}));
});
}
/**
* Get a list item by its name.
* @param {string} itemName - The name of the item to retrieve.
* @returns {ListItem | undefined} The list item with the specified name or undefined if not found.
*/
getItemByName(itemName) {
return this.items.find(item => item.elementReportName.includes(itemName));
}
/**
* Select a list item.
* @param {string} listItem - The name of the item to select.
* @returns {Promise<void>}
*/
selectItem(listItem) {
return __awaiter(this, void 0, void 0, function* () {
yield this.initializeItemsIfNeeded();
let item = this.getItemByName(listItem);
if (!item) {
// If the item is not in the initialized list, create it dynamically
item = new ListItem(this.page, this.locator.getByText(`${listItem}`), `List Item: ${listItem}`);
}
yield item.scrollIntoView();
yield item.click();
});
}
}
//# sourceMappingURL=List.js.map