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.

6 Comments

[Post comment]


1

Posted by Steve Povilaitis | Wednesday 12 September 20:09 PM

Great post!

I’m able to get everything to work except for the selenium.verifyTrue() method.

I get the following error:

Error: coldfusion.runtime.java.MethodSelectionException: The verifyTrue method was not found.

any ideas?

thanks,

Steve Povilaitis


2

Posted by James Netherton | Thursday 13 September 2:09 AM

Hi Steve,

ColdFusion is correctly throwing the exception as the VerifyTrue method does not exist on the DefaultSelenium class.

Take a look at the javadocs here:

http://release.openqa.org/selenium-remote-control/0.9.0/doc/java/com/thoughtworks/selenium/DefaultSelenium.html

Maybe there’s an alternative method that you could use?


3

Posted by Steve Povilaitis | Thursday 13 September 8:09 AM

Thanks James. That makes sense. I guess what I could do is use the CFCUnit assertions on the selenium methods - since I have the selenium code embedded as CFScript inside a CFCUnit TestSuite already. I’ll let you know how it works.

My goal is to have a completely automated Unit and User Level testing framework implented within CFEclipse. So far I have CFCUnit and CFUnit working from within an Ant script in CFeclipse. I have the Ant script configured as an automatic builder that runs the test suite anytime one of my .cfc files is modified. Selenium is the last missing link!

thanks again for your help!


4

Posted by Mike Henke | Monday 17 September 20:09 PM

Great Stuff. Here is a little different approach using an ant to call selenium test suites. http://tinyurl.com/2k47wt


5

Posted by Chris | Wednesday 11 February 15:32 PM

Great post. However, do you know anyway to automatically start the selenium-server from Coldfusion, so everything can be completely automated?


6

Posted by James Netherton | Wednesday 11 February 20:40 PM

Hi Chris,

Looks like the server can be started programatically. You’ll need to add selenium-server.jar to the CF classpath. You’ll then be able to execute the following code to start the server:

seleniumServer = createObject(”java”, “org.openqa.selenium.server.SeleniumServer”).init();

seleniumServer.start();

The SeleniumServer class also a stop() method to shut down the server. Full details here.

Hope that helps.


Leave a comment