[Httpunit-commit] CVS: httpunit/doc/tutorial task1editor-entry.html,1.3,1.4 task1editor-validation.h
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-05-23 03:07:33
|
Update of /cvsroot/httpunit/httpunit/doc/tutorial
In directory sc8-pr-cvs1:/tmp/cvs-serv23156/doc/tutorial
Modified Files:
task1editor-entry.html task1editor-validation.html
Log Message:
use form.setParameter
Index: task1editor-entry.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1editor-entry.html,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- task1editor-entry.html 27 Nov 2001 16:40:47 -0000 1.3
+++ task1editor-entry.html 23 May 2003 03:07:30 -0000 1.4
@@ -89,13 +89,13 @@
WebForm form = response.getFormWithID( "pool" );
assertNotNull( "No form found with ID 'pool'", form );
- WebRequest request = form.getRequest( "save", "Save" ); // (1) select a submit button
- request.setParameter( "away1", "Detroit Lions" ); // (2) enter values into the form
- request.setParameter( "home1", "Denver Broncos" );
- request.setParameter( "tiebreaker", "1" );
- response = client.getResponse( request ); // (3) submit the form
- form = response.getFormWithID( "pool" );
+ form.setParameter( "away1", "Detroit Lions" ); // (1) enter values into the form
+ form.setParameter( "home1", "Denver Broncos" );
+ form.setParameter( "tiebreaker", "1" );
+
+ SubmitButton saveButton = getSubmitButton( "save", "Save" ); // (2) select the desired submit button
+ response = form.submit( saveButton ); // (3) submit the form
assertEquals( "Away team 0", "", form.getParameterValue( "away0" ) ); // (4) verify the response
assertEquals( "Home team 0", "", form.getParameterValue( "home0" ) );
@@ -106,11 +106,10 @@
}
</pre>
<p>This is our first form submission. Take note:<ol>
-<li>The first step in submitting a form is obtaining a request from it. Since this form has two named submit buttons,
-we must specify which one we want to be included in the request.</li>
<li>We make a call for each parameter that we want to set. Any parameters we do not set will be submitted with their
default values.</li>
-<li>We submit the form by asking our client to get a response to the prepared request.</li>
+<li>Since there is more than one button in the form, we ask for the one we want by name and value.</li>
+<li>We submit the form, specifying the desired button.</li>
<li>We can use the same methods as above to check the new state of the pool. Alternately, we could have examined the
BettingPool object directly.</li></ol></p>
Index: task1editor-validation.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1editor-validation.html,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- task1editor-validation.html 22 Feb 2002 15:49:04 -0000 1.5
+++ task1editor-validation.html 23 May 2003 03:07:30 -0000 1.6
@@ -32,24 +32,26 @@
client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
- WebRequest request = form.getRequest( "save", "Open Pool" );
- request.setParameter( "away1", "Detroit Lions" );
- request.setParameter( "home1", "Denver Broncos" );
- request.setParameter( "home2", "Baltimore Ravens" );
- request.setParameter( "tiebreaker", "3" );
- InvocationContext context = client.newInvocation( request ); // (1) create an invocation context
-
- PoolEditorServlet servlet = (PoolEditorServlet) context.getServlet(); // (2) locate the invoked servlet
- servlet.updateBettingPool( context.getRequest() ); // (3) ask servlet to update the data
- String[] errors = servlet.getValidationErrors(); // (4) ask servlet to check the data
+ form.setParameter( "away1", "Detroit Lions" );
+ form.setParameter( "home1", "Denver Broncos" );
+ form.setParameter( "home2", "Baltimore Ravens" );
+ form.setParameter( "tiebreaker", "3" );
+ WebRequest request = form.getRequest( "save", "Open Pool" ); // (1) select the request object directly
+ InvocationContext context = client.newInvocation( request ); // (2) create an invocation context
+
+ PoolEditorServlet servlet = (PoolEditorServlet) context.getServlet(); // (3) locate the invoked servlet
+ servlet.updateBettingPool( context.getRequest() ); // (4) ask servlet to update the data
+ String[] errors = servlet.getValidationErrors(); // (5) ask servlet to check the data
assertEquals( "Number of errors reported", 2, errors.length );
assertEquals( "First error", "Tiebreaker is not a valid game", errors[0] );
assertEquals( "Second error", "Game 2 has no away team", errors[1] );
}
</pre>
<p>This test starts out like all the others, but once we have created the request, things venture into new territory:<ol>
-<li>Rather than simply asking for the response, we create a context in which we can step through the invocation of the servlet.
+<li>Rather than simply submitting the form, we ask for the request object used to do the submission. This was hidden
+from us in the last test.</li>
+<li>Given the request, we create a context in which we can step through the invocation of the servlet.
This uses the same mechanisms to locate and initialize the servlet, request, and response objects, but does not actually
invoke the servlet to process the request.</li>
<li>We retrieve the initialized servlet and cast it in order to get at its intermediate methods.</li>
@@ -98,28 +100,28 @@
client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
- WebRequest request = form.getRequest( "save", "Open Pool" ); // (1) select a submit button
- request.setParameter( "away1", "Detroit Lions" ); // (2) enter bad values into the form
- request.setParameter( "home1", "Denver Broncos" );
- request.setParameter( "home2", "Baltimore Ravens" );
- request.setParameter( "tiebreaker", "3" );
- response = client.getResponse( request ); // (3) submit the form
+ form.setParameter( "away1", "Detroit Lions" ); // (1) enter bad values into the form
+ form.setParameter( "home1", "Denver Broncos" );
+ form.setParameter( "home2", "Baltimore Ravens" );
+ form.setParameter( "tiebreaker", "3" );
- WebTable errorTable = response.getTableStartingWithPrefix( "Cannot ope" ); // (4) Look for error table
+ SubmitButton openButton = getSubmitButton( "save", "Open Pool" ); // (2) select the desired submit button
+ response = form.submit( saveButton ); // (3) submit the form
+
+ WebTable errorTable = response.getTableWithID( "errors" ); // (4) Look for the error table
assertNotNull( "No errors reported", errorTable );
- String[][] cells = errorTable.asText(); // (5) Convert non-empty cells to text
+ String[][] cells = errorTable.asText(); // (5) Convert non-empty cells to text
assertEquals( "Number of error messages provided", 2, cells.length - 1 );
assertEquals( "Error message", "Tiebreaker is not a valid game", cells[1][0] );
assertEquals( "Error message", "Game 2 has no away team", cells[2][0] );
}
</pre>
<p>Note:<ol>
-<li>We select the "Open Pool" button to be included with the form submission.</li>
-<li>We then enter known bad values.</li>
+<li>We enter known bad values.</li>
+<li>We then select the "Open Pool" button to be included with the form submission.</li>
<li>We want the response when we submit the form changes.</li>
-<li>We expect to find them in a table which we can recognize because its upper-leftmost non-empty cell which starts with
-a known string.</li>
+<li>We expect to find them in a table with "errors" as its ID.</li>
<li>Since we want to examine the textual content of any non-empty cells in the table, we ask that the table be converted
to a two-dimensional string array. In this case, there should only be one non-blank cell in each row.</li></ol></p>
@@ -136,7 +138,7 @@
<b>private void</b> reportErrors( PrintWriter pw, String[] errors ) {
- pw.println( "<table width='90%' style='background-color=yellow; " );
+ pw.println( "<table id='errors' width='90%' style='background-color=yellow; " );
pw.println( " border-color: black; border-width: 2; border-style: solid'>" );
pw.println( "<tr><td colspan='2'><b>Cannot open pool for betting:</b></td></tr>" );
<b>for</b> (int i=0; i < errors.length; i++) {
@@ -162,14 +164,13 @@
client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
- WebRequest request = form.getRequest( "save", "Open Pool" );
- request.setParameter( "away1", "Detroit Lions" );
- request.setParameter( "home1", "Denver Broncos" );
- request.setParameter( "away3", "Indianapolis Colts" );
- request.setParameter( "home3", "Baltimore Ravens" );
- request.setParameter( "tiebreaker", "3" );
- client.getResponse( request ); // (1) ignore the response
+ form.setParameter( "away1", "Detroit Lions" );
+ form.setParameter( "home1", "Denver Broncos" );
+ form.setParameter( "away3", "Indianapolis Colts" );
+ form.setParameter( "home3", "Baltimore Ravens" );
+ form.setParameter( "tiebreaker", "3" );
+ form.getSubmitButton( "save", "Open Pool" ).click(); // (1) click the submit button
response = client.getResponse( "http://localhost/PoolEditor" ); // (2) retrieve the page separately
form = response.getFormWithID( "pool" );
@@ -183,7 +184,8 @@
}
</pre>
<p>Note:<ol>
-<li>We are not interested in the response this time because we may ultimately have the browser forwarded to the main page.</li>
+<li>Since we are not interested in the response this time (because we may ultimately have the browser forwarded to the main page),
+we simply click the submit button. This does the same as calling form.submit(), but does not return a response value.</li>
<li>We come back to the form as though we were planning on editing it anew.</li>
<li>We want to ensure that the submit buttons are disabled so the user cannot submit the form.</li>
<li>We also verify that the fields are now marked readonly, which would prevent us from changing them. If the
|