1

at few places, am facing some unhandled alerts, so for every click planning to check if alert is present, for that am using the following code,

public boolean isAlertPresent(){
    try{
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        Alert a=driver.switchTo().alert();
        a.accept();
        return true;
    }
    catch (NoAlertPresentException e) {
        return false;
    }
    finally{
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
}    

but the above code is taking some time to check whether alert is present, as I am going to use this method for every click, its too costly to wait, making the implicit wait zero in the above code has no effect. can anybody help on this.

Muthu Kumar
  • 480
  • 1
  • 6
  • 8
  • possible duplicate of [How to wait for an alert in Selenium webdriver ?](http://stackoverflow.com/questions/12639280/how-to-wait-for-an-alert-in-selenium-webdriver) – alecxe Nov 22 '14 at 06:12
  • In a "possible duplicate" question there is an answer that might fit your needs - it is following the `EAFP` approach: try to switch and handle the exception, repeat. – alecxe Nov 22 '14 at 06:14
  • @alecxe my need is somewhat different than this, my problem is very specific about checking for an alert without any implicit wait. Say for example, if make implicit wait zero and check for an element is displayed, it will give result immediately, but while checking alert, that is not the case, even after making implicit wait to zero, it is waiting for sometime. – Muthu Kumar Nov 22 '14 at 06:20

1 Answers1

4

Implicit waits are for assigning global timeout(s), that means once assigned, selenium will wait for that amount of time (at max), each time it tries to find an element. On the other hand, Explicit waits are useful in assigning timeouts exclusively to any element in the webpage, and it overrides the timeout so set by implicit wait.

So, for the above, you can try Explicit wait instead like this:

public boolean isAlertPresent(){ 
    try{ 
        Alert a = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
        if(a!=null){
            System.out.println("Alert is present");
            driver.switchTo().alert().accept();
            return true;
        }else{
            throw new Throwable();
        }
    } 
    catch (Throwable e) {
        System.err.println("Alert isn't present!!");
        return false; 
    } 

} 

This will check the existence of alert within 10 seconds. If it finds the alert within that, it will return 'true' else it will return 'false'.

Subh
  • 4,354
  • 1
  • 13
  • 32
  • Hi Subh, I don't want to wait for 10 secs to check if the alert is present, I want to check immediately within 0 sec as I am going to check it for every click in automation – Muthu Kumar Nov 23 '14 at 17:31
  • @MuthuKumar: When putting explicit wait, for above, the wait time ranges till 10 seconds. It means, whenever the alert comes into picture, may it be some milliseconds, or more than that (at max 10 seconds), then it will be switched to, and further actions will proceed hence. But, if you want to wait till some milliseconds try this code: `Alert a = new WebDriverWait(driver,0,500).until(ExpectedConditions.alertIsPresent());` It will wait for 500 milliseconds for the alert. – Subh Nov 23 '14 at 17:43
  • Why this function need to be boolean? I mean it is accepting the alert always, whether you want to accept or not. What am I going to do when it returns false/true? – paul Apr 03 '18 at 10:28
  • @paul I have just modified OP's method where the return type was boolean already. You can change the return type as per your need as well, say "String" or might as well keep it as void. In the above case, it will be helpful to assert that whether the alert is present or not; here browser alert is taken into consideration. For instance, if it's a explicitly generated alert/modal by DEV in the browser, the above method will return false, inferring that the alert isn't present, and you can modify accordingly. You can further optimize the method by printing stacktrace to track the exact issue. – Subh Apr 09 '18 at 09:39