• No results found

HttpUnit Laboratorio di Sistemi Software - A.A. 2003/2004

N/A
N/A
Protected

Academic year: 2021

Share "HttpUnit Laboratorio di Sistemi Software - A.A. 2003/2004"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Laboratorio di Sistemi Software - A.A. 2003/2004

Introduction

HttpUnit, available from http://www.httpunit.org, is an open source Java library for programmatically interacting with HTTP servers.

HttpUnit provides an API for parsing HTML, submitting forms, following hyperlinks, setting cookies, and performing many other tasks normally associated with web browsers.

(3)

Installing HttpUnit

Add httpunit.jar and Tidy.jar, both included with HttpUnit, to your classpath. Also ensure that an XML parser is installed.

httpunit.jar contains the class files for the HttpUnit application Tidy.jar is an open source tool for checking the syntax of

(4)

Laboratorio di Sistemi Software - A.A. 2003/2004

“Agenda”

The recipes are presented in order as portions of a simple web application developed using a test-first approach.

If the web application were complete, it would allow users to subscribe and unsubscribe from a newsletter. It would also allow administrators to view the complete list of newsletter subscribers, but only after providing a username and

(5)

Preparing for Test-First Development

Problem:

You want to configure your development environment to support test-first development with HttpUnit, JUnit, Tomcat, and Ant.

Solution:

Create an Ant buildfile to automatically build and deploy your web application. The buildfile allows you to quickly redeploy and test after each change to your code.

(6)

Laboratorio di Sistemi Software - A.A. 2003/2004

Configuring Tomcat

(7)

Configuring Ant

In order to test using Ant's junit task, you should copy

servlet.jar, httpunit.jar, junit.jar, and Tidy.jar to Ant's lib

directory. This makes it easy to ensure that all of the required JAR files are loaded using the same Java ClassLoader when you are running your tests.

(8)

Laboratorio di Sistemi Software - A.A. 2003/2004

Classpath e compile targets

Define a classpath:

(9)

Web Application aRchive target

index.html \--- WEB-INF | web.xml |--- classes |--- lib index.html \--- WEB-INF | web.xml |--- classes |--- lib

(10)

Laboratorio di Sistemi Software - A.A. 2003/2004

Tomcat deploy

Use Tomcat’s manager application:

http://localhost:8080/manager/install?path=/news&war=jar:file:/path/to/news.war!/ Or create a deploy target:

(11)

Tomcat undeploy

Use Tomcat’s manager application: http://localhost:8080/manager/remove?path=/news Or create an undeploy target:

(12)

Laboratorio di Sistemi Software - A.A. 2003/2004

JUnit target

(13)
(14)

Laboratorio di Sistemi Software - A.A. 2003/2004

(15)

Following Hyperlinks (2)

Run the faulty test first, then write the servlet to generate a minimal subscribtion page (TDD

(16)

Laboratorio di Sistemi Software - A.A. 2003/2004

Writing testable HTML

Problem:

You want to use HttpUnit to test your web application, but it can't parse your HTML.

Solution:

Write well-formed HTML, ensuring the tags are properly nested and attributes are quoted. For best results, write XHTML.

If you are encountering problems, call this method to turn on HTML Tidy warnings:

This causes HTML Tidy to print warning messages to the console, letting you know which lines of your HTML pages are incorrect.

(17)
(18)

Laboratorio di Sistemi Software - A.A. 2003/2004

Testing HTML tables (2)

(19)

Testing HTML tables (3)

(20)

Laboratorio di Sistemi Software - A.A. 2003/2004

Testing HTML tables (4)

(21)

Extending the servlet behavior

(22)

Laboratorio di Sistemi Software - A.A. 2003/2004

(23)
(24)

Laboratorio di Sistemi Software - A.A. 2003/2004

Refactoring the Servlet

(25)
(26)

Laboratorio di Sistemi Software - A.A. 2003/2004

Testing Elements on HTML Forms (1)

Check existence of buttons:

(27)

Testing Elements on HTML Forms (2)

Check existence of input fields:

(28)

Laboratorio di Sistemi Software - A.A. 2003/2004

(29)

Newsletter Subscription page

Note:

the HttpUnit tests are not verifying every aspect of page layout.

(30)

Laboratorio di Sistemi Software - A.A. 2003/2004

(31)
(32)

Laboratorio di Sistemi Software - A.A. 2003/2004

(33)
(34)

Laboratorio di Sistemi Software - A.A. 2003/2004

Testing through a firewall

Set the proxySet, proxyHost, and proxyPort system properties.

¾ HttpUnit uses java.net.HttpURLConnection, which checks the proxySet system property to determine if it should operate through a proxy server.

(35)

Testing cookies

Cookies are little pieces of information that web applications store on the client browser's machine.

¾ Cookies allow web sites to maintain state information as you view different web pages.

Use WebConversation's addCookie( ) method to create new cookies, and its getCookieValue( ) method to retrieve cookie values.

(36)

Laboratorio di Sistemi Software - A.A. 2003/2004

(37)
(38)

Laboratorio di Sistemi Software - A.A. 2003/2004

Testing secure pages

You want to test a page that requires a username and password for login.

Simulate HTTP BASIC authentication using WebConversation's setAuthorization( ) method.

(39)

Bibliography

Eric M. Burke & Brian M. Coyner

“Java Extreme Programming Cookbook” O’Reilly

References

Related documents

Students from all different cultural and linguistic backgrounds and experiences learn from one another and begin to understand that learning is an active process in which one

where n component databases (each abiding internally to their own 3-level architecture) are integrated (via CF and the Mediator), resulting in the database schema of DBINT

The terms of this Lease shall continue to apply to Lessee's continued possession, provided that (i) if Lessee produces and sells oil or gas from the Leased Premises, Lessee shall

Ses caractéristiques le recommandent également pour le bas et salut les systèmes sains de fréquence avec des exécutions et des résultats

Choose a one-year contract with a 5% monthly fee discount or a two-year contract with a 10% discount for your tariff package, with security included: for every business

Conclusions and Implications for Research and Practice: The comparisons between transformational and instructional leadership and between the five leadership dimen- sions suggested

Pijanac ga nije razumio. Slabašno se nasmiješio. Za uzvrat očekujem samo čašicu razgovora. Katkad se zaželim razgovora. Pijančev je osmijeh, sad kad se stvar razjasnila,

We are, thus, motivated to observed the land subsidence using gravity method in Jakarta (2008-2010) and Bandung (2009-2011) with aim to understand the phenomenon of