@vibe/testkit
Version:
Vibe e2e testing toolkit
105 lines • 4.78 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 { BaseElement } from "./BaseElement";
import { Button } from "./Button";
import { TextField } from "./TextField";
/**
* Class representing a Toggle element.
* Extends the BaseElement class.
*/
export class Toggle extends BaseElement {
/**
* Create a Toggle element. Can handle both wrapper elements that contain input and button elements,
* or direct input elements.
* @param {Page} page - The Playwright page object.
* @param {Locator} locator - The locator for either the wrapper element or the input element directly.
* @param {string} elementReportName - The name for reporting purposes.
*/
constructor(page, locator, elementReportName) {
super(page, locator, elementReportName);
// Store locators for dynamic resolution
this.inputLocator = locator;
this.buttonLocator = locator;
}
/**
* Get the input element, dynamically determining the correct locator.
* @private
*/
getInput() {
return __awaiter(this, void 0, void 0, function* () {
// Try to determine if the main locator is an input or wrapper
try {
// First, try if the locator itself is an input
const tagName = yield this.inputLocator.evaluate(el => el.tagName.toLowerCase());
if (tagName === "input") {
return new TextField(this.getPage(), this.inputLocator, `${this.getElementReportName()} - Input`);
}
}
catch (_a) {
// If evaluation fails, continue to try child input
}
// If not an input, try to find input as child
const childInput = this.inputLocator.locator("input");
return new TextField(this.getPage(), childInput, `${this.getElementReportName()} - Input`);
});
}
/**
* Get the button element, dynamically determining the correct locator.
* @private
*/
getButton() {
return __awaiter(this, void 0, void 0, function* () {
// Try to determine if the main locator is an input or wrapper
try {
const tagName = yield this.buttonLocator.evaluate(el => el.tagName.toLowerCase());
if (tagName === "input") {
// If main locator is input, look for sibling div
const siblingDiv = this.buttonLocator.locator("+ div");
return new Button(this.getPage(), siblingDiv, `${this.getElementReportName()} - Button`);
}
}
catch (_a) {
// If evaluation fails, continue to try child div
}
// If not an input, try to find div as child (wrapper scenario)
const childDiv = this.buttonLocator.locator("div");
return new Button(this.getPage(), childDiv, `${this.getElementReportName()} - Button`);
});
}
/**
* Set the toggle state.
* @param {boolean} isToCheck - True to turn on the toggle, false to turn off.
* @returns {Promise<void>}
*/
set(isToCheck) {
return __awaiter(this, void 0, void 0, function* () {
yield test.step(`Set toggle to ${isToCheck} for ${this.getElementReportName()}`, () => __awaiter(this, void 0, void 0, function* () {
if ((yield this.isOn()) !== isToCheck) {
const button = yield this.getButton();
yield button.click();
}
}));
});
}
/**
* Check if the toggle is on.
* @returns {Promise<boolean>} True if the toggle is on, false otherwise.
*/
isOn() {
return __awaiter(this, void 0, void 0, function* () {
return yield test.step(`Check if toggle is on for ${this.getElementReportName()}`, () => __awaiter(this, void 0, void 0, function* () {
const input = yield this.getInput();
return yield input.isChecked();
}));
});
}
}
//# sourceMappingURL=Toggle.js.map