Vinoygeorge's Blog

Selenium RC and Pop-up handling

Home > selenium > Selenium RC and Pop-up handling

Selenium RC and Pop-up handling

Well its been a while since I found some time to get back to my blog. I started this blog anticipating at least one blog enty per week, but recently have been really busy at work that it took me almost 6 months to write my second blog entry.

I am not going to plan anything like this again and will try to find some time to write about my learnings while using Selenium RC with TestNG, Maven and Anthill Pro.

Recently I have been asked by one of the readers about the pop-up handling in Selenium in general and Selenium RC in specific. As per my understanding Selenium IDE does not support recording on the pop-ups because If the popup is a new window, i.e., not in the html body of the main winodw, Selenium IDE, Dom Inspector, and other Firefox plugins do not work. The reason is there are multiple document variables to handle and it might not be a good idea to record any popup window which make the event listener more complicated.

You can use Selenium RC to handle the pop ups. There are different methods available to deal with different types of pop ups. Following are some methods that I have used to handle JavaScript alerts using Selenium RC java client.


selenium.chooseCancelOnNextConfirmation()
selenium.chooseOkOnNextConfirmation()

These method calls should be executed before the event that generates the JavaScript alerts. e.g. if clicking a Delete button pops up a java script alert with OK and Cancel button then your functions call would be.

selenium.chooseOkOnNextConfirmation();
selenium.click(buttonLocator);

This would instruct selenium to click OK on the pop up that appears as a result of clicking the delete button. You can also assert on the pop up in the next statement. There is also a function available to verify that the alert is present.


isAlertPresent() - this would return a boolean (true/false) based on the alert existence

I have not used Selenium RC to verify the login dialogs but there are some functions available to handle pop ups like these.

e.g The following code will use the selectWindow, windowFocus and close functions to work on a pop up/dialog.


selenium.selectWindow("window id");
selenium.windowFocus();
selenium.type("username", "user name");
selenium.type("password", "password");
selenium.click("logingButton");
selenium.waitForPageToLoad("30000");
selenium.close();

Sometimes you might need to assert on the pop ups. e.g. you might want to assert that on deleting a button a pop up appears. You can use waitForPopUp to make sure that your test script waits for the pop up to appear and does not give you a time out error.


assertEquals("Delete Confirmation", selenium.getAlert());
selenium.waitForPopUp("deleteWindowLocator", "30000");
selenium.selectWindow("deleteWindowLocator");

If you have custom defined pop-ups then you might want to use  Java Robot to do deal with custom define pop-ups. The Robot class is defined in java.awt package. You can control the mouse and keyboard events using Robot class. It is not an elegant solution but it works.

You can also use some other tools (AutoIt) to keep listening for certain events such as SSL security certificates or download dialogs and click on OK whenever these type of pop up appear.

There is another way of dealing with the file download pop ups. You can define a custom firefox profile and disable the file download pop up, so that it never appears during the test execution but still downloads the stuff on the hard disk which you can verify for testing purposes. You then launch selenium server with this custom firefox profile.

There can be many ways to acheive the same task. e.g. you can define a JavaScript function to handle the pop ups and make that available to Selenium RC via user extensions. This is the beauty of open source tools that you have source code available to you and you can tweak things around according to your own requirements.

I hope that it will help you in dealing with pop ups. Please feel free to comment and ask questions.


Possibly related posts: (automatically generated)

Be the first to like this post.
  1. omsis
    April 28, 2009 at 8:49 am | #1

    Hi epyramid,

    Thank You very much. It is an excellent post about selenium pop-ups.

  2. omsis
    May 11, 2009 at 12:53 pm | #2

    Hi epyramid,
    Here is I am facing a problem for giving the focus to a pop-up window. I need your help to resolve it. Please help me out. The problem is as follows:

    I am using selenium RC(java). In my application there is a link. when I am clicking on that link it is opening a pop-up window.The pop-up window has title but has not windowID. Problem is I am not able to select that pop-up window by the command: selenium.selectWindow(“window title name”);

    The code used for generating the pop-up window is like following:

    Add/Update Team

    Here when I am clicking on Add/Update link, it is calling a javascript function called fnAddUpdateTeam(“EDIT”) which contains the window.open.

    The function is like:

    window.open(url,””,”toolbar=no,status=yes,menubar=no,location=no,resizable=yes,scrollbars=yes,width=1000,height=600,top=80,left=80″);

    Please suggest how to give focus to this pop-up window???

    • epyramid
      May 11, 2009 at 1:20 pm | #3

      Read this, it might help you.

      http://stackoverflow.com/questions/99045/handling-browser-pop-up-windows-with-selenium

      If this does not work for you, then the easiest way is to coordinate with
      the development team and ask them to give the window a title so that you can
      test it easily, otherwise you can use selenium.getAllWindowsId and get the
      id of the window you are interested in and then call the selectWindow with
      that ID.

      I hope this helps.

      Haroon

  3. omsis
    May 11, 2009 at 2:14 pm | #4

    Hi

    Thanks for your response.
    The pop-up has a title but the command
    selenium.selectWindow(“window title”); is not working and the window name is blank in my case as I have mentioned above. Any further suggetion please???

    I visited the link you have mentioned but thay used a windowID to work around.But is there any suggestion for above case??

    • epyramid
      May 11, 2009 at 2:56 pm | #5

      This is from the Selenium RC API documenation. You need the windowId to
      select the window not the window title as documented below.

      selectWindow

      void *selectWindow*(java.lang.String windowID)

      Selects a popup window; once a popup window has been selected, all commands
      go to that window. To select the main window again, use null as the target.

      Note that there is a big difference between a window’s internal JavaScript
      “name” property and the “title” of a given window’s document (which is
      normally what you actually see, as an end user, in the title bar of the
      window). The “name” is normally invisible to the end-user; it’s the second
      parameter “windowName” passed to the JavaScript method window.open(url,
      windowName, windowFeatures, replaceFlag) (which selenium intercepts).

      Selenium has several strategies for finding the window object referred to by
      the “windowID” parameter.

      1.) if windowID is null, (or the string “null”) then it is assumed the user
      is referring to the original window instantiated by the browser).

      2.) if the value of the “windowID” parameter is a JavaScript variable name
      in the current application window, then it is assumed that this variable
      contains the return value from a call to the JavaScript window.open()
      method.

      3.) Otherwise, selenium looks in a hash it maintains that maps string names
      to window “names”.

      4.) If *that* fails, we’ll try looping over all of the known windows to try
      to find the appropriate “title”. Since “title” is not necessarily unique,
      this may have unexpected behavior.

      If you’re having trouble figuring out what is the name of a window that you
      want to manipulate, look at the selenium log messages which identify the
      names of windows created via window.open (and therefore intercepted by
      selenium). You will see messages like the following for each window as it is
      opened:

      debug: window.open call intercepted; window ID (which you can use with
      selectWindow()) is “myNewWindow”

      In some cases, Selenium will be unable to intercept a call to window.open
      (if the call occurs during or before the “onLoad” event, for example). (This
      is bug SEL-339.) In those cases, you can force Selenium to notice the open
      window’s name by using the Selenium openWindow command, using an empty
      (blank) url, like this: openWindow(“”, “myFunnyWindow”).

      *Parameters:*windowID – the JavaScript window ID of the window to select
      If this does not solve your situation then you might want to do this test
      manually, because 100% test automation is not always possible. As a smart
      tester you need to sometimes evaluate the pros and cons for automating a
      test case. I would mark some tests to be done via manual testing if it is
      difficult to automate them or the it takes a lot of time to maintain brittle
      test cases.

      Thanks
      Haroon

  4. omsis
    May 11, 2009 at 3:48 pm | #6

    Thank You very much

  5. Avikash
    July 15, 2009 at 1:19 pm | #7

    hi,

    Really, this is a very good Response about selenium POP UPs handling

  6. Ed
    August 5, 2009 at 5:52 pm | #8

    Hi
    I am facing the problem with the “resend information” window from Firefox. What I am doing is a selenium.refresh then I can not go thourh the FF window. I have tried hitting “Enter” with Java robots but the script hangs there at the refresh command, it just hangs and does not continue, that means the command: robot.keyPress(KeyEvent.VK_ENTER);
    is not executed.. any idea?
    thanks

    • epyramid
      August 5, 2009 at 9:16 pm | #9

      are you running selenium in the interactive mode????

      we use selenium.refresh but our tests run in the multi window mode and have not experienced any problem with it. We do not use java robots as Selenium refresh function works for us.

  7. Ed
    August 6, 2009 at 7:32 am | #10

    Thanks for your reply.
    I tried with interactive mode and it has the same behavior.
    the problem is not the refresh function but the firefox dialog:
    “To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier”
    on that dialog I should click on the button “Resend” but I can not hit it, as I said the script does nothing more. Any idea why I can’t hit the “resend” button using java robot or using presskey selenium function ?

    thanks, ed

  8. Bruno
    August 28, 2009 at 8:07 pm | #11

    When I recorded an script with alerts the Selenium IDE save the command assertAlert with the message
    of that alert in target.

    When I play this script this command fails and the button ok isn’t pressed?

    How I can replace assertAlert with another command that works?

    Thanks so much

    Bruno

  9. Gopi
    September 16, 2009 at 3:22 am | #13

    how do you find the window id of a given pop window.I know the title but could not locate window id
    can anyone help
    I am using selenium RC with java

    • epyramid
      October 17, 2009 at 11:47 pm | #14

      I am using Firebug to locate the elements and inspect the HTML structure of the application under test.

  10. October 1, 2009 at 6:16 am | #15

    Hi,
    I am facing alert message handling using selenium.Below i have given the link to my site its an online application.In this link i will keep the mandatory field blank and click on the save button, at that time an alert message will display.But while running the same script in selenium at that time it is not displaying that pop up.Please help me regarding this.

    Regards
    Nagaraj

    • epyramid
      October 17, 2009 at 11:52 pm | #16

      Do you see the test failing due to this? In some cases the test performs the action too fast that you cant see the test closing the popups especially if you are running the tests using Firefox.

      But if the tests are failing and you expect the window to be there then the best bet would be record the action with Selenium IDE and then see what Java code it generates for the action. I use Selenium IDE when I want to make sure that I am doing the right thing. It is a good training tool.

  11. RAmesh
    October 31, 2009 at 7:11 am | #17

    This really good and thanks for every one for providing information about how to use pop ups using selenium RC

    thanks for all

  12. Subodh Singh
    November 5, 2009 at 1:27 pm | #18

    Excellent bro…..:) it is very useful
    Thanks
    ~Subodh

  13. Punit
    November 9, 2009 at 6:41 am | #19

    I have tried this but unable to work on a pop up named save.
    On page a button save logs is there when i click that button a new wpop up called save opens where i have to type the location, where i have to save files and then click on save button or press enter. but it’s not able to identify the window.

  14. Ketan Soni
    November 15, 2009 at 5:29 pm | #20

    My application spawns a new browser/window whenever i enter URL in the browser and closes the first main browser in which i’ve typed the URL.

    I tried accessing the newly spawned browser/window using “selectWindow”.

    Any workaround if that first popup window automatically closes itself?

    Thanks,
    – ketan.

    • Ketan Soni
      November 15, 2009 at 5:30 pm | #21

      I basically need to play with newly spawned window but currently m not able to get the instance of it.

  15. Madhusudhanarao
    January 6, 2010 at 11:36 am | #22

    Hi, Can you please suggest me how to handle the Security Information popup that comes when any https url is opened using Selenium.

    Thanks

  16. February 3, 2010 at 12:02 pm | #23

    I am writing an XHTML script, which will run in Selenium RC. I am trying to write code to open the dropdown list. [This dropdown is a customized control and is a combination of Text box, auto complete control ]
    I was suggested to use keypress commands, but I am beginner to Sillenium, Can anyone help me?

    drop down list items are [record1, record2..]

    when I type “record”, the drop down populates with autocomplete. now I need to select that autopopulated record by either hitting enter or down arrow key..

    Can anyone please help on this?

  17. karthik
    February 11, 2010 at 10:14 am | #24

    can you provide the code how to handle popups in selenium RC( C sharp)

  18. karthik
    February 16, 2010 at 1:07 pm | #25

    what could be the command to reload a page in c sharp…..

    i tried with selenium.WaitForPageToLoad(“30000″); and selenium.Refresh(); option both are not working could any one suggest me a beeter solution

  19. Pranab
    March 3, 2010 at 5:48 am | #26

    hi epyramid,
    this was an awesome blog for pup-up. before going through the blog i was at scale 0 specially handling pup-ups and now i feel very confident. Thanx a ton Dude….. :)

  20. vikash
    April 30, 2010 at 6:22 am | #27

    thank u for giving such a good info.

  21. May 13, 2010 at 12:46 am | #28

    Fascinating write-up. Thanks for sharing

    • Loganathan
      June 2, 2010 at 10:27 am | #29

      can u tell me how to handle the confirmation box ex:In my application if user already login to the application again i enter the same user name and password application display the confirmation box with “YES” and “NO” button let me no how do i click that button.I tried “selenium.chooseCancelOnNextConfirmation()
      selenium.chooseOkOnNextConfirmation()” but its not working.Pls help me

      • epyramid
        June 5, 2010 at 9:29 pm | #30

        can you send me the piece of code that you have???

  22. Getafix
    June 18, 2010 at 4:26 am | #31

    Hi EPyramid,

    Very useful blog indeed.
    Can you give me an example of using javascript to handle file upload/download dialogue
    I want to acheive this without using AutoIT

    Many thanks,

  23. Amita
    July 7, 2010 at 7:13 am | #32

    Hi,

    Can anyone tell me how to get the window id? I am using Selenium-RC

  24. Amita
    July 7, 2010 at 8:40 am | #33

    Hi,

    I am using selenium RC for java programming language. Can anyone help me how to get the window Id for handling po-up window? I tried the Firebug option. But, didnt work out for me. Actually when i placed the mouse on the pop-up window and tried right click, was unable to do the right click and also Tools option in menu bar, was disabled.

    Thanks,
    Amita

    • epyramid
      July 10, 2010 at 1:29 pm | #34

      The window id will should have been defined in the HTML of the page. If it is not then ask your developers to provide a window id for testability. You can use firebug to inspect the HTML structure of the page at run time.

  25. gajanan
    August 17, 2010 at 7:19 am | #35

    Hi….
    i am facing tough time in handling of Popups and alert msgs…..
    I tried using various commands for the alert msgs….but didnt work
    then finally i tried Robot JAVA class it was working fine…then after couple of days if i am executing the same script, even the robot class is not working, can anyone solve this issue….
    Secondly with the Popups…I dont know any of the properties of the Popup which appear on my application….
    how to handle this kind of Popups….

    thanks,
    Gajanan

  26. Mariyam
    August 17, 2010 at 8:09 am | #36

    Hi..
    I am using selenium RC for java programming language.

    1) I have to use page up and page down which does not get recorded in selenium IDE

    2) I am trying to edit the text in a rich textbox editor which does not get recorded in selenium IDE

    How can I acheive the above

    Thanks,
    Mariyam.

  27. Serge
    August 27, 2010 at 11:28 am | #38

    Hello.
    Could you please tell, how to disable pop-ups in IE? I disabled them manually Tools-Pop-up Blocker-Turn off pop-up blocker. But when selenium launches IE pop-ups blocker is on again and I get exception:
    18:13:40.515 INFO – Command request: getAlert[, ] on session f20077c326414241b8bfb3a323fa504b
    18:13:40.609 INFO – Got result: OK,A Microsoft Dynamics CRM window was unable to open, and may have been blocked by
    a pop-up blocker. Please add this Microsoft Dynamics CRM server to the list of sites your pop-up blocker allows to
    pen new windows:mscrmorguniquename998.t57-crmapp.pem.fcom on session f20077c326414241b8bfb3a323fa504b

    I put this site into the list of allowed sites but this error still persists.
    My code:

    Browser.getBrowser().chooseOkOnNextConfirmation();
    Browser.getBrowser().clickAt( BUTTON_DELETE_LOCATOR, “1,1″ );

  28. eric
    September 16, 2010 at 10:33 pm | #39

    Hi,

    Thank you for your post, but I encounted a even stranger problem. I think you are my last hope.

    So basically, I need to select a location in the popup window, and then click OK to return to the parent window to proceed. I use selenium IDE to record everything I did, and tried to run it from both IDE and Nunit.

    Now it is the weird thing:
    In IDE, the script freezes at clicking the button to popup the location window. The window shows, but the script freezes. No messages indicate what is going on, which is really frustrating. I tried to pause that clicking command and manually execute the next command, which is the select top window, but I got error… HOWEVER, I tried to run the script manually, and it worked!!! I had to alt-tab once to be able to click the next commands in the script from IDE, but that is it.

    In Nunit, the testing case freezes forever, and the command history shows the program freezes at selenium.click(the_location_window_button). I put some text output before and after the clicking line, only the one before outputs the text in the Nunit.

    So how could I solve the problem, and what is exactly this problem? Please help. Thanks!

    Here is the IDE commands:

    clickAndWait
    link=Users

    =====The next command freeze the script/program =====

    clickAndWait
    ctl00_ContentBody_cSearchUser_refLocation_btn

    selectFrame
    relative=top

    clickAndWait
    tvt3

    click
    btnOK

    selectWindow
    null

    clickAndWait
    ctl00_ContentBody_cSearchUser_btnSearch

  29. October 12, 2010 at 2:11 am | #40

    This is funny, we’re not working together anymore and you’re still managing to help me, hehehehe. How are you? Say hi to the Eagles folks for me. ^_^

    • epyramid
      October 12, 2010 at 8:21 pm | #41

      well the world is a very small place. I am good. How about you?? how come you be interested in Selenium and Pop up handling.. :)

  30. SCSVEL
    October 15, 2010 at 10:30 am | #43

    Hi all,

    Thx for your posts…I am using Visual Studio 2010 IDE with C# Code generated by selenium IDE.
    I used below code and not working….

    1) selenium.Click(“css=input[id$='_LookupFooterImageButton']“);
    2) String feedWinId = selenium.GetEval(“{var windowId; for(var x in selenium.browserbot.openedWindows ) {windowId=x;} }”);
    3) selenium.SelectWindow(feedWinId);
    4) selenium.WindowFocus();
    5) selenium.Type(“//form[@id='form1']/table//*[@id='SearchTextBox']“, “p”);

    Here while executing, selenium RemoteRunner executes line 1 which is going to open popup window and after that no response for long time..
    So I closed that opened popup window manually while executing, Immediately lines 2,3 and 4 are executing….
    Line 5 is the command to give input on popup window…

    Can u help me on this? How i can pass input to Popup window….
    I have attached my screenshot for your ref.

    Thx in advance,
    SCSVEL

    • epyramid
      October 15, 2010 at 7:28 pm | #44

      can you confirm using firebug or something like it to make sure that the window Id is correct?? and I do not see any screenshots?

  31. SHANMUGAVEL.C
    October 18, 2010 at 4:27 am | #45

    No..Always it returns the MainScreen window ID as null and i am not getting any window ID for Popup Window as lines after popupwindow opens, are not executing. Once i manully closed popup window, immediately executes..
    So i am not able to get popup window ID…

    Hope u understand my exact problem…

    thx for your reply..

  32. Arshiya Chand
    October 19, 2010 at 4:57 am | #46

    Hi,
    Can you please help me to handle popups in selenium using Testng.I am actually using a PLM tool called Agile where we are using Selenium to automate test scripts.I have used all the ways but nothing seems to be working.Please let me know as this is a very big challenge to me.
    Waiting for your response.

    Thanks,
    Arshiya

  33. Prakash Krishnamoorthy
    October 20, 2010 at 2:25 pm | #47

    What steps will reproduce the problem?
    1. Launch one page and one pop up is also getting launched with that page.
    2. Id or Name for those windows are generated dynamically.

    What is the expected output? What do you see instead?
    Here I want to validate my pop up message, close the pop up and come back to main window and proceed my UI opeartion.

    if record using the Selenium IDE and navigating into pop up and main window is identified with some dynamic id or name(). whereas if i use selenium.getAllWindowNames() or selenium.getAllWindowIds() the array with size 2 and the values are [selenium_main_app_window, selenium_main_app_window] and [undefined, undefined]. Why there is the discrepancy among IDE and RC. Is there any way for me to get those dynamic id like IDE through RC.I want to traverse among my pop up and main window and proceed my ui operation.

    What version of the product are you using? On what operating system?
    selenium 1.0

  1. December 31, 2009 at 5:46 pm | #1
Logged in as vinoygeorge. Logout »
Click here to cancel reply.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: