Selenium Navigate Back and jQuery Dirty Forms plugin

107 Views Asked by At

I have a few forms that use the jQuery Dirty Forms plugin to alert users before they attempt to navigate away.

Until Chrome 52, I was able to run a test for the alert raised by the plugin with Selenium. I called driver.Navigate().Back();, which would trigger an alert box. I could then handle the alert using the code below:

d.Navigate().Back();

try
{
    // Expecting an alert now
    IAlert a = d.SwitchTo().Alert();

    // And acknowledge the alert (equivalent to clicking "OK")
    a.Accept();
}
catch (NoAlertPresentException ex)
{
    System.Diagnostics.Debug.Write($"Warning: Expected an alert but none was present. {ex.Message}");
}

However, since Chrome 52, the behavior of .Back() appears to have changed. Now, an InvalidOperationException is thrown with the message

unknown error: cannot determine loading status
from unexpected alert open
(Session info: chrome=53.0.2785.116)
(Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 10.0.10586 x86_64)

I am discarding the Exception at this time, but the next test won't run because of the alert. Specifically, that means that I cannot run multiple tests sequentially. I have to run each test individually, manually acknowledge the alert so Chrome will close

I am on the latest version of Selenium (2.53.1) and have tried ChromeDriver 2.22/23/24. All have the same behavior.

I know no one here can control Chrome, but I am wondering if there are alternatives I could employ to achieve this objective of testing to make sure the dirty forms alert appears. I can't really change from Chrome to another browser because of some other issues. Submitting the form is also something I don't want to do (in case the dirty forms doesn't get triggered, it would modify the database and I don't want to work around that).

Update: 2016-09-22

I am working around the issue with the following code for now.

try
{
    // Navigate back to the previous page
    d.Navigate().Back();

    try
    {
        IAlert a = d.SwitchTo().Alert();

        // Get the text of the alert or prompt
        System.Diagnostics.Debug.WriteLine(a.Text);
        // And acknowledge the alert (equivalent to clicking "OK")
        a.Accept();
    }
    catch (NoAlertPresentException ex)
    {
        System.Diagnostics.Debug.Write($"Warning: Expected an alert but none was present. {ex.Message}");
    }
}
// Chrome 52: this is how the driver does it now?
catch (InvalidOperationException ex)
{
    if (!ex.Message.Contains("unexpected alert"))
        throw;

    IAlert a = d.SwitchTo().Alert();
    Assert.IsTrue(a.Text.Contains("lose these changes"));

    a.Accept();

    System.Diagnostics.Debug.WriteLine("Handled alert through InvalidOperationException handler.");
}
1

There are 1 best solutions below

0
SvenAelterman On

With the release of ChromeDriver 2.29, this has been fixed. Per ChromeDriver release notes:

Fixes a bug that interferes with handling the alert dialog on page unload.

I am now running Chrome 58 and also updated to the latest Selenium libraries (3.4.0). The code I was originally using (from the original post) is now working again.

Original code copied for reference:

d.Navigate().Back();

try
{
    // Expecting an alert now
    IAlert a = d.SwitchTo().Alert();

    // And acknowledge the alert (equivalent to clicking "OK")
    a.Accept();
}
catch (NoAlertPresentException ex)
{
    System.Diagnostics.Debug.Write($"Warning: Expected an alert but none was present. {ex.Message}");
}