TestRaptor
TestRaptor

Reputation: 1315

How to verify an alert is not present using Selenium and Java

I have tests that involve required fields and alert boxes. When you don't fill out the required field, an alert box should display. I am checking for this alert box by using

ExpectedConditions.alertIsPresent()

When I fill out the required field, I want to verify that an alert no longer appears. How can I do this using Selenium and Java?

Upvotes: 1

Views: 2277

Answers (2)

user3535807
user3535807

Reputation: 268

in case of webdriver

public boolean isAlertPresent(){
    try{
        driver.switchTo().alert();
        return true;
    }catch (NoAlertPresentException e){
        return false;
    }
}

Upvotes: 1

StrikerVillain
StrikerVillain

Reputation: 3776

Use the following code snippet

public boolean isAlertPresent() {
    try {
        new WebDriverWait(driver,10).untill(ExpectedConditions.alertIsPresent);
        return true;
    } catch (TimeOutException e) [
        return false;
    }
}

Upvotes: 0

Related Questions