tm-playwright-framework
Version:
Playwright Cucumber TS framework - The easiest way to learn
110 lines (109 loc) • 4.61 kB
JavaScript
/**
* RADIOBUTTON.TS
*
* This TypeScript file contains methods to perform actions on Radio Button (Options).
*
* @author Sasitharan, Govindharam
* @reviewer Sahoo, AshokKumar
* @version 1.0 - 1st-JUNE-2025
*
* @methods
* - `select`: Accepts an element locator as a `string` or Elemetn and selects the given option.
* - `selectByLabel`: Accepts an element locator as a `string` or Element and selects the option by the given label.
*/
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 RadioButtonActions {
/* Method to be executed when an Object instantiated for this class */
constructor() { }
/**
* Selects the given option for the specified radio button element.
* @param {any} locator - The element locator as a string or Element.
* @param {number} [timeOut=30000] - (Optional) The timeout in milliseconds to wait for the element to be visible.
* @returns {Promise<void>}
*/
// Accepts element locator as string or Element and selects the given option
async select(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 = obj.evaluate((el) => el.tagName);
let asrt = new Assertion(fixture.page);
let object = (await asrt.getText(obj)).trim();
if (tagName === "input") {
if (!obj.isChecked()) {
await obj.check();
message = `Option "${object}" has been "Selected" successfully`;
}
else
message = `Option "${object}" has already been "Selected"`;
}
else {
let result = false;
try {
let optn = await obj.locator("//input[@type='radio']");
result = await optn.isChecked();
}
catch (error) { }
if (!result) {
await obj.click();
message = `Click action has been performed successfully on "${object}"`;
}
else
message = `Click action ignored. Option "${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 during the selection of the option";
Logger.logSoftAssertFailure(error);
}
}
finally {
Logger.logStatus(status, message);
}
}
/**
* Selects the radio button option by the given label.
* @param {any} locator - The element locator as a string or Element.
* @param {string} labelValue - The label of the option to select.
* @param {number} [timeOut=30000] - (Optional) The timeout in milliseconds to wait for the element to be visible.
* @returns {Promise<void>}
*/
// Accepts element locator as string or Element and selects the option by given label
async selectByLabel(locator, labelValue, 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 });
message = `Option with label "${labelValue}" has been selected successfully`;
}
catch (error) {
status = 'Failed';
if (error instanceof Error) {
message = error.message;
Logger.logSoftAssertFailure(error);
}
else {
message = "An unknown error occurred during the selection of the option by label";
Logger.logSoftAssertFailure(error);
}
}
finally {
Logger.logStatus(status, message);
}
}
}