简体   繁体   中英

How to know whether Text is asserted

In my application there is a case where a screen gives 2 types of alerts in 2 scenarios. I need to assert the text in the alert and close the alert.

I have used the following:

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

Using this I am able to accept the alert. However, even if some other alert is displayed, the alert will be close and it is not shown whether the alert matches the text given.

So how do I find whether the text matches or not?

You can set a boolean to true if it matches, and use it for your purpose accordingly. Sample method below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM