如何知道Text是否被断言

时间:2014-01-22 08:46:46

标签: java eclipse selenium selenium-webdriver

在我的应用程序中,有一种情况是屏幕在2种情况下提供2种类型的警报。我需要在警报中断言文本并关闭警报。

我使用了以下内容:

Alert javascriptAlert= driver.switchTo().alert();
String text= javascriptAlert.getText();
assertTrue(text.matches("Saved Successfully."));
javascriptAlert.accept();

使用此功能,我可以接受警报。但是,即使显示其他警报,警报也会关闭,并且不会显示警报是否与给定的文本匹配。

那我如何找到文本是否匹配?

1 个答案:

答案 0 :(得分:0)

如果匹配,则可以将布尔值设置为true,并相应地将其用于您的目的。以下示例方法:

public boolean matchAlertTextAndClose(String textToMatch){
    Alert javascriptAlert= driver.switchTo().alert();
    String text= javascriptAlert.getText();
    boolean doesTextMatch = false;
    if(text.matches(textToMatch)){
        doesTextMatch = true;
    }
    else{
        doesTextMatch = false;
    }
    javascriptAlert.accept();
    return doesTextMatch;
}
相关问题