Problems Getting Started
- Why can't I unzip the download library?
- What is the org.xml.sax package?
- I don't understand how the ... API works.
Server Issues
- Is https supported?
- Can I use HttpUnit through a proxy server?
- How can I handle certificates from a test machine?
- Why isn't HttpUnit handling my non-English pages?
- How does HttpUnit handle request timeouts?
- Why isn't HttpUnit seeing cookies from my site?
Form Handling Problems
- How do I uncheck a checkbox?
- Why can't I change hidden parameter values?
- How do I get the response when I click a button?
Links and Images
- How do I find a link for a given image?
JavaScript support
- How do I use HttpUnit to test my pages that use JavaScript?
- JavaScript is not being executed at all. Why not?
- How do I turn off scripting for certain tests?
- My JavaScript works in a browser, but HttpUnit says "ReferenceError: ... is not defined"
- How do I test JavaScript alert boxes?
- How do I test JavaScript popup dialogs?
- How do I handle a page that uses JavaScript features that HttpUnit does not support?
- I called document.write() in my page, but getText() does not show the results! Why not?
ServletUnit usage
- How can I see the servlet session between calls?
- Can I use JSPs with ServletUnit?
Problems Getting Started
Why can't I unzip the download library?
HttpUnit is archived using the classes in the java.util.zip package.
Some older Unzip programs cannot understand this format.
If you are using WinZip, stick to version 7.0 or later.
What is the org.xml.sax package?
You may be getting compile or runtime errors asking for the org.xml.sax package.
You need to have xerces or another XML parser in your classpath. Xerces is included in the download.
I don't understand how the ... API works.
The best way to understand how the API works is to look at the samples in the
test directory.
This contains an example of just about every feature of the library and is used to verify that they continue to work.
Of course, if you know a tech writer who would like to contribute to improving the documentation, that is always welcome :)
Server Issues
Can I use HttpUnit through a proxy server?
Yes. Call
WebClient.setProxyServer( host, port )
before sending your request, or if you
need to specify a username and password, call
WebClient.setProxyServer( host, port, username, password )
.
How can I handle certificates from a test machine?
Often you will need to run your secure website - complete with certificates - from a test
server. Under most conditions, this will cause a validation problem, as the JSSE detects that the
certificate does not match the requested server. You can "trick" HttpUnit into accepting the certificate
as follows:
Assume that your certificate is for your real machine at realserver.domain.com
and your
test machine is at the IP address: 192.168.1.23. You want your tests to send requests to
realserver.domain.com
(since that is what your certificates expect), but to actually
go to the test machine.
First, create a DNSListener
:
DNSListener aListener = new DNSListener() {
public String getIpAddress( String hostName ) {
if (hostName.equals( "realserver.domain.com" )) return "192.168.1.23";
return hostName;
}
};
This listener substitutes the test IP address for the specified server name. Now specify this listener
for your client by calling
wc.getClientProperties().setDnsListener( aListener )
.
All subsequent requests will be rerouted as you specify, but the security layer should think it is coming
from the named server.
Why isn't HttpUnit handling my non-English pages?
When no Content-Type header is specified, HTML 1.1 says that the character set is to be taken as iso-8859-1.
Unfortunately, some HTTP servers do not send this parameter correctly, and many browsers provide a workaround
to permit the user to determine the character set in some other fashion. To imitate this behavior,
HttpUnit allows you to set the expected character set for future pages by calling
HttpUnitOptions.setDefaultCharacterSet()
.
This setting will not apply to those pages for which the server specifies the character set.
Non-English form handling is supported as well. Any parameter values entered into a form will be encoded as the same
character set as the document containing the form. The server code will then have to handle those characters
appropriately. In Java, that would be converting them to Unicode with statements such as
String rawName = request.getParameter( "name" );
String japaneseName = new String( name.getBytes("8859_1"), "EUC_JP" );
where the proper encoding should be substituted for "EUC_JP". The
getBytes
call is needed to extract
the raw bytes from the parameter string.
How does HttpUnit handle request timeouts?
Badly. Unfortunately, through JDK 1.3, HttpURLConnection (used by HttpUnit to access web servers) has no support for timeouts.
Supposedly, this is changed in JDK 1.4, but HttpUnit does not support the new feature as yet.
Why isn't HttpUnit seeing cookies from my site?
According to the
Cookie specification,
sites may specify a domain and path to restrict the scope of their cookies. These specifications
must follow certain rules, and the vast majority of sites do. Unfortunately, there are some
popular sites (such as Yahoo.com) which do not. By default, HttpUnit is very strict about
implementing the spec, and will reject such cookies. You can control this behavior by calls to the
CookieProperties class.
If you suspect that this is happening, you can also register a
CookieListener to watch for cookies
being rejected.
Form Handling Problems
How do I uncheck a checkbox?
The setParameter call lets you specify the values per parameter name. An unchecked checkbox has no value to transmit, so you would call:
form.setParameter( "checkbox-name", new String[0] );
or
form.removeParameter( "checkbox-name" );
If the checkbox's name is unique, you can say:
form.setCheckbox( "checkbox-name", false );
or
form.toggleCheckbox( "checkbox-name" );
Why can't I change hidden parameter values?
HttpUnit is intended primarily to imitate what a user could do. Since a user cannot change
hidden parameters directly, HttpUnit stops you from doing it in your code as part of its validation
of parameters against the form containing them. If you have to change these values, you have a couple of choices:
- Rather than setting the parameters directly on the form, call
WebForm.newUnvalidatedRequest()
(specifying any submit button information if necessary) to obtain a WebRequest copied from
the form but not tied to it. You will then be able to set any values you like in this request.
Note that parameter order is not guaranteed when such a request is submitted, nor are any JavaScript events fired.
- You can call
<form>.getScriptableObject().setParameterValue( <name>, <value> );
to change
the underlying value in the form. This will behave the way JavaScript does - resetting the form will now restore this value,
rather than the original one.
How do I get the response when I click a button?
When you call Button.click, a number of things can happen, depending on the definition of the button.
Unfortunately, it is then impossible to predict what, if any, response is relevant. The method therefore
does not return WebResponse. In most cases, it is the main window of your browser that you care about
and you can always obtain that by calling
client.getCurrentPage()
.
Links and Images
How do I find a link for a given image?
It is not uncommon to encounter a structure such as
<a href="something"><img src=blah name=my_image></a>. The find this link, the first step
is to find the image, for which a number of methods are defined, allowing images to be found based on name,
src attribute, id, or alt text. Given the image, the link is easily obtained by calling
image.getLink()
JavaScript support
How do I use HttpUnit to test my pages that use JavaScript?
For most purposes, You should not have to do anything special; however, not all JavaScript constructs are supported as yet.
See
the list of supported JavaScript features for more information.
JavaScript is not being executed at all. Why not?
If you do not have the Rhino JAR (js.jar) in your classpath, JavaScript features do not work.
How do I turn off scripting for certain tests?
If you call
HttpUnitOptions.setScriptingEnabled( false );
JavaScript will not be interpreted.
My JavaScript works in a browser, but HttpUnit says "ReferenceError: ... is not defined"
This usually indicates that you are calling a function which contains statements that HttpUnit does not understand.
Rhino unfortunately does not provide a very helpful error message in such cases. Try removing statements from the
function until you find out which one is causing the problem and then submit a request for the offending construct to be supported.
How do I test JavaScript alert boxes?
HttpUnit automatically acts as though you have clicked the OK button and queues up a list of messages generated by
calls to the alert() function. You can see the messages by making successive calls to
client.popNextAlert()
.
each of which will return the next alert message and remove it from the queue. You can at any time
call
client.getNextAlert()
to see the next message without removing it. If there are no messages left in the queue, these methods
simply return an empty string.
How do I test JavaScript popup dialogs?
You can register a
DialogResponder
by calling
client.setDialogResponder( responder )
, specifying an object which should be invoked
whenever JavaScript code invokes a
prompt()
or
confirm()
function. The
DialogResponder
interface has two methods. When a
confirm()
function is encountered, HttpUnit will invoke the
getConfirmation
method, passing the prompt as a parameter. The method should return
true
to indicate the clicking of the OK
button or
false
to indicate that the CANCEL button was clicked.
When a prompt()
function is encountered, HttpUnit will invoke the
getUserResponse
method, passing the prompt and default response. The method should return the response appropriate for the test.
How do I handle a page that uses JavaScript features that HttpUnit does not support?
If you call
HttpUnitOptions.setExceptionsThrownOnScriptError( false );
problems will be recorded but will not throw exceptions. You can see the list of problems detected by calling
HttpUnitOptions.getScriptErrorMessages();
I called document.write() in my page, but getText() does not show the results! Why not?
The execution of scripts only affects the parsed result. WebResponse.getText() shows the
original
text in the page. You will see the changed text only if you examine text inside a parsed item such as a form, link, or table.
ServletUnit usage
How can I see the servlet session between calls?
Servlet session is maintained on the server. Therefore, if you want to do this, you must be using
ServletUnit.
Once you have that working, you can get take advantage of the power of ServletUnit to get at internal values,
using the
InvocationContext
object. For example,
ServletUnitClient client = ... // the client you have been using
client.getResponse( servlet-url ); // invoke your servlet normally
// now get an invocation context using the same URL used to invoke the servlet
InvocationContext ic = client.newInvocationContext( servlet-url );
// obain the session just used. Note: pass false to avoid creating it if it does not already exist
HttpSession session = ic.getRequest().getSession( false );
Now you can examine the contents of the session to see what the servlet left in it.
Can I use JSPs with ServletUnit?
Absolutely! But since you are now emulating the servlet environment, you will need to make sure that you have access
to the relevant classes. By default, ServletUnit is configured to use Jasper, the JSP engine which is part of Tomcat.
You will therefore need the Jasper jar in your classpath, along with any on which it depends. Once you have done that,
your JSPs should run, just as they would in Tomcat.
Copyright © 2000-2008, Russell Gold
Hosted by