in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Java Tools for Extreme programming mastering open source tools including - Hightower R.

Hightower R. Java Tools for Extreme programming mastering open source tools including - Wiley publishing , 2002. - 417 p.
ISBN: 0-471-20708
Download (direct link): javatoolsforextremeprog2002.pdf
Previous << 1 .. 144 145 146 147 148 149 < 150 > 151 152 153 154 155 156 .. 159 >> Next

Extends: ObjectDescription
WebClient emulates a Web browser for the purpose of maintaining context for a series of HTTP requests. It manages cookies, computes relative URLs, and presents a single object interface for sending requests to and retrieving responses from a server. Generally, every test method in an HttpUnit-based test case will need to access a WebClient.
WebClient is an abstract class with a single concrete subclass (WebConversation) within the HttpUnit framework; the closely related ServletUnit framework defines another one. As such, you will primarily use instances of WebConversation to access the functionality of WebClient.
The following example illustrates the basic use of WebClient/WebConversation: public void testSiteAuthorization(,,,) throws Exception {
WebConversation wc = new WebConversation();
String siteUrl = "";
boolean caught = false;
WebResponse resp = wc.getResponse(siteUrl);
catch(AuthorizationRequiredException e){
System.out.println(e); caught = true;
wc.setAuthorization("Jane Tester", "TestPassword123");
WebResponse resp = wc.getResponse(siteUrl); assertEquals("Logged In - Welcome", resp.getTitle());
addCookie(String name, String value)
public void addCookie(String name, String value)
This method adds the specified name/value pair to the list of cookies to be sent with every request to the server. The server may also set such cookies:
public void testCookieLogonTransfer() throws Exception{
WebConversation wc = new WebConversation();
String siteUrl = ""; wc.setAuthorization("Jane Tester", "TestPassword123");
WebResponse resp = wc.getResponse(siteUrl);
String sessionldKey = resp.getNewCookieNames()[0];
String sessionldValue = resp.getNewCookieValue(sessionKey);
WebConversation newBrowser = new WebConversation(); newBrowser.addCookie(sessionKey, sessionValue); newBrowser.getResponse(siteUrl);
/*--would throw AuthorizationRequiredException if not logged in*/
protected String getCookieHeaderField()
This method returns the value of the cookie header as a String. It appears to be used internally by WebClient, but it could be used externally for validation purposes.
public String[] getCookieNames()
getCookieValue(String name)
public String getCookieValue(String name)
These methods return the names and values of the active cookies that will be sent to the server with each request. (See addCookie() for an example.)
getFrameContents(String frameName)
public WebResponse getFrameContents(String frameName)
This method gets the contents of the frame with the specified name as a WebResponse. (See getFrameNames() for an example.)
public String[] getFrameNames()
This method returns a String[] containing the names of all the currently active frames in the virtual Web browser represented by the WebClient. The topmost frame can be retrieved with the keyword _top. As each request is made, the contents of the appropriate frame are replaced with the WebResponse resulting from the request. Assuming the String "fooFrame" is the target of the WebRequest request, resp and frame would refer to the same object after these lines were executed:
WebResponse resp = conversation.getResponse(request);
WebResponse frame = conversation.getFrameContents("fooFrame");
Listing 16.2 details how you can use frame interaction with HttpUnit. It may help to review the page the example is based on: (the Javadoc for class URLConnection in
Listing 16.2: Use of Frames in HttpUnit.
public void testFrameInteraction() throws Exception{
/*Note: uses collections from java.util*/
String site = "";
WebClient wc = new WebConversation();
WebResponse response = wc.getResponse(site);
/*assert that all of the expected frames exist*/
String[] frameNames = wc.getFrameNames();
List frameList = Arrays.asList(frameNames);
/*get the frame with the name 'packageFrame'*/ response = wc.getFrameContents("packageFrame");
/*follow a link which updates the classFrame *and assert that we end up in the correct place
WebLink link = response.getLinkWith("URLConnection"); assertEquals("classFrame", link.getTarget()); response = wc.getResponse(link.getRequest());
String frameString = response.getText(); assertTrue(frameString.indexOf("URLConnection") > -1);
/*check that the frame that was updated was actually the classFrame */
WebResponse frameContents = wc.getFrameContents("classFrame"); assertSame(response, frameContents);
protected java.util.Dictionary getHeaderFields()
This method returns a Dictionary containing all the active headers that will be sent to the server (including headers set by earlier responses and also those set by calls to setHeaderField).
Previous << 1 .. 144 145 146 147 148 149 < 150 > 151 152 153 154 155 156 .. 159 >> Next