Jul 03
Running Selenium tests from ColdFusion
Posted by James Netherton | Tuesday 03 July 2007 5:00 AM | In Testing,ColdFusion
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 & .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
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:
//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.
4 Comments
[Post comment]
1
Posted by Steve Povilaitis | Wednesday 12 September 2007 8:58 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 2007 2:07 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/...
Maybe there's an alternative method that you could use?
3
Posted by Steve Povilaitis | Thursday 13 September 2007 8:41 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 2007 8:57 PM
Great Stuff. Here is a little different approach using an ant to call selenium test suites. http://tinyurl.com/2k47wt