Jul 03

Running Selenium tests from ColdFusion

Posted by James Netherton | Tuesday 03 July 2007 0:07 AM | In Testing

As I mentioned in my first Selenium post, it is possible via the Selenium remote control server, to execute Selenium commands from Java, PHP, Ruby &amp .NET.

Since ColdFusion is a Java application we can also leverage this functionality. Here’s a very simple example on how you might go about doing this:

First off, you’ll need the Selenium remote control server. Once downloaded, extract the file contents. Copy selenium-java-client-driver.jar to either WEB-INF/cfusion/lib/ on multiserver mode or /lib on the standalone install. Now restart ColdFusion. Also, if you do not have the Java runtime installed, head over to Java.com to download and install.

I used the Selenium FireFox IDE plugin to create a very simple test that searches for my blog URL in google and then clicks through a couple of links. Finally I assert whether the title of my blog, ‘INSANE THEORY’, is present on the web page.

Notice under the ‘Format’ menu option you have the option to choose which programming language selenium generates code for. By selecting ‘Java’ my test fixture code looks like this:

package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class NewTest extends SeleneseTestCase {

	public void testNew() throws Exception {

		selenium.open("/");

		selenium.type("q", "jamesnetherton.com");

		selenium.click("btnG");

		selenium.waitForPageToLoad("30000");

		//Click my Jetty in CF8 post, link

		selenium.click("link=exact:Jetty in ColdFusion 8?");

		selenium.waitForPageToLoad("30000");

		//Now click on the CF blog category
		selenium.click("link=ColdFusion (22)");
		selenium.waitForPageToLoad("30000");

		//Make sure my blog title has been output onto the page
		assertEquals("INSANE THEORY", selenium.getText("//div[3]/h1/a"));

	}
}

Not much help to us ColdFusion developers huh? In fact, all of the above can be easily translated to work in ColdFusion:


<cfscript>

	//This should now be loaded via the CF classpath

	selenium = createobject("java","com.thoughtworks.selenium.DefaultSelenium").init("localhost", 4444, "*firefox", "http://www.google.co.uk");

	selenium.start();

	selenium.open("/");

	selenium.type("q", "jamesnetherton.com");

	selenium.click("btnG");

	selenium.waitForPageToLoad("30000");

	selenium.click("link=exact:Jetty in ColdFusion 8?");

	selenium.waitForPageToLoad("30000");

	selenium.click("link=ColdFusion (23)");

	selenium.waitForPageToLoad("30000");

	assertEquals("INSANE THEORY", selenium.getText("//div[3]/h1/a"));

	selenium.stop();

</cfscript>

Before you run the above code you’ll need to have started the Selenium server. From the command line browse to the directory where selenium-server.jar is located and enter:

java -jar selenium-server.jar

The Selenium server should now start and run on port 4444. Now you can test out the ColdFusion script above. What should happen is that FireFox will be started and each of the steps within the test script will be executed. If any errors occur, such as my test to make sure the blog title is printed, an exception will be thrown.

Overall this is quite powerful because it allows tests to be dynamic in nature. For example, perhaps you are testing a holiday booking system and you need to enter a specific range of dates. You could use your host programming language to handle this logic. It is also useful because you could include these tests within unit testing frameworks such as CFUnit, CFCUnit, JUnit and PHPUnit. You’ll then be able to easily execute your unit and functional tests using the same testing framework.