Selenium Handle条件断言

时间:2014-10-13 14:28:03

标签: selenium selenium-webdriver jbehave

我正在做一个UI动作,有两种可能性;

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

现在我需要在Selenium中断言相同。

再一次,UI上只有一个结果......要么我得到一条信息消息,要么我得到一条成功消息..

所以基本上我需要断言以下任何一个;

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

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

我是否应该尝试捕获或使用if ... else?

1 个答案:

答案 0 :(得分:1)

您可以使用将布尔值作为参数的断言方法,并进行条件OR检查:

对于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");

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

如果您使用的是Hamcrest匹配器,则可以执行以下操作:

assertThat(alertMessage, anyOf(equalTo(infoMessage), equalTo(successMessage)));