0

I am doing a UI action and there are 2 possibilities;

1. I get an info message OR
2. I get a success message

Now I need to assert the same in Selenium.

Again there is only one outcome possible on the UI...either I get an info message OR i get a success message..

So basically I need to assert on either of the following;

assertThat(getAlertMessage("info", "cancel info message").getText(), equalTo("cancel info message"));

assertThat(getAlertMessage("success", "cancel success message").getText(), equalTo("cancel success message"));

Should I do a try catch OR use if...else ?

2
  • Which tool are you using to do the asserts? Junit, TestNG or some other tool? Commented Oct 13, 2014 at 15:06
  • Are you looking to using hamcrest matchers in your assert? Commented Oct 13, 2014 at 16:42

1 Answer 1

2

You could use the assertion methods that take booleans as parameters and just do a conditional OR check:

For TestNG:

String alertMessage = getAlertMessage("info", "cancel info message").getText();
String infoMessage = "cancel info message";
String successMessage = "cancel success message";

assertTrue(alertMessage.equals(infoMessage) || alertMessage.equals(successMessage), "Alert message does not equal either info or success message");

For JUnit:

String alertMessage = getAlertMessage("info", "cancel info message").getText();
String infoMessage = "cancel info message";
String successMessage = "cancel success message";

assertTrue("Alert message does not equal either info or success message", alertMessage.equals(infoMessage) || alertMessage.equals(successMessage));

If you are using Hamcrest matchers, you can do this:

assertThat(alertMessage, anyOf(equalTo(infoMessage), equalTo(successMessage)));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.