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

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 = "http://www.eDogFood.com/ordering.jsp";
boolean caught = false;
try{
WebResponse resp = wc.getResponse(siteUrl);
}
catch(AuthorizationRequiredException e){
System.out.println(e); caught = true;
}
assertTrue(caught);
wc.setAuthorization("Jane Tester", "TestPassword123");
WebResponse resp = wc.getResponse(siteUrl); assertEquals("Logged In - Welcome", resp.getTitle());
}
Methods
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{
393
WebConversation wc = new WebConversation();
String siteUrl = "http://www.eDogFood.com/ordering.jsp"; 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*/
}
getCookieHeaderField()
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.
getCookieNames()
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.)
getFrameNames()
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: http://java.sun.com/j2se/1.3/docs/api/index.html (the Javadoc for class URLConnection in java.net).
Listing 16.2: Use of Frames in HttpUnit.
public void testFrameInteraction() throws Exception{
/*Note: uses collections from java.util*/
String site = "http://java.sun.com/j2se/1.3/docs/api/index.html";
WebClient wc = new WebConversation();
394
WebResponse response = wc.getResponse(site);
/*assert that all of the expected frames exist*/
String[] frameNames = wc.getFrameNames();
List frameList = Arrays.asList(frameNames);
assertTrue(frameList.contains("classFrame"));
assertTrue(frameList.contains("packageListFrame"));
assertTrue(frameList.contains("packageFrame"));
assertTrue(frameList.contains("_top"));
/*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);
}
getHeaderFields()
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