Eugene Truuts
Eugene Truuts

Reputation: 379

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);

Upvotes: 2

Views: 5602

Answers (3)

irfan
irfan

Reputation: 896

below option should also work using ternary operator:

Assert.assertEquals(expectedPopUpV1.equalsIgnoreCase(actualPopUp )?expectedPopUpV1:expectedPopUpV2 , actualPopUp );

Upvotes: 2

Timothy T.
Timothy T.

Reputation: 1091

There isn't any such method available. The two closest options I can think of:

if(!actualPopUp.equals(expectedPopUp1) || !actualPopUp.equals(expectedPopUp2) {
    Assert.fail("insert failure messsage here"); 
}

or

if(actualPopUp.equals(expectedPopUp1) || actualPopUp.equals(expectedPopUp2) {
    Assert.assertTrue("insert message");
}

The other option would be to really extend the Assert capability by constructing your own with a Builder pattern to be in the tune of:

AssertBuilder.with(actualPopUp).equals(expectedPopUp1).or(expectedPopUp2);

But that may be too complicated for a simple use case.

Upvotes: 1

Guy
Guy

Reputation: 50819

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);

Upvotes: 7

Related Questions