Search |
|||
John Ferguson Smart's blogParameterized web tests with Maven and SeleniumPosted by johnsmart on November 12, 2009 at 4:07 PM PST
Selenium is a popular web testing framework, well known for the Selenium IDE, which lets you record and replay web tests in the form of HTML files. However, that is not my preferred way of using Selenium. In fact, I much prefer using tools like Selenium for Acceptance Test-Driven Development. The high-level Selenium API is great for writing executable acceptance tests. This approach also works well with easyb, but in this article, for simplicity's sake, we'll just be sticking to plain old JUnit-driven acceptance tests. Suppose we want to implement a simple user login feature. Let's look as a simple acceptance test written using Selenium, based on some code that came out of a recent coding dojo, which verifies that a user can log on to the application by providing a username and password:
Now suppose you want to run this Selenium integration test in your Maven project. Let's go through the steps. The first thing you need to do is to add a dependency on the Selenium Java client libraries:
Next, you need to do is to get your web application starting automatically just before your integration tests. That's pretty easy to do in Maven. A simple approach is to use the Maven Jetty plugin, as shown here:
So now you have your web application starting up before your unit tests. The next step is to ensure that your Selenium unit tests only run during the integration test phase. Well, nothing simpler with the maven-surefire-plugin:
OK, it's a bit verbose, but it's easy enough to understand if you know how the Maven lifecycle plugins work. What I'm doing here is excluding tests in the webtests package from the unit tests, and including them in the integration tests phase. Once you have this working, however, there is a third step. To run these Selenium tests, you need to start a Selenium RC server running on your local machine. The Selenium RC server opens a browser and runs your Selenium tests in the browser. This is easy to do in Maven, using the selenium-maven-plugin:
You can now run your Selenium tests in Maven. Now, when you run "mvn verify", Selenium will start up a browser and run your Selenium integration tests. "All too easy", you might say. "But what if you want to run these tests on a build server? What if there are several web tests running for different projects in parallel? Won't Jetty fail to start if there is another instance of Jetty already running on the same port"? Well, you're dead right - we need to be able to get Jetty to start up on a different port. And we need to be able to provide the port number as a parameter.
That way, you can pass in the Jetty port from the command line.
Oh, but wait - we're not quite done yet. The Jetty port is still hard-coded in the Selenium test class:
Well, that shouldn't be too hard to fix. How about we just read the property from the system properties?
Don't bother trying, it won't work. You see, the unit tests are started in a forked Java process, so they don't have access to the original command line properties passed into Maven. The work-around is to pass the properties explicitly to the tests in the surefire plugin, as shown here:
Now it will work as expected. Also know that Selenium is not your only option for web testing. Doing this with JWebUnit involves essentially the same steps, without the need to set up a Selenium RC server. »
Related Topics >>
Blogs Comments
Comments are listed in date ascending order (oldest first)
|
ArchivesNovember 2009
October 2009 September 2009 August 2009 July 2009 June 2009 May 2009 April 2009 March 2009 February 2009 January 2009 December 2008 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 April 2008 March 2008 February 2008 January 2008 October 2007 July 2007 June 2007 May 2007 April 2007 March 2007 February 2007 November 2006 Recent Entries |
||
|
|