Copy
Wakaleo Consulting
Java Web Testing Frameworks

Introduction

This newsletter contains news and updates about Java development tools, and in particular topics related to the Java Power Tools Bootcamp course.  

In this issue of the Java Power Tools newsletter I would like to talk about web testing frameworks. Automated web tests are a great way to automate smoke and regression tests, and can also be effectively used in a TDD or BDD approach to designing your overall screen behaviour and screen flow. In this issue, we will compare several web testing frameworks that can be used with Java: Selenium, HTMLUnit,  WebDriver and JWebUnit.

Selenium - the real McCoy
Selenium is probably among the most well-known web testing frameworks. Selenium works by starting up and controlling a web browser. So it can reproduce the behaviour of a browser more accurately than any other of the testing frameworks we discuss here. This can be good for complex Javascript and AJAX testing, though in practice complex dynamic screens based on Javascript are always hard to test automatically. 

Selenium uses a high-level API that is easy to read and makes it easier to model page behaviour when using a TDD-style approach. A typical Selenium test, written in Java, looks like this:

@Test

public void testDepositCash() throws Exception {

  

    Selenium selenium
      = new DefaultSelenium("localhost", 4444,

                    "*firefox",
                    "http://localhost:9090");

    selenium.start();

    selenium.open("/ebank");

    selenium.waitForPageToLoad("10000");

    selenium.type("depositAmount", "100");

    selenium.click("deposit");

    selenium.waitForPageToLoad("10000");

    selenium.isTextPresent("Balance: $100");     

}

 

Selenium is mature and powerful, and has features such as record-and-replay of test scripts, and an integrated IDE build on top of Firefox. On the down side, Selenium requires more infrastructure behind the scenes, and is typically several times slower than the in-memory solutions that we will look at next.

HTMLUnit - fast but low level 

Next on our list is HTMLUnit. HTMLUnit works by simulating a browser in an embedded process - it is therefore much faster than Selenium. However, HTMLUnit needs to simulate Javascript behaviour, which can make it less reliable when it comes to Javascript and AJAX-reliant pages. On the other hand, it is often sufficient when you are designing your basic page design and screen-flow. 

One disadvantage that HTMLUnit does have is that it relies on a fairly low-level - you need a good grasp of the structure of the HTML page, which can make the tests fragile and does not facilitate a behaviour-driven development approach at all . A typical test looks like this:


@Test

public void depositCash() throws Exception {

    WebClient webClient = new WebClient();

    HtmlPage page
      = webClient
        .getPage("http://localhost:9090/ebank");

    assert page.asText().contains("Balance: $0");

    HtmlForm form = page.getForms().get(0);

    HtmlSubmitInput depositButton
        = form.getInputByName("deposit");

    HtmlTextInput textField
        = form.getInputByName("depositAmount");

 

    // Change the value of the text field

    textField.setValueAttribute("100");

 

    // Now submit the form by clicking the button 
    // and get back the second page.

    HtmlPage result = depositButton.click();

    assert result.asText()
                 .contains("Balance: $100");

}
 
JWebDriver - promising but a bit green
The third tool on our list is WebDriver. This is a fairly recent tool from the Selenium team, which is designed to be an abstraction layer enabling you to build tests that can run on both HTMLUnit and Selenium. The API is (intentionally) low level: it is not particularly richm and is a little odd in some ways, and in my opinion is not really abstract enough to write TDD-style web tests comfortably. In fact that is not it's intention - it is intended to be a low-level layer on which other tools are built. Here is a sample:

@Test

public void testDepositCash() {

    driver = new HtmlUnitDriver();

    driver.get("http://localhost:9090/ebank");

    WebElement element
     = driver.findElement(
              By.name("depositAmount"));
    element.sendKeys("100");
    element.submit();
    assertThat(driver.getPageSource(),
               containsString("Balance: $100"));

}
 

WebDriver is still pretty immature, and many simple test cases just don't work yet. The Selenium driver is also still work in progress. So I wouldn't say this one is production-ready just yet.

JWebUnit - a good compromise 
JWebUnit tries to give you the best of both worlds - a high-level API similar to the one in Selenium, combined with an embedded broswer simulation for fast testing. It is actually built on top of HTMLUnit, but has a much friendlier, higher level API. (It also has the ambition of letting you run both HTMLUnit and Selenium tests with the same test cases, but only the HTMLUnit moter exists as of yet).

A typical test case looks like this:

public class TestDepositCash extends WebTestCase {

    public void testDepositShouldAddToBalance() {
        setBaseUrl("http://localhost:9090/ebank");

        beginAt("/");

        assertTextPresent("Balance: $0");

        setTextField("depositAmount","100");

        clickButtonWithText("deposit");

        assertTextPresent("Balance: $100");

    }
}
As can be seen here, the test commands are simple and abstract enough to let you design your pages and screen flows without overly committing to the detailed page structure, and the tests run very quickly. 


If you want to learn more about automated web testing, TDD, BDD and the like, check out the Java Power Tools Bootcamps - we cover many aspects of automated web testing, including Selenium, HTMLUnit and JWebunit in the context of Test-Driven and Behaviour-Driven Design. Check it out today!  

 

Upcoming Bootcamps
There are still places available on the following Java Power Tools Bootcamps - book early to avoid disappointment!

Canberra - 7-11 September
Brisbane - 5-9 October
Sydney - 19-23 October

Wellington - 16-20 November
Unsubscribe <<Email Address>> from this list.

Copyright (C) 2009 Wakaleo Consulting All rights reserved.

Forward this email to a friend
Update your profile
Email Marketing Powered by Mailchimp