There is a scenario where I need to perform an action(delete or add) on a particular element. But that element may belong to different classes and depending on the class. I have to perform the action on that element.
Example: There are 3 classes (class1, class2, and class3). So if that element belongs to the class "class1 and class2" Then 1st it should perform delete action and then add action. But if that element belongs to the class "class3". Then it should only perform add action.
Below is the snippet of my code.
if(class1.isPresent()) {
deleteSpecial.click();
console.log("Class 1 executed");
addSpecialCard.element(addSpecial.locator()).click();
} else if(class2.isPresent()) {
deleteSpecial.click();
console.log("Class 2 executed");
addSpecialCard.element(addSpecial.locator()).click();
} else {
addSpecialCard.element(addSpecial.locator()).click();
console.log("Else executed");
}
In the above way. It always execute the 1st conditon. It never goes to the else if or else condition.
I tried in this way also. But that also didn't work.
if(class1.isDisplayed() || class2.isDisplayed()) {
deleteSpecial.click();
console.log("Class 1 executed");
addSpecialCard.element(addSpecial.locator()).click();
} else {
addSpecialCard.element(addSpecial.locator()).click();
console.log("Else executed");
}
I tried even doing this using promise. But I don't know, how can I use the else if condition in that. (given that it has to check for 3 classes). Below is the snippet of that.
class1.isDisplayed().then((result) =>{
if(result) {
deleteSpecial.click();
console.log("Class 1 executed");
addSpecialCard.element(addSpecial.locator()).click();
} else {
addSpecialCard.element(addSpecial.locator()).click();
console.log("Else executed");
}
});
Any help or suggestion will be much appreciated. I have been trying to solve this problem from weeks. But no luck. Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire