Selecting Popup Windows in Selenium

This example shows how to start a Selenium Server and run a Selenium client in Java. The example opens up a popup window by clicking on the link in index.html. It then selects the popup window by first using the “name=popupWindowName” option for selectWindow. It then grabs the original window (using “name=null”). And, finally, it grabs the popup again by using the global javascript variable option in selectWindow (“var=popupWindowVar”).

In order to run this test, you will need the following three jar files on your classpath.

  • junit-4.8.2.jar
  • selenium-java-2.17.0.jar
  • selenium-server-standalone-2.17.0.jar

Unit Test

package org.example;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;

import com.thoughtworks.selenium.DefaultSelenium;

public class SeleniumIntegrationTest {
	private static SeleniumServer server = null;
	private DefaultSelenium selenium = null;

	public static void oneTimeSetUp() throws Exception {
		// Create a configuration to override defaults.
		RemoteControlConfiguration rcc = new RemoteControlConfiguration();
		rcc.setSingleWindow(true); // Support Popups
		rcc.setTrustAllSSLCertificates(true); // Trust SSL

		// Start the Selenium Server.
		server = new SeleniumServer(false, rcc);

	public static void oneTimeTearDown() throws Exception  {

	public void setUp() throws Exception {
//		// Create a Selenium thread.
//		// Open in Chrome
//		selenium = new DefaultSelenium("localhost", 4444,
//				"*googlechrome",
//				"http://localhost:8080/GrailsDefault/");
//		// Open in Safari
//		selenium = new DefaultSelenium("localhost", 4444,
//				"*safari C:\\Program Files (x86)\\Safari\\Safari.exe",
//				"http://localhost:8080/GrailsDefault/");
//		// Open in IE
//		selenium = new DefaultSelenium("localhost", 4444,
//				"*iexplore",
//				"http://localhost:8080/GrailsDefault/");
		// Open in Firefox
		selenium = new DefaultSelenium("localhost", 4444,

		// Start the server

	public void tearDown() {
		// Stop the Selenium thread.

	public void popupWindowExample() {
		// Open the base window."index.html");
		assertEquals(selenium.getTitle(), "Popup Window Example");

		// Click button to popup new window."id=popupButton");
		// Select the popup window.
		// popupWindowID is the ID given in the javascript.
		selenium.waitForPopUp("popupWindowID", "30000");
		assertEquals(selenium.getTitle(), "Popped Up Window");

		// Click a link to google on the popup."link=This link goes to google");
		assertEquals(selenium.getTitle(), "Google");

		// Select the original window.
		assertEquals(selenium.getTitle(), "Popup Window Example");

		// Select the window by a javascript variable.
		assertEquals(selenium.getTitle(), "Google");



<title>Popup Window Example</title>

function popupNewWindow() {
	window.popupWindowVar ="popup.html", "popupWindowID");
function popupNewWindow2() {
	window.popupWindowVar2 ="popup.html", "popupWindowID2");


	<div id="header">
		<button id="popupButton" onclick="popupNewWindow()">Popup Window</button>
		<button id="popupButton2" onclick="popupNewWindow2()">Popup Window 2</button>


<title>Popped Up Window</title>

	<div id="header">
		This is a popup window.
	<div id="links">
		<a href="">This link goes to google</a>.

I found a bug in the variation of selectWindow that tries to grab the window from a JavaScript variable (i.e “var=foo”). It works in Chrome and IE. However, I could not get it working with Firefox 9.0.1 with Windows 7. The error is below. I’ve opened up a defect. The bug is listed here:

com.thoughtworks.selenium.SeleniumException: ERROR: Window does not exist. If this looks like a Selenium bug, make sure to read for potential workarounds.
        at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(
        at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(
        at com.thoughtworks.selenium.DefaultSelenium.selectWindow(
        at org.example.SeleniumIntegrationTest.popupWindowExample(
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Grails 2.0.0 lib directory fix

I recently had a problem with Grails when I started a new project in Eclipse with Grails 2.0.0. I tried adding an external JAR file to my “lib” directory by dragging the JAR file over to the directory.

In Grails 1.3.7, the JAR file would then get added to the list of grails dependences (after refreshing the dependencies). However, that stopped working. After reading some user group posts, here’s what I did to fix it.

1) run “grails clean”
2) drag the jar file to your “lib” directory.
3) right-click on the project and choose Groovy Tools -> Refresh Dependencies

How to get JAX-WS Web Service Method Name

I’m not sure if this will always work, but here is some sample code that I put into a Handler to log the web service method name that is being called. I wanted to be able to track each method, and see which ones were getting use.

This web service was running on IBM WebSphere 6.1.

public boolean handleMessage(LogicalMessageContext context) {
	String webMethodOperationName = null;
	try {
		Field mepCtxField = context.getMessage().getClass().getDeclaredField("mepCtx");
		Object mepCtx = mepCtxField.get(context.getMessage());
		Field requestMCField = mepCtx.getClass().getDeclaredField("requestMC");
		Object requestMC = requestMCField.get(mepCtx);
		Field operationDescField = requestMC.getClass().getDeclaredField("operationDesc");
		Object operationDesc = operationDescField.get(requestMC);
		Field webMethodOperationNameField = operationDesc.getClass().getDeclaredField("webMethodOperationName");
		webMethodOperationName = (String) webMethodOperationNameField.get(operationDesc);
	catch (Exception e) {
		log.warn("Cannot find method name of operation.");
	return true;