Skip to content

Commit 8e4c51c

Browse files
committed
added method
1 parent fc29796 commit 8e4c51c

1 file changed

Lines changed: 114 additions & 5 deletions

File tree

src/helper/web/webHelper.ts

Lines changed: 114 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class WebHelper extends Helper {
6363
retVal = await this.changeToggleStatus(
6464
locator!,
6565
locatorName,
66-
valueToUse
66+
valueToUse!
6767
);
6868
break;
6969
case "checkbox":
@@ -96,7 +96,7 @@ export class WebHelper extends Helper {
9696
break;
9797
case "radiobutton":
9898
let letCurrentRadioVal = await this.getRadioValue(
99-
locator,
99+
locatorValue,
100100
objName
101101
);
102102
if (
@@ -110,7 +110,7 @@ export class WebHelper extends Helper {
110110
);
111111
}
112112
letCurrentRadioVal = await this.getRadioValue(
113-
locator,
113+
locatorValue,
114114
objName
115115
);
116116
if (letCurrentRadioVal !== valueToUse) {
@@ -319,7 +319,9 @@ export class WebHelper extends Helper {
319319
mandatory,
320320
all
321321
);
322-
if (!result) return [];
322+
if (!result) {
323+
return [];
324+
}
323325
return result.all();
324326
} catch (error) {
325327
const errorMessage =
@@ -1013,6 +1015,79 @@ export class WebHelper extends Helper {
10131015
return false;
10141016
}
10151017

1018+
@step("getRadioValue")
1019+
async getRadioValue(locator: string, fieldName:string): Promise<string| null> {
1020+
let value = '';
1021+
try{
1022+
const radioElements = await this.findAllElements(locator);
1023+
if (!radioElements || radioElements.length === 0) {
1024+
logError(`No radio elements found for locator: ${locator}`);
1025+
return value;
1026+
}
1027+
1028+
for (const radioElement of radioElements) {
1029+
//To Do : Add code to select radio button
1030+
}
1031+
return value || null
1032+
}catch(error){
1033+
const errorMessage = error instanceof Error ? error.message : String(error);
1034+
logError(`Error in getRadioValue for locator [${locator}]: ${fieldName}. Error: ${errorMessage}`);
1035+
return null;
1036+
}
1037+
1038+
}
1039+
1040+
@step("changeRadioStatus")
1041+
async changeRadioStatus(radioLocator: string, radioName: string,expectedStatus: string): Promise<boolean> {
1042+
let retVal = true;
1043+
let actualStatus = await this.getRadioValue(radioLocator, radioName);
1044+
let i = 1;
1045+
while (actualStatus !== expectedStatus) {
1046+
try {
1047+
const radioElements = await this.findAllElements(radioLocator);
1048+
if (!radioElements || radioElements.length === 0) {
1049+
logError(`No radio elements found for locator: ${radioLocator}`);
1050+
return false;
1051+
}
1052+
1053+
let found = false;
1054+
for (const radioElement of radioElements) {
1055+
const radioText = (await radioElement.textContent())?.trim() || "";
1056+
if (radioText.toLowerCase() === expectedStatus.toLowerCase()) {
1057+
await this.click(radioElement);
1058+
found = true;
1059+
break;
1060+
}
1061+
}
1062+
1063+
if (!found){
1064+
logInfo(`Radio not found with expected status: ${expectedStatus}. Retrying... (Attempt ${i})`);
1065+
}
1066+
await this.delay(1);
1067+
actualStatus = await this.getRadioValue(radioLocator, radioName);
1068+
if (actualStatus === expectedStatus) {
1069+
retVal = true;
1070+
break;
1071+
}
1072+
i++;
1073+
1074+
1075+
} catch (error) {
1076+
const errorMessage = error instanceof Error ? error.message : String(error);
1077+
logError(`Error in changeRadioStatus for locator [${radioLocator}]: ${radioName}. Error: ${errorMessage}`);
1078+
}
1079+
1080+
if (i === WaitFor.LOW_RETRY_COUNT) {
1081+
logError(`Failed to change radio status to ${expectedStatus} after ${i} attempts.`);
1082+
expect.soft(false, `Failed to change radio status to ${expectedStatus} after ${i} attempts.`).toBeTruthy();
1083+
retVal = false;
1084+
break;
1085+
}
1086+
}
1087+
logInfo(`Radio status changed to ${expectedStatus}`);
1088+
return retVal;
1089+
}
1090+
10161091
@step("setCheckBoxStatus")
10171092
async setCheckBoxStatus(el: Locator, state: string = "true") {
10181093
let isChecked = await el.isChecked();
@@ -1064,8 +1139,9 @@ export class WebHelper extends Helper {
10641139
let locator: Locator | null = null;
10651140
if (typeof selectorOrLocator === "string") {
10661141
locator = await this.findElement(selectorOrLocator, "xpath", true);
1142+
}else{
1143+
locator = selectorOrLocator as Locator;
10671144
}
1068-
locator = selectorOrLocator as Locator;
10691145

10701146
if (!locator) {
10711147
expect(false, "Locator not found").toBeTruthy();
@@ -1092,6 +1168,39 @@ export class WebHelper extends Helper {
10921168
return isChecked;
10931169
}
10941170

1171+
@step("uncheck")
1172+
async uncheck(selectorOrLocator: Locator | string): Promise<boolean> {
1173+
let locator: Locator | null = null;
1174+
if (typeof selectorOrLocator === "string") {
1175+
locator = await this.findElement(selectorOrLocator, "xpath", true);
1176+
}else{
1177+
locator = selectorOrLocator as Locator;
1178+
}
1179+
1180+
if (!locator) {
1181+
expect(false, "Locator not found").toBeTruthy();
1182+
return false;
1183+
}
1184+
const value = await locator.getAttribute("class")||"";
1185+
if (value?.includes("disable")) {
1186+
logInfo("Element is disabled, cannot uncheck");
1187+
expect.soft(false, "Element is disabled, cannot uncheck").toBeTruthy();
1188+
return false;
1189+
}
1190+
1191+
let isChecked = await this.isChecked(locator);
1192+
if (!isChecked) {
1193+
logInfo("Element is already unchecked");
1194+
return true;
1195+
} else {
1196+
await locator.click();
1197+
}
1198+
1199+
isChecked = await this.isChecked(locator);
1200+
logInfo(`Element is unchecked: ${isChecked}`);
1201+
return isChecked;
1202+
}
1203+
10951204
@step("getValueFromArray")
10961205
getValueFromArray(testData: string[], preVal: string) {
10971206
const currentIndex = testData.indexOf(preVal);

0 commit comments

Comments
 (0)