Java Reference
In-Depth Information
HTTP libraries suitable for unit testing include HtmlUnit, HttpUnit, and Apache
Commons HttpClient. We examine HttpClient in action in the section “Testing ser-
vices with HttpClient.”
In this chapter, we test server services for Ajax applications , which we distinguish from
testing web services , a different web standard.
We now examine each technique in more detail with implementation examples.
We start with functional testing with Selenium and HtmlUnit.
13.3
Functional testing
We now look at testing the application stack by continuing our demonstration of Sele-
nium and HtmlUnit started in the previous chapter on presentation layer testing. We
show you how to use Selenium and HtmlUnit to write the same kind of tests. We show
how to deal with the asynchronous aspect of an Ajax application before going on to
test specific components that make up the stack, such as JavaScript libraries and
server-provided services (as opposed to web services). The key aspect of writing tests
for an Ajax application is to know when an action causes a change in the DOM . We
show how to do this and create functional tests using Selenium and then HtmlUnit.
13.3.1
Functional testing with Selenium
If you're not familiar with Selenium, please consult the previous chapter to get an
understanding of the fundamentals we build on here. We pick up Selenium where we
left off, this time testing Ajax, a more advanced task.
Once a page is loaded in the browser, Selenium sees that version of the page. To
see the updated DOM , you must use a different API than what we showed you in the
previous chapter.
Let's take as an example the form in figure 13.3. When you click the Get Message
button, the page queries the server and returns a simple text message: Hello World.
JavaScript code then updates the DOM with the message, and the browser displays the
input field value, as you can see in the screenshot.
Listing 13.1 shows the HTML source for the page, which includes the JavaScript to
perform the HTTP XML Request.
Figure 13.3
A simple Ajax form
 
 
 
 
 
 
 
Search WWH ::




Custom Search