UNPKG

@vibe/testkit

Version:
198 lines 8.38 kB
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"; /** * Class representing a base element for Playwright tests. */ export class BaseElement { /** * Create a BaseElement. * @param {Object} page - The Playwright page object. * @param {Object} locator - The locator for the element. * @param {string} elementReportName - The name for reporting purposes. */ constructor(page, locator, elementReportName) { this.page = page; this.locator = locator; this.elementReportName = elementReportName; } /** * Get the locator of the element. * @returns {Object} - The locator of the element. */ getLocator() { return this.locator; } /** * Check if the element is enabled. * @returns {Promise<boolean>} - Returns true if the element is enabled, otherwise false. */ isEnabled() { return __awaiter(this, void 0, void 0, function* () { let isEnabled = false; yield test.step(`Return if ${this.elementReportName} is enabled`, () => __awaiter(this, void 0, void 0, function* () { isEnabled = yield this.locator.isEnabled(); return isEnabled; })); return isEnabled; }); } /** * Hover the element. * @returns {Promise<void>} */ hover() { return __awaiter(this, void 0, void 0, function* () { yield test.step(`Hover ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () { yield this.locator.hover(); })); }); } /** * Click the element. * @returns {Promise<void>} */ click() { return __awaiter(this, void 0, void 0, function* () { yield test.step(`Click ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () { yield this.locator.click(); })); }); } /** * Scroll the element into view if needed. * @returns {Promise<void>} */ scrollIntoView() { return __awaiter(this, void 0, void 0, function* () { yield test.step(`Scroll ${this.elementReportName} into view`, () => __awaiter(this, void 0, void 0, function* () { yield this.locator.scrollIntoViewIfNeeded(); })); }); } getAttributeValue(attributeName, // eslint-disable-next-line @typescript-eslint/no-explicit-any options = { timeout: 10000, pollInterval: 500 }) { return __awaiter(this, void 0, void 0, function* () { let attributeValue = null; yield test.step(`Get attribute ${attributeName} of ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () { const startTime = Date.now(); while (Date.now() - startTime < options.timeout) { attributeValue = yield this.locator.getAttribute(attributeName); if (attributeValue !== null) { break; } while (Date.now() - startTime < options.timeout) { yield new Promise(resolve => setTimeout(resolve, options.pollInterval)); } if (attributeValue === null) { throw new Error(`Attribute ${attributeName} did not exist after ${options.timeout} ms`); } } })); return attributeValue; }); } getText() { return __awaiter(this, void 0, void 0, function* () { let text; yield test.step(`Get text of ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () { text = yield this.locator.innerText(); return text; })); return text; }); } waitFor(options = {}) { return __awaiter(this, void 0, void 0, function* () { yield test.step(`Wait for ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () { yield this.locator.waitFor(options); })); }); } waitForVisible() { return __awaiter(this, void 0, void 0, function* () { yield test.step(`Wait for ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () { yield this.locator.waitFor({ state: "visible" }); })); }); } waitForAbsence() { return __awaiter(this, void 0, void 0, function* () { yield test.step(`Wait for ${this.elementReportName} to be absent`, () => __awaiter(this, void 0, void 0, function* () { yield this.waitFor({ state: "detached" }); })); }); } count() { return __awaiter(this, void 0, void 0, function* () { let count = 0; yield test.step(`Count elements matching ${this.elementReportName}`, () => __awaiter(this, void 0, void 0, function* () { count = yield this.locator.count(); })); return count; }); } isVisible() { return __awaiter(this, void 0, void 0, function* () { let isVisible = false; yield test.step(`Check if ${this.elementReportName} is visible`, () => __awaiter(this, void 0, void 0, function* () { isVisible = yield this.locator.isVisible(); })); return isVisible; }); } /** * Wait for the list elements to stabilize (i.e., the count of items remains constant for a specified duration). * @param {Locator} locator - The locator for the elements. * @returns {Promise<void>} */ waitForAndVerifyElements(locator) { return __awaiter(this, void 0, void 0, function* () { yield test.step(`Wait for ${this.elementReportName} items to stabilize and verify existence`, () => __awaiter(this, void 0, void 0, function* () { let previousCount = 0; let stableCountTime = 0; const stabilizationTimeMs = 500; // eslint-disable-next-line no-constant-condition while (true) { const currentCount = yield locator.count(); // Verify we have at least one element if (currentCount === 0) { yield this.page.waitForTimeout(100); continue; } // Check if all elements are visible const elements = yield locator.all(); const visibleStates = yield Promise.all(elements.map(el => el.isVisible())); const allVisible = visibleStates.every(state => state === true); if (!allVisible) { yield this.page.waitForTimeout(100); continue; } if (currentCount === previousCount) { stableCountTime += 100; } else { stableCountTime = 0; } if (stableCountTime >= stabilizationTimeMs) { break; } previousCount = currentCount; yield this.page.waitForTimeout(100); } if ((yield locator.count()) === 0) { throw new Error(`No ${this.elementReportName} elements found after stabilization`); } })); }); } } //# sourceMappingURL=BaseElement.js.map