Jason Huggins, the creator of Selenium, presented at JSConf 2009 and demonstrated a way around Javascript alerts that Selenium cannot handle. After speaking with Jason, he provided me with the original code snippet from Aaron Boodman and I have created a Firefox extension for general use that can be downloaded from my Github site.
The magic happens in a function called alertClose. If all you need to do is close an alert it should work unmodified for you. If you need to do something a little more exotic you may need to edit the subject.location which specifies what types of windows it closes. By default it points to chrome://global/content/commonDialog.xul, which is the chrome type for alert boxes. There is a Mozillazine article that has a listing of the various types of chrome windows if you need to customize.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a convenience method that exists to determine if the window should be closed or not. | |
// There could be a lot more logic here, but for now we are just closing any window whose | |
// location property is equal to "chrome://global/content/commonDialog.xul", which is what the | |
// alert boxes that I have tested use. | |
alertClose: function (subject) { | |
if(subject.location == "chrome://global/content/commonDialog.xul") { | |
window.setTimeout(function() { subject.close(); }, 1000); | |
} | |
} |
So there you have it. A very simple way to get around Javascript alerts. Hopefully this solution is only temporary and the new WebDriver code being integrated into Selenium 2 will work around it in a more elegant way.
Hello Dan,
ReplyDeleteThe Article on Closing Javascript Alerts in Selenium Tests is good. It give detail information about it .Thanks for Sharing the information about Selenium Web Driver Tools. mobile application testing