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 & .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:
<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.
8 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.
7
Posted by alexseinicolaev | Tuesday 17 November 9:31 AM
Hello, dear colleagues. Sorry that it was not on site Testforum.com writing.
Wea’can please where to get the key or breaking a toy [url=http://igry-besplatno.ru/?alawar_action=game&alawar_id=6363]Farm Frenzy 3[/url] & [url=http://vegas-game.ru/mini-games?alawar_action=game&alawar_id=6588]Farm Frenzy 2[/url] though-be?
Kids games very much, but they were not complete and is now asking the keys. All Internet searched, working crack and the key to the game not found!
‘ll Show where to get or buy at a reasonable price. Write please on the forum or on my mail granitss@mail.ru
Sincerely Alexey.
8
Posted by Shannon Holck | Wednesday 18 November 16:27 PM
@Steve, The reason you can’t use the Selenese verify commands is that the Java version “extends SeleneseTestCase”. If you look at the com.thoughtworks.selenium.SeleneseTestCase class (I viewed it by referencing the Java client jar from Eclipse), you’ll see those missing methods. I’m working on a wrapper so that these could be available, but the best approach would be for someone to write a true ColdFusion driver for Selenium. This could save valuable time that CF users have to spend rewriting Java variable assignments and the like. I’m trying the wrapper approach because it will be faster, but if anyone knows a TRUE CF Selenium client driver or has written one, I’d love to know about it. Mike Henke posted something about a CF driver, but turns out these were just instructions on how to use the Java client driver within ColdFusion, really just the same post as here, although I like James explanation much better! I’ll let you all know how my wrapper works out.