tm-playwright-framework
Version:
Playwright Cucumber TS framework - The easiest way to learn
136 lines (135 loc) • 5.71 kB
JavaScript
/**
* CHECKBOX.TS
*
* This TypeScript file contains methods to perform actions related to the Check Box element.
*
* @author Govindharam, Sasitharan
* @reviewer Sahoo, AshokKumar
* @version 1.0 - 1st-JUNE-2025
*
* @methods
* - `check`: Accepts an element locator as a `string` or Element and selects the checkbox if it is not already selected. Ignores if already checked.
* - `uncheck`: Accepts an element locator as a `string` or Element and unselects the checkbox if it is selected. Ignores if already unchecked.
*/
import { fixture } from 'tm-playwright-framework/dist/hooks/pageFixture.js';
import Logger from 'tm-playwright-framework/dist/report/logger.js';
import Assertion from 'tm-playwright-framework/dist/assertion/assertion.js';
export default class CheckboxActions {
/* Method to be executed when an Object instantiated for this class */
constructor() { }
/**
* Selects the checkbox if it is not already selected. Ignores if already checked.
* @param locator - The element locator as a string or Element.
* @param timeOut - The optional timeout in milliseconds for the action.
*/
async check(locator, timeOut = Number(process.env.DEFAULT_TIMEOUT) ?? 30000) {
const timestamp = new Date().toISOString();
let status = 'Success';
let message = '';
let obj;
try {
obj = typeof locator === 'object' ? locator : await fixture.page.locator(locator);
await obj.waitFor({ state: 'visible', timeout: timeOut });
let tagName = await obj.evaluate((el) => el.tagName);
let asrt = new Assertion(fixture.page);
let object = (await asrt.getText(obj)) || 'Unknown Checkbox';
object = object.trim();
if (tagName.toLowerCase() === "input") {
if (!obj.isChecked()) {
await obj.check({ timeout: timeOut });
message = `Checkbox "${object}" has been "Selected" successfully`;
}
else
message = `Checkbox "${object}" has already been "Selected"`;
}
else {
let result = false;
try {
let chkBox = await obj.locator("//input[@type='checkbox']");
result = await chkBox.isChecked();
}
catch (error) {
Logger.logSoftAssertFailure(error);
}
if (!result) {
await obj.click({ timeout: timeOut });
message = `Click action has been performed successfully on "${object}"`;
}
else
message = `Click action ignored. Checkbox "${object}" has already been "Selected"`;
}
}
catch (error) {
status = 'Failed';
if (error instanceof Error) {
message = error.message;
Logger.logSoftAssertFailure(error);
}
else {
message = "An unknown error occurred while checking the checkbox";
Logger.logSoftAssertFailure(error);
}
}
finally {
Logger.logStatus(status, message);
}
}
/**
* Unselects the checkbox if it is selected. Ignores if already unchecked.
* @param locator - The element locator as a string or Element.
* @param timeOut - The optional timeout in milliseconds for the action.
*/
async uncheck(locator, timeOut = Number(process.env.DEFAULT_TIMEOUT) ?? 30000) {
const timestamp = new Date().toISOString();
let status = 'Success';
let message = '';
let obj;
try {
obj = typeof locator === 'object' ? locator : await fixture.page.locator(locator);
await obj.waitFor({ state: 'visible', timeout: timeOut });
let tagName = await obj.evaluate((el) => el.tagName);
let asrt = new Assertion(fixture.page);
let object = (await asrt.getText(obj)) || 'Unknown Checkbox';
object = object.trim();
if (tagName.toLowerCase() === "input") {
if (!obj.isChecked()) {
await obj.setChecked(false, { timeout: timeOut });
message = `Checkbox "${object}" has been "Un Selected" successfully`;
}
else {
message = `Checkbox "${object}" has already been "Un selected"`;
}
}
else {
let result = false;
try {
let chkBox = await obj.locator("//input[@type='checkbox']");
result = await chkBox.isChecked();
}
catch (error) {
Logger.logSoftAssertFailure(error);
}
if (result) {
await obj.click({ timeout: timeOut });
message = `Click action has been performed successfully on "${object}"`;
}
else
message = `Click action ignored. Checkbox "${object}" has already been "Unselected"`;
}
}
catch (error) {
status = 'Failed';
if (error instanceof Error) {
message = error.message;
Logger.logSoftAssertFailure(error);
}
else {
message = "An unknown error occurred while unchecking the checkbox";
Logger.logSoftAssertFailure(error);
}
}
finally {
Logger.logStatus(status, message);
}
}
}