0

ERROR net.serenitybdd.core.Serenity - No alert is present (WARNING: The server did not provide any stacktrace information)

I get this error when I try to detect an alert with this code:

Alert alertBox = getDriver().switchTo().alert();

The alert popup does show up, but it is not exactly the same when I execute the operation via Selenium WebDriver and manualy. This might be the source of the problem but I don't know why the popup is different.

The alert when done manualy

The alert when done with Selenium

This is the function that calls the alert:

function confirm_remove() {
    return window.confirm("Alert message");
}

Thanks for your help

UPDATE:

Ok I found a way to bypass the problem but not really solve it.

I tried inserting waits and sleep like you suggested me to but it did not solve the problem, the alert was still undetected.

In my project I am using a Test class which calls steps from my Steps class which calls webElements from my Page class. The thing is that between the step which clicks on the "Delete" button and the step that manages the alert, Selenium loses the handling of the alert. So I regrouped those two steps and Selenium seems to handle the alert well.

Pierrick
  • 1
  • 1
  • 2

2 Answers2

0

You might need to add code to wait for the alert to be visible. Selenium can't tell if JavaScript has finished executing.

waitForAlert(WebDriver driver)
{
   int i=0;
   while(i++<5)
   {
        try
        {
            Alert alert = driver.switchTo().alert();
            break;
        }
        catch(NoAlertPresentException e)
        {
          Thread.sleep(1000);
          continue;
        }
   }
}
MikeJRamsey56
  • 2,779
  • 1
  • 20
  • 34
  • Thanks, I tried using this but Selenium still can't handle the alert – Pierrick Mar 03 '16 at 08:44
  • Try getting all the open windows and then open each in turn and determine if that is the alert window. When you find the alert continue with your processing. Don't forget to save the original window handle so that you can switch back to it when you are finished. See [How to get handlers for all open windows and browsers browsers on selenium?](http://stackoverflow.com/questions/31929347/how-to-get-handlers-for-all-open-windows-and-browsers-browsers-on-selenium) – MikeJRamsey56 Mar 03 '16 at 18:12
0

A little more elegant solution :

WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.alertIsPresent());

Use the WebDriverWait, every time you have dynamic element that are not present when the page is done loading, like alert, popupwindow, modal popup, hiden element which turn visible,.

jpprade
  • 3,497
  • 3
  • 45
  • 58
  • Yes, you are right of course. When things are not working as expected I tend to start with loops. I can add debug statements, set a breakpoint and then take a look around. Once things are working I replace the loops with actions. Could I break on the wait? Probably .... Old habits. – MikeJRamsey56 Mar 05 '16 at 19:35