tm-playwright-framework
Version:
Playwright Cucumber TS framework - The easiest way to learn
149 lines (148 loc) • 7.01 kB
JavaScript
/**
* ELEMENT.TS
*
* This TypeScript file contains methods to find elements from the web page.
*
* @author Sasitharan, Govindharam
* @reviewer Sahoo, AshokKumar
* @version 1.0 - 1st-JUNE-2025
*
* @methods
* - `get`: Accepts an element locator as a `string`, finds the element based on the locator, and returns it.
* - `getByLabel`: Accepts an element locator as a `string`, finds the element based on the label, and returns it.
* - `getByRole`: Accepts an element locator as a `string`, finds the element based on the Role attribute value, and returns it.
* - `waitTillDisappear`: Accepts an element locator as a `string`, finds the element, and waits until the element disappears.
*/
import { fixture } from "tm-playwright-framework/dist/hooks/pageFixture.js";
import Logger from "tm-playwright-framework/dist/report/logger.js";
const timestamp = new Date().toISOString();
let status = 'Success';
export default class Elements {
constructor() { }
/**
* Accepts an element locator as a `string`, finds the element based on the locator in the provided context (page or frame), and returns it.
* @param {string} locator - The locator string to identify the element.
* @param {number} [timeOut=80000] - Optional timeout in milliseconds.
* @param {Page | Frame} [context=fixture.page] - Optional page or frame context.
* @returns {Promise<any>} - The located element.
*/
async get(locator, timeOut = Number(process.env.DEFAULT_TIMEOUT) * 3 || 80000, context = fixture.page) {
let object;
let message = "";
let status = 'Success';
try {
if ('waitForLoadState' in context) {
await context.waitForLoadState("domcontentloaded", { timeout: timeOut });
}
object = await context.locator(locator);
message = `Object has been identified for: "${locator}"`;
}
catch (error) {
status = 'Failed';
message = error instanceof Error
? `${error.message}. For locator: ${locator}`
: "An unknown error occurred";
}
finally {
Logger.logStatus(status, message);
}
return object;
}
/**
* Accepts an element locator as a `string`, finds the element based on the label, and returns it.
* @param {string} label - The label string to identify the element.
* @param {number} [timeOut=80000] - Optional timeout in milliseconds.
* @returns {Promise<any>} - The located element.
*/
async getByLabel(label, timeOut = Number(process.env.DEFAULT_TIMEOUT) * 3 || 80000) {
let object;
let message = "";
try {
await fixture.page.waitForLoadState("networkidle", { timeout: timeOut });
object = await fixture.page.getByLabel(label);
message = `Object has been identified for Label :- "${label}"`;
}
catch (error) {
status = 'Failed';
if (error instanceof Error) {
message = `${error.message}. For label: ${label}`;
Logger.logSoftAssertFailure(error);
}
else {
message = "An unknown error occurred";
Logger.logSoftAssertFailure(error);
}
}
finally {
Logger.logStatus(status, message);
}
return object;
}
/**
* Accepts an element locator as a `string`, finds the element based on the ARIA Role value, and returns it.
* @param {"alert" | "alertdialog" | "application" | "article" | "banner" | "blockquote" | "button" | "caption" | "cell" | "checkbox" | "code" | "columnheader" | "combobox" | "complementary" | "contentinfo" | "definition" | "dialog" | "directory" | "document" | "feed" | "figure" | "form" | "grid" | "gridcell" | "group" | "heading" | "img" | "link" | "list" | "listbox" | "listitem" | "log" | "main" | "marquee" | "math" | "menu" | "menubar" | "menuitem" | "menuitemcheckbox" | "menuitemradio" | "navigation" | "none" | "note" | "option" | "paragraph" | "presentation" | "progressbar" | "radio" | "radiogroup" | "region" | "row" | "rowgroup" | "rowheader" | "scrollbar" | "search" | "searchbox" | "separator" | "slider" | "spinbutton" | "status" | "switch" | "tab" | "table" | "tablist" | "tabpanel" | "term" | "textbox" | "timer" | "toolbar" | "tooltip" | "tree" | "treegrid" | "treeitem"} roleType - The role type to identify the element.
* @param {key: string, value: string} [filter: {ke: value}] - Optional filter.
* @param {number} [timeOut=80000] - Optional timeout in milliseconds.
* @returns {Promise<any>} - The located element.
*/
async getByRole(roleType, filter, timeOut) {
let object;
let message = "";
try {
await fixture.page.waitForLoadState("networkidle", { timeout: timeOut });
if (filter) {
object = await fixture.page.getByRole(roleType, filter);
}
else {
object = await fixture.page.getByRole(roleType);
}
//object.scrollIntoViewIfNeeded();
message = `Object has been identified for Role :- "${roleType} ${filter ? JSON.stringify(filter) : ''}"`;
}
catch (error) {
status = 'Failed';
if (error instanceof Error) {
message = `${error.message}. For role type: "${roleType} ${filter ? JSON.stringify(filter) : ''}"`;
Logger.logSoftAssertFailure(error);
}
else {
message = "An unknown error occurred";
Logger.logSoftAssertFailure(error);
}
}
finally {
Logger.logStatus(status, message);
}
return object;
}
/**
* Accepts an element locator as a `string`, finds the element, and waits until the element disappears.
* @param {string} locator - The locator string to identify the element.
* @param {number} [timeOut=80000] - Optional timeout in milliseconds.
* @returns {Promise<any>} - The element that disappeared.
*/
async waitTillDisappear(locator, timeOut = Number(process.env.DEFAULT_TIMEOUT) ?? 80000) {
let object;
let message = "";
try {
object = await fixture.page.locator(locator);
await object.waitFor({ state: "hidden", timeout: timeOut });
message = `Object disappeared. Locator: :- "${locator}"`;
}
catch (error) {
status = 'Failed';
if (error instanceof Error) {
message = `${error.message}. For locator: ${locator}`;
Logger.logSoftAssertFailure(error);
}
else {
message = "An unknown error occurred";
Logger.logSoftAssertFailure(error);
}
}
finally {
Logger.logStatus(status, message);
}
return object;
}
}