Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Assert Equals to Value1 or Value2

I want to test in Selenium (Java, Testng) If my actual value is equal to one of the two values because my value can be Value1 or Value2 and both values will be correct.

If I want to assert only one equality I use this construction:

String expectedPopUpV1 = "Value1";
String expectedPopUpV2 = "Value2";
String actualPopUp = driver.findElement(By.cssSelector(Value)).getText();
Assert.assertEquals(actualPopUp,expectedPopUp);

but what should I do if I want to make something like

Assert.assertEquals(actualPopUp,expectedPopUp1||expectedPopUp1);
like image 482
Evgenii Truuts Avatar asked Feb 23 '26 08:02

Evgenii Truuts


2 Answers

You can use assertTrue(boolean condition, String message) for this and give the details in the message

boolean isEqual = actualPopUp.equals(expectedPopUpV1) || actualPopUp.equals(expectedPopUpV2);
Assert.assertTrue(isEqual, "The text " + actualPopUp + " is not " + expectedPopUpV1 + " or " + expectedPopUpV2);
like image 84
Guy Avatar answered Feb 25 '26 20:02

Guy


below option should also work using ternary operator:

Assert.assertEquals(expectedPopUpV1.equalsIgnoreCase(actualPopUp )?expectedPopUpV1:expectedPopUpV2 , actualPopUp );
like image 26
irfan Avatar answered Feb 25 '26 22:02

irfan