Oct 03

Selenium Wiki at thecrumb.com

Posted by James Netherton | Wednesday 03 October 2007 10:55 PM | In Testing

Jim Priest has set up a Selenium area on his Wiki. Hopefully as more people start adopting Selenium there will be plenty more resources added to the list.

Blog entries from yours truly are mentioned, thanks Jim!

Check it out...

Selenium Wiki

 

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 /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.

 

Jul 02

Creating a Selenium test suite

Posted by James Netherton | Monday 02 July 2007 5:00 AM | In Testing

Most testing frameworks have the concept of grouping a set of text fixtures into a 'suite' so that you can execute a number of related tests together.

Creating test suites with Selenium doesn't appear to be obvious at first. Here's a simple guide on how to create and execute suites of functional tests:

You can run test suites via the Selenium web browser application. You'll need to download the Selenium Core in order to do this. After you have extracted the downloaded file, copy the 'core' directory to somewhere that is accessible under your server web root (E.g the document root). Browse to http://your server/path to selenium core/TestRunner.html and you should see the Selenium test runner application.

In order to create a test suite, all you have to do is create an HTML document that is composed of a table of links to your individual unit tests. For example:

<html>
<head>
<title>My Application Test Suite</title>
</head>
<body>

<table>
<tr><td><b>Suite Of Tests</b></td></tr>
<tr><td><a href="./TestLogin.html">Test Login</a></td></tr>
<tr><td><a href="./TestFormEntry.html">Test Form Entry</a></td></tr>
<tr><td><a href="./TestFormSave.html">Test Form Save</a></td></tr>
</table>

</body>
</html>

Obviously you'll need to adjust the href's to wherever you have your test cases stored, but essentially that is all there is to creating a test suite.

Now return to the Selenium test runner application and alter the unit test path so that it finds the HTML file containing your test suite definition. When the page reloads you should see all of your individual unit tests listed within the frame on the left hand side. You now have the option of executing all tests within the suite or run selected tests.

The test runner reports results in the usual manner, stating the number of passes, failures or number of cases where errors were encountered within the actual test scripts.

All of this is quite handy because as I mentioned in my previous post, you could run a suite of tests as part of a continuous integration process upon application deployment or commits to a code repository.

 

Jul 01

Functional testing with Selenium

Posted by James Netherton | Sunday 01 July 2007 11:53 AM | In Testing

If you're taking an agile approach to software development, it's highly likely you'll be using test first development practices in order to test the programming logic that has been written.

Unit testing is great for exercising the various components under the bonnet of the application but it doesn't really help in terms of testing the interface that an end user will encounter. Functional testing is important, as it allows you to verify that the interface to your application is behaving as expected and could potentially trap any errors that have not been caught by unit testing.

selenium is a tool for testing web applications from the Browser or via a FireFox plugin.

The Selenium IDE allows you to record test scripts which can automate the testing of a web application interface. For example, I could record the actions of logging into an application and then then clicking through to some feature and perhaps entering and saving some data. I could then assert that the output from those actions meets my expectations, very similar to how unit testing frameworks work.

The great thing about all of this is that it removes most of the tedium in testing your application when you are developing and debugging. Most importantly, you have a set of repeatable and consistent tests that can be performed at the click of a button. Better yet, Selenium also has a remote control server which lets you execute tests from API's covering Java, Ruby, PHP, .NET and Perl. The advantage of this approach is you write your functional tests in your preferred programming language and maybe execute them as part of a continuous integration process.

So what are you waiting for? Go try it out!