Java Reference
In-Depth Information
Listing 13.1
testform.html—a simple form (client)
<html>
<body>
<script type="text/javascript">
function newXHR() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("This browser does not support XMLHTTP.");
}
}
function setMessage() {
var xhr = newXHR();
xhr.onreadystatechange=function() {
if(xhr.readyState == 4) {
document.helloForm.serverMessage.value=xhr.responseText;
}
}
xhr.open("GET","hello-world.asp", true);
xhr.send(null);
}
</script>
<form name="helloForm"><input value="Get Message" type="button"
name="getMsgBtn" onclick="setMessage();" /> Message: <input
type="text" name="serverMessage" /></form>
</body>
</html>
Let's examine the key elements in the
HTML
. The form
F
defines a button, which
when clicked invokes the JavaScript function
setMessage
C
. The first thing the
set-
Message
function does is call
newXHR
b
, which creates a new
HTTP
XML
request
object.
setMessage
then calls the server
E
and updates the
DOM
D
when the
response is ready.
The following shows the server-side source for this example (hello-world.asp).
B
C
D
E
F
<%
response.expires=-1
response.write("Hello World")
%>
The response is the string
"Hello
World"
b
. To run this example locally with
IIS
,
you'll need to create a virtual directory for the example webapp directory and use the
IIS
Permission Wizard to grant it default rights.
In order to test the form and check that the message is what we expect it to be, we
use the same
JU
nit scaffolding from the previous chapter and first set up a test suite to
manage the Selenium server:
B
[...]
@RunWith(ManagedSeleniumServerSuite.class)
@SuiteClasses( { UnmanagedFormTester.class })