Friday, September 25, 2009

Changing Selenium Jar to globally handle Modal Dialog box

  • Unpack Selenium jar. (User JRE jar.exe utility)
  • Open selenium-browserbot.js file.
  • Search for function 'IEBrowserBot.prototype.modifyWindowToRecordPopUpDialogs'"
  • Add following codes at the begning of this function


if(win.showModalDialog)
{
if (typeof(win.top.g_selRetVar) == 'undefined')
win.top.g_selRetVar = null;
win.top._UTL_SetSelRetVar = function (val){
win.top.g_selRetVar = val;
win.top.status = win.top.g_selRetVar + ' is returned from child';
};
win.showModalDialog = function( sURL, vArguments, sFeatures)
{
if ((typeof(win.top.g_selRetVar) != 'undefined') && (win.top.g_selRetVar!=null))
{
var temp = win.top.g_selRetVar;
win.top.g_selRetVar = null;
return temp;
}
try {
win.top.open (sURL, 'modal', sFeatures);
} catch(e){ win.showModalDialog(sURL, vArguments, sFeatures); }
};
}
if(win.close)
{
win.attachEvent('onbeforeunload', function _selhandler()
{
if(win.opener && win.opener._UTL_SetSelRetVar)
win.opener._UTL_SetSelRetVar(win.returnValue);
});
}


  • Pack the jar again. (User JRE jar.exe utility)
If you do not wish to change your jar file you can inject the javascript through your script. This has been mentioned in
Handling Modal windows
Download modified Selenium RC

12 comments:

  1. Hi

    Can i use with Selenium selenium-server-2.0a4 alpha version.

    I am also not finding selenium.jar in selenium-server-2.0a4 alpha version.

    Can you know which version i can implement you fix

    Thanks
    Ravi

    ReplyDelete
  2. Hello,
    Could you be more specific "Unpack Selenium jar".
    I can't find file selenium-browserbot.js inside any jar.

    I am using selenium-server-1.0.3 and Java Client Driver.

    Thanks
    Hadrian Polewczak

    ReplyDelete
  3. Hi,

    I still have problem with modal window.
    In my webpage it is possible to turn off modal window for test purposes, but if I run script to show new window (no modal), I am not able to type text and click find button inside this recently opened window(Element //*[@id=\"quick_pt_text\"] not found).

    Is there any possibility to programatically switch to new window, type something inside this window and come back to main window.

    selenium.open("http://devel.lp/main.pl");
    selenium.runScript("open_find(selected_tab)");
    Thread.sleep(5000);
    // now I try to type sth inside opened window
    selenium.typeKeys("//*[@id=\"quick_pt_text\"]", "sth");
    selenium.click("//*[@id=\"find_btn\"]");
    Thread.sleep(5000);

    assertTrue(true);

    PS
    Sorry for previous comment, I have already find selenium-browserbot.js

    Hadrian Polewczak

    ReplyDelete
  4. Hi,

    I add the code above in the selenium-browserbot.js in my selenium-server.jar.

    However my test isn't working. At the beginning of the test, it is like "suspended". I can't open the url I want...

    What's wrong ?

    ReplyDelete
  5. Modified Selenium Server has been added in the post.

    ReplyDelete
  6. It's already not working.

    I open the jar to see the code and I don't find any added code in the BrowserBot.prototype._modifyWindow method in selenium-browserbot.js file. Is it normal ?

    ReplyDelete
  7. Modification is done in IEBrowserBot.prototype.modifyWindowToRecordPopUpDialogs function.

    Please verify it again this should work.

    ReplyDelete
  8. Hi Amit,

    I am a recent user of selenium and I currently have to automate on IE browser for my current project. I have attached your updated jar to my webdriver project but I am not sure how to use this change to interact with a modal window that is currently blocking me from moving forward. Can you provide me with some coding example of how to use your update, I am using java/groovy with webdriver.

    Thanks in advance

    ReplyDelete
  9. Hi Amit,

    I am a recent user of selenium and I currently have to automate on IE browser for my current project. I have attached your updated jar to my webdriver project but I am not sure how to use this change to interact with a modal window that is currently blocking me from moving forward. Can you provide me with some coding example of how to use your update, I am using java/groovy with webdriver.

    Thanks in advance

    ReplyDelete
  10. Hi Amit,
    Firstly, let me thank you for sharing such wealth of information with the world.
    I just have one query, will this function work in SeleniumRC 2.20 version
    Warm Regards
    Prashant

    ReplyDelete
  11. Prashant,

    With no or little tweek you can use this code in any SEleniumRC version.

    ReplyDelete
  12. i have changed the JAR as mentioned, but it is not able to get the modal window id, when i used
    selenium
    .getEval("{var windowId; for(var x in selenium.browserbot.openedWindows ) {windowId=x;} }")


    pls help.

    ReplyDelete