I want to share my experience of trying to run Selenium tests against an application that calls alert() and confirm() during the onload() event handlers of its pages.

It's quite explicit in the documentation that this functionality is not currently supported:

> Selenium does NOT support _javascript_ alerts that are generated in a page's onload() event handler
> Selenium does NOT support _javascript_ confirmations that are generated in a page's onload() event handler.

I wasn't able to fix the limitation, but I did come up with a work-around, provided you are prepared to modify your application slightly.

I inserted the following code into a common block of _javascript_ that gets included in all pages of the application, and gets executed at the beginning on the onload() event handler, before any calls to alert() or confirm():

if (parent.browserbot) {
   parent.browserbot.modifyWindowToRecordPopUpDialogs(window, parent.browserbot);

This worked fine for my project.  There is some concern about leaving a reference to the test infrastructure in the page that gets served to real users' browsers.  If this really bothers you, it should be quite easy to exclude these few lines using a build flag, so that they only appear in special deployments of the application that will be tested using Selenium.

In general of course I'd recommend to avoid alert() and confirm() during page load altogether - I don't think it's a great way of doing things, but maybe you'll be stuck with it too!

Alistair Jones
Developer, ThoughtWorks UK
Selenium-users mailing list

Reply via email to