Alert doesn't display when link clicked via WebDriver

476 views
Skip to first unread message

skd

unread,
Aug 29, 2012, 12:42:43 PM8/29/12
to seleniu...@googlegroups.com
I've search through the forums, but haven't found any issues that match the problem I'm seeing. My apologies if this has been answered before...

My AUT has a link for deleting an object from the page.  When I click the link manually, a JavaScript confirm box displays with Ok/Cancel buttons, but when my test clicks the link, no popup is displayed and the object is deleted immediately! 

JavaScript on the page:
<a href="javascript:deleteSample('17');">
 <img title="Delete Sample" alt="Delete" src="images/delete.gif"/>
</a>

JavaScript from .js file:
function deleteSample(sampleId) {
    var sampleName = $('sampleName_' + sampleId).value;
    confirmDelete = confirm("Are you sure you want to delete sample '" + sampleName + "'?");
    if (sampleId != null && confirmDelete == true) {
        new Ajax.Request(window.location.href, {
            parameters: {
                requestingMethod: "sampleSelector.deleteSample",
                datasetId: dataset,
                deleteSample: "true",
                sampleSelectorId: sampleId
            },
            onComplete: this.deleteStatus,
            onException: function (request, exception) { throw(exception); }
        });
        messagePopup("Deleting sample '" + sampleName + "'... (This may take a few moments)", "MESSAGE");
    }
}

Test code (which times out on the waitForAlert():
String deleteLocator = "//a[@href=\"javascript:deleteSample('"+sampleId+"');\"]";
WebElement link = driver.findElement(By.xpath(deleteLocator));
link.click();
waitForAlert(5, driver);
assertTrue(driver.switchTo().alert().getText().matches("^Are you sure you want to delete sample '"+sampleName+"'[\\s\\S]$"));
driver.switchTo().alert().accept();

I'm using Firefox 14.0.1 with Selenium 2 v2.25.0.

Any help resolving this would be greatly appreciated.

Justin Gourley

unread,
Aug 29, 2012, 6:26:15 PM8/29/12
to seleniu...@googlegroups.com
I believe the webdriver automatically gets rid of alerts.... if you need to run a check you can getText on the alert object... something like this

public String getAlert() {
        Alert alert = driver.switchTo().alert();
        String str = alert.getText();
        alert.accept();
        return str;
    }


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/xFB55Zi8Ap8J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Peter Gale

unread,
Aug 30, 2012, 2:41:51 AM8/30/12
to Selenium Users
I think Justin is right in saying that WebDriver is automatically handling your alert, but then you would not be able to handle the alert manually becauase WebDriver has accepted it for you already and so effected the download.

Not sure how you turn off the autohandling feature for the duration of your popup if that is the case, but it should be documented on Selenium HQ somewhere or someone else on the forum might know.
 

Date: Wed, 29 Aug 2012 15:26:15 -0700
Subject: Re: [selenium-users] Alert doesn't display when link clicked via WebDriver
From: justin....@gmail.com
To: seleniu...@googlegroups.com

skd

unread,
Aug 30, 2012, 11:06:37 AM8/30/12
to seleniu...@googlegroups.com
Well, actually, it's a confirmation with OK/Cancel buttons, not an alert.  If WebDriver is automatically accepting it, there is no way to cancel the alert and thus prevent the delete. If you look at the test code shown in my original post, it does try to get the alert. As far as I can tell, the popup never displays.

I'm wondering now if the problem could be caused by a setting in my Firefox profile.

Peter Gale

unread,
Aug 30, 2012, 12:25:42 PM8/30/12
to Selenium Users
If it' is not displaying when running under Selenium then it would be best to put some debugging statements into the Javascript to see why is is not displaying and seemingly going straight on to do the deletion.


Date: Thu, 30 Aug 2012 08:06:37 -0700
From: skd...@gmail.com
To: seleniu...@googlegroups.com
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/H7fkL9PQHOwJ.

skd

unread,
Aug 31, 2012, 11:51:04 AM8/31/12
to seleniu...@googlegroups.com
I found a reference to a similar issue for WebDriverBackedSelenium with a workaround here. So, I've reverted my test back to using WebDriverBackedSelenium instead of WebDriver, and the workaround seems to fix my problem.

Now, I just need to figure out how to implement the workaround for my old HTML-based tests...

Dibyaranjan Kar

unread,
Sep 4, 2012, 1:17:52 PM9/4/12
to seleniu...@googlegroups.com
skd,

I am also facing the exact problem but in IE. Good thing for you is "handlesAlerts" capability has been added to firefoxdriver. Where you can tell driver to ignore all the alerts came in your way. just set the capability to ignore and its done.

Thanks
Dibya

Jim Evans

unread,
Sep 4, 2012, 1:28:14 PM9/4/12
to seleniu...@googlegroups.com
The capability your looking for is "unhandledAlertBehaviour", and it has also been added to IE.
Message has been deleted

Dibyaranjan Kar

unread,
Sep 6, 2012, 12:35:51 AM9/6/12
to seleniu...@googlegroups.com
Hello Jim,

Is it added to Java language binding? I can get it work with FF but not in IE.

Selenium Version -: 2.25
Language -: Java


Thanks
Dibya

On Wednesday, 29 August 2012 22:12:43 UTC+5:30, skd wrote:
Reply all
Reply to author
Forward
0 new messages