Java Reference
In-Depth Information
Listing 12.5
Example form page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
➥
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function validate_form(form) {
if (form.in_text.value=="") {
alert("Please enter a value.");
form.in_text.focus();
return false;
}
}
</script>
<title>Form</title></head>
<body>
<form name="validated_form" action="submit.html"
➥
onsubmit="return validate_form(this);" method="post">
Value:
<input type="text" name="in_text" id="in_text" size="30"/>
<input type="submit" value="Submit" id="submit"/>
</form>
</body>
</html>
This form looks like figure 12.1 when
you click the button without input.
We test normal user interaction with
the form in listing 12.6.
Figure 12.1
Sample form and alert
Listing 12.6
Testing a form
@Test
public void
testForm()
throws
IOException {
WebClient client =
new
WebClient();
HtmlPage page = (HtmlPage)
client.getPage("file:src/main/webapp/formtest.html");
HtmlForm form = page.getFormByName("validated_form");
HtmlTextInput input =(HtmlTextInput) form.getInputByName("in_text");
input.setValueAttribute("typing...");
HtmlSubmitInput submitButton = (HtmlSubmitInput)
form.getInputByName("submit");
HtmlPage resultPage = (HtmlPage) submitButton.click();
WebAssert.assertTitleEquals(resultPage, "Result");
// more asserts...
}
B
C