How do you test your web applications? In the previous Java Power Tools Newsletter, we looked at different levels of web testing from a technical viewpoint, and described tools that could be used for each level. This time we will be looking at how automated web testing fits into the larger picture. In particular, we will look at how you can use Selenium in different ways for different types of testing. Selenium is a popular and widely-used web testing framework. Unlike other web testing tools in the Java world, it actually opens a web browser and uses the web browser to exercise the web application. This is both it's main strength and it's main weakness. It enables a rich scope of testing, including relatively accurate testing of AJAX-based applications. But it is much slower than other tools, and requires more testing infrastructure and setup. Using Selenium for Test-Driven Development In a nutshell, Test-Driven Development involves designing your code by writing tests that verify the behaviour of your application. This approach is sometimes referred to as "executable specifications". This is easy enough when you are working with pure Java code, but it gets a bit trickier at the web layer. Now Selenium has a rich and flexible API that you can use to write your tests using a TDD or BDD approach. Selenium RC lets you write tests directly in Java (or in a number of other languages, such as Groovy or Ruby), and execute them as normal JUnit tests. A simple Selenium test case, written in Java, might look like this: public class TestLoginStory extends SeleneseTestCase { public void setUp() throws Exception { setUp("http://localhost:8080/tweeter-web/", "*firefox"); } public void testUserShouldLogInByEnteringUsername() throws Exception { selenium.open("home"); selenium.type("username", "scott"); selenium.type("password", "tiger"); selenium.click("signin"); selenium.waitForPageToLoad("5000"); assertTrue(selenium.isTextPresent("Hi scott!")); } } Now these tests are pretty high-level. They step through the application, and check the flow and general behaviour. They certainly don't try to test every possible path through the application - that is not the aim. The aim of these tests is to guide the development process at the subsequent levels, and to act as regression tests once the code has been implemented. Using Selenium for Regression Testing If you haven't been doing web tests from the start of your project, don't worry - all is not lost! Selenium is also a great tool for regression testing. Web-based regression testing is probably the most effective way to introduce testing into an existing project. In general, retroactive end-to-end tests are more efficient and more accurate than writing unit tests after the fact, though writing unit tests (and using TDD) is an excellent strategy when fixing bugs or adding new features. If you have an existing application, a useful starting point is to use Selenium IDE. Selenium IDE is a Firefox plugin that enables you to record and replay Selenium scripts in Selenese, which is essentially a series of Selenium commands encoded in an HTML table. You simply step through your application, and Selenium will record your actions in Selenese. At various points you can insert assertions to check that the application is doing what you expect. You can also run the Selenese test scripts using Ant or Maven in order to automate them. While this is a useful trick, I still find it convenient to convert these scripts into Java. These raw recorded selenese scripts are usually not robust enough for production use, and need to be considerably fine-tuned. The values typed into fields is hard-coded, for example, and the XPath expressions tend to be brittle. You will also certainly need to insert pauses between the steps to ensure that the next page has time to load, which Selenium IDE does not do for you.
Fortunately, the Selenium IDE lets you save Selenium scripts in a number of programming languages. Once the tests are available in Java or Groovy (for example), it is easy to make them more robust, and introduce more sophisticated tests involving parameters, loops, conditions and data-driven tests. For example, you can use JUnit 4 parameterized tests to exercise your web application with different sets of test data. These Java tests are also easier to integrate into your automated build process.
Another useful trick is to run your Selenium regression tests in combination with a test coverage tool such as Cobertura or Clover. While test coverage data on this sort of regression tests is only a guideline (the aim is not to achieve 100% test coverage with this sort of test), it can help you identify areas of functionality that your regression tests are missing. Scaling your tests The main problem with Selenium tests are that they tend to be slow. Opening and manipulating a browser is much slower than simply sending HTTP across the network, as tools like JWebUnit do. The Selenium Grid is one possible solution to this - with the Selenium Grid, you can distribute your Selenium tests across multiple machines, which can speed up your tests considerably. If your Selenium tests are slowing your process down, you probably should take a look. Training news 2010 is going to be an exciting year on the training front. In the first half of the year, we will be running the very popular Testing and TDD for Java Developers course in Wellington, Sydney, Canberra and Melbourne. Whether you are new to Java testing practices, or already have some experience in the developer testing and TDD/BDD, this material can really help you take . It's a fun, practical workshop packed with labs, where you will using TDD/BDD and a host of testing tools on a real application, to develop real features, rather than the overly-simplified TDD examples you often see. We will also be running our comprehensive Java Power Tools Bootcamps, in Wellington, Sydney and Canberra, with other sites to be announced soon! This is a full-on course where every developer is guaranteed to learn not just one new technique, but a range of tips, tricks and tools that you can use across the board to speed up your development and set up a kick-ass development infrastructure. We are also very excited to announce that we will be running the new virtual-classroom Maven training courses in sessions suitable for Australia, New Zealand and Asia. Over the past six months, the new Online Maven Courses have proved to be tremendously popular. It's a great way to get your team up to speed with Maven. And starting on the 27th of January, we will be running these courses in collaboration with Sonatype at a time better suited to Australia, New Zealand, and Asia. The MVN-101 class is ideal for programmers who work with Maven projects and need to understand how to work with an existing Maven build. It is also appropriate for Maven users who are interested in Maven fundamentals. It comprehensively covers Maven installation and configuration, explains the motivation behind Maven and gives an overview of related development tools. We will be running the first MVN-101 course on January 27 and 29, 2010 from 10:00 am to 2:00 pm Tokyo Time. That's 12pm-4pm in Sydney, 2pm-6pm in Auckland, and 9am-1pm in Singapore. The MVN-201 course follows on from MVN-101 course and covers more advanced build automation topics. This course is ideal for Development Infrastructure Engineers who are responsible for maintaining enterprise development infrastructure, or anyone wanting a more complete understanding of Maven. We will be running the first MVN-201 course on February 3 and 5, 2010 from 10:00 am to 2:00 pm Tokyo Time. Again, that's 12pm-4pm in Sydney, 2pm-6pm in Auckland, and 9am-1pm in Singapore.
|