Selenium Remote Control For Java — A Tutorial (part 3)

Let’s take a look at working with popup windows and Selenium Remote Control.

The simplest way to work with popups is to do whatever creates the popup (in this case, clicking a link) and then use the getAllWindowNames() and selectWindow(String windowId) methods. The following method (placed in our test class created in part 2) demonstrates creating and identifying popups:

(Note that the example code refers to a real page.)

 public void testPopUpCreation()
      throws Exception
   {
      selenium.open(TEST_PAGE_URL);
      String[] windowNames = selenium.getAllWindowNames();

      assertEquals(1, windowNames.length);
      assertEquals("myiframe", windowNames[0]);

      selenium.click("link=This link opens a popup");
      selenium.waitForPopUp("popup", MAX_WAIT_TIME_IN_MS);
      windowNames = selenium.getAllWindowNames();

      assertEquals(2, windowNames.length);
      assertTrue(Arrays.asList(windowNames).contains("myiframe"));
      assertTrue(Arrays.asList(windowNames).contains("popup"));
   }

So, we find all the windows Selenium knows about. In this case we end up with two windows: the main window, “myiframe” and the popup, “popup”.

Let’s use this information to actually interact with the popup.

public void testWindowSelection()
      throws Exception
   {
      selenium.open(TEST_PAGE_URL);
      assertEquals(TEST_PAGE_TITLE, selenium.getTitle());

      selenium.click("link=This link opens a popup");
      selenium.waitForPopUp("popup", MAX_WAIT_TIME_IN_MS);
      selenium.selectWindow("popup");

      assertEquals("Bit Motif", selenium.getTitle());
   }

We create the popup, and then use selectWindow to let Selenium know this is the window we are working with. In this case, we just get the title of the page. But, at this point, we could do all the things we’d normally do.

After we’ve finished with the popup, we probably want to close it, and go back to originating window. Doing this is, well, a little ugly. But, it works. Here’s the code:

   public void testWindowSelection_ClosePopUp_ReturnToMainWindow()
      throws Exception
   {
      selenium.open(TEST_PAGE_URL);
      assertEquals(TEST_PAGE_TITLE, selenium.getTitle());

      selenium.click("link=This link opens a popup");
      selenium.waitForPopUp("popup", MAX_WAIT_TIME_IN_MS);
      selenium.selectWindow("popup");

      assertEquals("Bit Motif", selenium.getTitle());

      // close popup
      selenium.close();

      // select original window
      selenium.selectWindow(null);

      assertEquals(TEST_PAGE_TITLE, selenium.getTitle());
   }

Most of the above we’ve seen before. We open the popup, point Selenium to it, and check something. What’s new is the close method. It does what you’d expect a method named close to do.

After the popup is closed, we have to tell Selenium to look at another window. (If we don’t, when we try to do something, we get an exception.) In our case, we want to work work with the “main” window. To do this, we have to pass null to selectWindow.

It’s a bit counterintuitive. You might think that it would be “myiframe” or something like that, but no. Once we have the main window, we can process as usual.

One final note on working with popups. Prior to Selenium Remote Control verison 0.9.1, I couldn’t get popups working with *iehta browser launcher. So, make certain you have a recent version of Selenium Remote Control. The jars used in this example can be found here.

Get poppin!

2 Responses to “ Selenium Remote Control For Java — A Tutorial (part 3) ”

  1. Tom Says:

    Thanks for the tip on setting window to null

  2. amol Says:

    Hi,

    I am working with pop ups and my flow is

    MainWindow –> Popup —>RETURN VAL–>MainWindow

    Now I when my popup tried to return a value to the main window it gives the following error

    [error] isNewPageLoaded found an old pageLoadError
    [error] Current window or frame is closed!

    The popup has a button “Update” and it has a onClick=”submit()” function.

    I tried placing pause at various intervals but it still fails.

    You have any idea about this?

Leave a Reply