Package com.facilita.fc.web
Class Form
- java.lang.Object
-
- com.facilita.fc.web.HtmlElement
-
- com.facilita.fc.web.Form
-
public class Form extends HtmlElement
Represents an HTML form. Each element in the form is represented by anHtmlElement
object. The elements in the form are accessed by using the various "get" methods (e.g.getHtmlElement(String)
,getInputElement(String, String)
). TheHtmlElement
objects can be modified to change the data that will be sent back to the server. More elements can be added to the form usingaddElement(HtmlElement)
, and removed usingremoveElement(String, String)
.Usually, a
Form
object is obtained by extracting it from aResponse
, using theResponse.extractForm(int)
method.e.g.
The form is then usually modified by changing theResponse response = request.send(); Form form = response.extractForm(0);
HtmlElement
objects it contains, and sent back to the server on a later request. This could be either as query data on a GET request:
Or, as the message body on a POST request:// Create a Url with the form data as a query string Url url1 = new Url("http://localhost/"); url1 = url1.withQuery(form); // Create a GET request based on the url and send it to the web server. Request request1 = getWebBrowser().createRequest(HttpMethod.GET, url1, 1); // Send the request to the web server Response response1 = request1.send();
// Create a Url Url url2 = new Url("http://localhost/"); // Create a POST request and set the form data as the message body Request request2 = getWebBrowser().createRequest(HttpMethod.POST, url2, 2); request2.setMessageBody(form); // Send the the request to the web server Response response2 = request2.send();
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addElement(HtmlElement element)
Appends the specifiedHtmlElement
to the end of thisForm
.void
delete()
boolean
extractWasSuccessful()
Returnstrue
if this form was successfully extracted from aResponse
using theResponse.extractForm(int)
method.java.lang.String
getAction()
Gets the location to send the form data when theForm
is submitted.static long
getCPtr(Form obj)
For internal use only.int
getElementCount()
Gets the number ofHtmlElement
objects in thisForm
.java.lang.String
getEncType()
Gets the encoding type for thisForm
.FileElement
getFileElement(java.lang.String name)
Gets theFileElement
with the specifiedname
.FileElement
getFileElement(java.lang.String name, java.lang.String value)
HtmlElement
getHtmlElement(int index)
Gets the nthHtmlElement
object in this form.HtmlElement
getHtmlElement(java.lang.String name)
Gets theHtmlElement
with the specifiedname
.HtmlElement
getHtmlElement(java.lang.String name, java.lang.String value)
int
getIndex()
Gets the zero-based index of this form within theResponse
from which it was extracted.InputElement
getInputElement(java.lang.String name)
Gets theInputElement
with the specifiedname
.InputElement
getInputElement(java.lang.String name, java.lang.String value)
java.lang.String
getMethod()
Gets the HTTP method that should be used to send thisForm
to the web server, e.g. GET, POST.java.lang.String
getMultipartFormBoundary()
Gets theString
that will be used as the boundary between input elements when encoding this form as multipart/form-data.OptionElement
getOptionElement(java.lang.String name)
Gets theOptionElement
with the specifiedname
.OptionElement
getOptionElement(java.lang.String name, java.lang.String value)
SelectElement
getSelectElement(java.lang.String name)
Gets theSelectElement
with the specifiedname
.SelectElement
getSelectElement(java.lang.String name, java.lang.String value)
TextAreaElement
getTextAreaElement(java.lang.String name)
Gets theTextAreaElement
with the specifiedname
.TextAreaElement
getTextAreaElement(java.lang.String name, java.lang.String value)
void
insertElement(int index, HtmlElement element)
Inserts the specifiedHtmlElement
into the form.boolean
isMultipartForm()
Returnstrue
if this is a multi-part form.void
removeElement(int index)
Removes the nthHtmlElement
from thisForm
.void
removeElement(java.lang.String name)
Removes theHtmlElement
with the specifiedname
from this form.void
removeElement(java.lang.String name, java.lang.String value)
void
setCharEncoding(java.lang.String charEncoding)
Sets the character encoding to use when interpreting thisForm
.void
setEncType(java.lang.String encType)
Sets the encoding type for thisForm
.void
setMultipartFormBoundary(java.lang.String boundary)
Sets the multipart boundary that will be used when encoding this form as multipart/form-data.-
Methods inherited from class com.facilita.fc.web.HtmlElement
clone, getAfterEndTagPosition, getAfterStartTagPosition, getAttribute, getAttributes, getCharEncoding, getContent, getContentPosition, getCPtr, getId, getName, getTag, getValue, hasAttribute, isDisabled, isSuccessfulControl, removeAttribute, setAttribute, setAttribute, setContent, setDisabled, setId, setName, setValue
-
-
-
-
Constructor Detail
-
Form
public Form(long cPtr, boolean cMemoryOwn)
For internal use only. Of no interest to the user.- Parameters:
cPtr
- pointer to the CPP wrapped objectcMemoryOwn
- indicates if this object is responsible for memory management of the CPP object
-
Form
public Form()
Creates a new blankForm
object containing noHtmlElement
objects.
-
Form
public Form(Form other)
Creates a newForm
object which is an exact copy of the specifiedForm
.- Parameters:
other
- theForm
to copy
-
-
Method Detail
-
getCPtr
public static long getCPtr(Form obj)
For internal use only. Of no interest to the user.- Parameters:
obj
- a reference to an object of this class- Returns:
- a long containing the address of the CPP wrapped object
-
delete
public void delete()
- Overrides:
delete
in classHtmlElement
-
getHtmlElement
public HtmlElement getHtmlElement(java.lang.String name) throws NoSuchValueException, java.io.UnsupportedEncodingException
Gets theHtmlElement
with the specifiedname
.- Parameters:
name
- the name of theHtmlElement
to retrieve- Returns:
- the first
HtmlElement
in the form that has the specifiedname
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anHtmlElement
with the specifiedname
- See Also:
getInputElement(String)
,getFileElement(String)
,getTextAreaElement(String)
,getSelectElement(String)
,getOptionElement(String)
-
getHtmlElement
public HtmlElement getHtmlElement(java.lang.String name, java.lang.String value) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
- Parameters:
name
- the name of theHtmlElement
to retrievevalue
- the value of theHtmlElement
to retrieve- Returns:
- the first
HtmlElement
in the form that has the specifiedname
andvalue
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anHtmlElement
with the specifiedname
andvalue
BadValueException
- An internal error occurred- See Also:
getInputElement(String, String)
,getFileElement(String, String)
,getTextAreaElement(String, String)
,getSelectElement(String, String)
,getOptionElement(String, String)
-
getInputElement
public InputElement getInputElement(java.lang.String name) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
Gets theInputElement
with the specifiedname
.- Parameters:
name
- the name of theInputElement
to retrieve- Returns:
- the first
InputElement
in the form that has the specifiedname
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anInputElement
with the specifiedname
BadValueException
- theHtmlElement
with the specifiedname
is not anInputElement
- See Also:
getHtmlElement(String)
,getFileElement(String)
,getTextAreaElement(String)
,getSelectElement(String)
,getOptionElement(String)
-
getInputElement
public InputElement getInputElement(java.lang.String name, java.lang.String value) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
- Parameters:
name
- the name of theInputElement
to retrievevalue
- the value of theInputElement
to retrieve- Returns:
- the first
InputElement
in the form that has the specifiedname
andvalue
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anInputElement
with the specifiedname
andvalue
BadValueException
- theHtmlElement
with the specifiedname
andvalue
is not anInputElement
- See Also:
getInputElement(String, String)
,getFileElement(String, String)
,getTextAreaElement(String, String)
,getSelectElement(String, String)
,getOptionElement(String, String)
-
getFileElement
public FileElement getFileElement(java.lang.String name) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
Gets theFileElement
with the specifiedname
.- Parameters:
name
- the name of theFileElement
to retrieve- Returns:
- the first
FileElement
in the form that has the specifiedname
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain aFileElement
with the specifiedname
BadValueException
- theHtmlElement
with the specifiedname
is not aFileElement
- See Also:
getHtmlElement(String)
,getInputElement(String)
,getTextAreaElement(String)
,getSelectElement(String)
,getOptionElement(String)
-
getFileElement
public FileElement getFileElement(java.lang.String name, java.lang.String value) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
- Parameters:
name
- the name of theFileElement
to retrievevalue
- the value of theFileElement
to retrieve- Returns:
- the first
FileElement
in the form that has the specifiedname
andvalue
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain aFileElement
with the specifiedname
andvalue
BadValueException
- theHtmlElement
with the specifiedname
andvalue
is not aFileElement
- See Also:
getInputElement(String, String)
,getHtmlElement(String, String)
,getTextAreaElement(String, String)
,getSelectElement(String, String)
,getOptionElement(String, String)
-
getOptionElement
public OptionElement getOptionElement(java.lang.String name) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
Gets theOptionElement
with the specifiedname
.- Parameters:
name
- the name of theOptionElement
to retrieve- Returns:
- the first
OptionElement
in the form that has the specifiedname
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anOptionElement
with the specifiedname
BadValueException
- theHtmlElement
with the specifiedname
is not anOptionElement
- See Also:
getHtmlElement(String)
,getInputElement(String)
,getTextAreaElement(String)
,getSelectElement(String)
,getFileElement(String)
-
getOptionElement
public OptionElement getOptionElement(java.lang.String name, java.lang.String value) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
- Parameters:
name
- the name of theOptionElement
to retrievevalue
- the value of theOptionElement
to retrieve- Returns:
- the first
OptionElement
in the form that has the specifiedname
andvalue
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anOptionElement
with the specifiedname
andvalue
BadValueException
- theHtmlElement
with the specifiedname
andvalue
is not anOptionElement
- See Also:
getInputElement(String, String)
,getHtmlElement(String, String)
,getTextAreaElement(String, String)
,getSelectElement(String, String)
,getFileElement(String, String)
-
getSelectElement
public SelectElement getSelectElement(java.lang.String name) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
Gets theSelectElement
with the specifiedname
.- Parameters:
name
- the name of theSelectElement
to retrieve- Returns:
- the first
SelectElement
in the form that has the specifiedname
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain aSelectElement
with the specifiedname
BadValueException
- theHtmlElement
with the specifiedname
is not aSelectElement
- See Also:
getHtmlElement(String)
,getInputElement(String)
,getTextAreaElement(String)
,getOptionElement(String)
,getFileElement(String)
-
getSelectElement
public SelectElement getSelectElement(java.lang.String name, java.lang.String value) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
- Parameters:
name
- the name of theSelectElement
to retrievevalue
- the value of theSelectElement
to retrieve- Returns:
- the first
SelectElement
in the form that has the specifiedname
andvalue
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain aSelectElement
with the specifiedname
andvalue
BadValueException
- theHtmlElement
with the specifiedname
andvalue
is not aSelectElement
- See Also:
getInputElement(String, String)
,getHtmlElement(String, String)
,getTextAreaElement(String, String)
,getOptionElement(String, String)
,getFileElement(String, String)
-
getTextAreaElement
public TextAreaElement getTextAreaElement(java.lang.String name) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
Gets theTextAreaElement
with the specifiedname
.- Parameters:
name
- the name of theTextAreaElement
to retrieve- Returns:
- the first
TextAreaElement
in the form that has the specifiedname
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain aTextAreaElement
with the specifiedname
BadValueException
- theHtmlElement
with the specifiedname
is not aTextAreaElement
- See Also:
getHtmlElement(String)
,getInputElement(String)
,getSelectElement(String)
,getOptionElement(String)
,getFileElement(String)
-
getTextAreaElement
public TextAreaElement getTextAreaElement(java.lang.String name, java.lang.String value) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
- Parameters:
name
- the name of theTextAreaElement
to retrievevalue
- the value of theTextAreaElement
to retrieve- Returns:
- the first
TextAreaElement
in the form that has the specifiedname
andvalue
- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain aTextAreaElement
with the specifiedname
andvalue
BadValueException
- theHtmlElement
with the specifiedname
andvalue
is not aTextAreaElement
- See Also:
getInputElement(String, String)
,getHtmlElement(String, String)
,getSelectElement(String, String)
,getOptionElement(String, String)
,getFileElement(String, String)
-
removeElement
public void removeElement(java.lang.String name, java.lang.String value) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
- Parameters:
name
- the name of theHtmlElement
to removevalue
- the value of theHtmlElement
to remove- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anHtmlElement
with the specifiedname
andvalue
BadValueException
- An internal error occurred- See Also:
addElement(HtmlElement)
,removeElement(String)
,removeElement(int)
-
removeElement
public void removeElement(java.lang.String name) throws NoSuchValueException, BadValueException, java.io.UnsupportedEncodingException
Removes theHtmlElement
with the specifiedname
from this form.- Parameters:
name
- the name of theHtmlElement
to remove- Throws:
java.io.UnsupportedEncodingException
- The encoding is not supported.NoSuchValueException
- the form does not contain anHtmlElement
with the specifiedname
BadValueException
- An internal error occurred- See Also:
addElement(HtmlElement)
,removeElement(String, String)
,removeElement(int)
-
getIndex
public int getIndex()
Gets the zero-based index of this form within theResponse
from which it was extracted.e.g. the 1st
Form
in aResponse
will have an index of zero, the 2nd form will have an index of one, etc.- Returns:
- the index of this
Form
within theResponse
from which it was extracted - See Also:
Response.extractForm(int)
-
getMethod
public java.lang.String getMethod()
Gets the HTTP method that should be used to send thisForm
to the web server, e.g. GET, POST. This method is equivalent to callinggetAttribute("method");
.- Returns:
- the HTTP method that should be used to send this form to the web server
-
getAction
public java.lang.String getAction()
Gets the location to send the form data when theForm
is submitted. This method is equivalent to callinggetAttribute("action");
.- Returns:
- the value of the "action" attribute
-
getEncType
public java.lang.String getEncType()
Gets the encoding type for thisForm
. This method is equivalent to callinggetAttribute("enctype");
.e.g.
application/x-www-form-urlencoded
,multipart/form-data
- Returns:
- the encoding type for this
Form
- See Also:
setEncType(String)
-
setEncType
public void setEncType(java.lang.String encType)
Sets the encoding type for thisForm
. This method is equivalent to callingsetAttribute("enctype");
.e.g.
application/x-www-form-urlencoded
,multipart/form-data
- Parameters:
encType
- the encoding type to set- See Also:
getEncType()
-
getElementCount
public int getElementCount()
Gets the number ofHtmlElement
objects in thisForm
.- Returns:
- the number of
HtmlElement
objects in thisForm
-
getHtmlElement
public HtmlElement getHtmlElement(int index) throws NoSuchValueException
Gets the nthHtmlElement
object in this form.- Parameters:
index
- the index of theHtmlElement
to get- Returns:
- the nth
HtmlElement
in this form - Throws:
NoSuchValueException
- There is no element with the specified index.
-
addElement
public void addElement(HtmlElement element)
Appends the specifiedHtmlElement
to the end of thisForm
.- Parameters:
element
- theHtmlElement
to add
-
insertElement
public void insertElement(int index, HtmlElement element)
Inserts the specifiedHtmlElement
into the form.- Parameters:
index
- the position in the form at which the specifiedHtmlElement
is to be insertedelement
- theHtmlElement
to insert
-
removeElement
public void removeElement(int index)
Removes the nthHtmlElement
from thisForm
.- Parameters:
index
- the index of theHtmlElement
to remove- See Also:
addElement(HtmlElement)
,removeElement(String)
,removeElement(int)
-
extractWasSuccessful
public boolean extractWasSuccessful()
Returnstrue
if this form was successfully extracted from aResponse
using theResponse.extractForm(int)
method.- Returns:
true
if this form was successfully extracted from aResponse
- See Also:
Response.extractForm(int)
-
getMultipartFormBoundary
public java.lang.String getMultipartFormBoundary()
Gets theString
that will be used as the boundary between input elements when encoding this form as multipart/form-data.e.g. for this POST data message body:
--multipart-form-boundary Content-Disposition: form-data; name="input1" value1 --multipart-form-boundary Content-Disposition: form-data; name="files-to-upload[]" Content-Type: multipart/mixed; boundary=multipart-file-boundary --multipart-file-boundary Content-Disposition: file; filename="image.bmp" Content-Type: image/bmp (binary image data) --multipart-file-boundary Content-Disposition: file; filename="image2.bmp" Content-Type: image/bmp (binary image data) --multipart-file-boundary-- --multipart-form-boundary--
form.getMultipartFormBoundary()
will return"--multipart-form-boundary"
.The Content-Type header sent to the server will be
Content-Type: multipart/form-data; boundary=multipart-form-boundary
- Returns:
- the
String
that will be used as the boundary between input elements (including the--
prefix), when encoding this form as multipart/form-data - See Also:
getEncType()
,setEncType(String)
,setMultipartFormBoundary(String)
-
setMultipartFormBoundary
public void setMultipartFormBoundary(java.lang.String boundary)
Sets the multipart boundary that will be used when encoding this form as multipart/form-data.e.g.
The POST data message body contains:// Populate form with data Form form = previousResponse.extractForm("myForm"); form.getInputElement("input1").setValue("value1"); form.getFileElement("files-to-upload[]").addFile("image.bmp", "C:\\dataFiles\\image.bmp", "image/bmp"); form.getFileElement("files-to-upload[]").addFile("image2.bmp", "C:\\dataFiles\\image2.bmp", "image/bmp"); // Set the multipart boundary for the form form.setMultipartFormBoundary("--multipart-form-boundary"); // Send the POST request Request request = getWebBrowser().createRequest(HttpMethod.POST, myUrl); request.setMessageBody(form); Response response = request.send();
The Content-Type header sent to the server will be--multipart-form-boundary Content-Disposition: form-data; name="input1" value1 --multipart-form-boundary Content-Disposition: form-data; name="files-to-upload[]" Content-Type: multipart/mixed; boundary=multipart-file-boundary --multipart-file-boundary Content-Disposition: file; filename="image.bmp" Content-Type: image/bmp (binary image data) --multipart-file-boundary Content-Disposition: file; filename="image2.bmp" Content-Type: image/bmp (binary image data) --multipart-file-boundary-- --multipart-form-boundary--
Content-Type: multipart/form-data; boundary=multipart-form-boundary
- Parameters:
boundary
- theString
that will be used as the boundary between input elements (including the--
prefix), when encoding this form as multipart/form-data- See Also:
getEncType()
,setEncType(String)
,getMultipartFormBoundary()
-
isMultipartForm
public boolean isMultipartForm()
Returnstrue
if this is a multi-part form. This method is equivalent togetEncType() == "multipart/form-data";
- Returns:
true
if the enctype for this form is "multipart/form-data"
-
setCharEncoding
public void setCharEncoding(java.lang.String charEncoding)
Sets the character encoding to use when interpreting thisForm
. Usually, the character encoding will be set automatically when the form is extracted from aResponse
using theResponse.extractForm(int)
method.- Overrides:
setCharEncoding
in classHtmlElement
- Parameters:
charEncoding
- the character encoding to use when interpreting thisForm
.- See Also:
HtmlElement.getCharEncoding()
-
-