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 anHtmlElementobject. The elements in the form are accessed by using the various "get" methods (e.g.getHtmlElement(String),getInputElement(String, String)). TheHtmlElementobjects 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
Formobject 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);HtmlElementobjects 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 voidaddElement(HtmlElement element)Appends the specifiedHtmlElementto the end of thisForm.voiddelete()booleanextractWasSuccessful()Returnstrueif this form was successfully extracted from aResponseusing theResponse.extractForm(int)method.java.lang.StringgetAction()Gets the location to send the form data when theFormis submitted.static longgetCPtr(Form obj)For internal use only.intgetElementCount()Gets the number ofHtmlElementobjects in thisForm.java.lang.StringgetEncType()Gets the encoding type for thisForm.FileElementgetFileElement(java.lang.String name)Gets theFileElementwith the specifiedname.FileElementgetFileElement(java.lang.String name, java.lang.String value)HtmlElementgetHtmlElement(int index)Gets the nthHtmlElementobject in this form.HtmlElementgetHtmlElement(java.lang.String name)Gets theHtmlElementwith the specifiedname.HtmlElementgetHtmlElement(java.lang.String name, java.lang.String value)intgetIndex()Gets the zero-based index of this form within theResponsefrom which it was extracted.InputElementgetInputElement(java.lang.String name)Gets theInputElementwith the specifiedname.InputElementgetInputElement(java.lang.String name, java.lang.String value)java.lang.StringgetMethod()Gets the HTTP method that should be used to send thisFormto the web server, e.g. GET, POST.java.lang.StringgetMultipartFormBoundary()Gets theStringthat will be used as the boundary between input elements when encoding this form as multipart/form-data.OptionElementgetOptionElement(java.lang.String name)Gets theOptionElementwith the specifiedname.OptionElementgetOptionElement(java.lang.String name, java.lang.String value)SelectElementgetSelectElement(java.lang.String name)Gets theSelectElementwith the specifiedname.SelectElementgetSelectElement(java.lang.String name, java.lang.String value)TextAreaElementgetTextAreaElement(java.lang.String name)Gets theTextAreaElementwith the specifiedname.TextAreaElementgetTextAreaElement(java.lang.String name, java.lang.String value)voidinsertElement(int index, HtmlElement element)Inserts the specifiedHtmlElementinto the form.booleanisMultipartForm()Returnstrueif this is a multi-part form.voidremoveElement(int index)Removes the nthHtmlElementfrom thisForm.voidremoveElement(java.lang.String name)Removes theHtmlElementwith the specifiednamefrom this form.voidremoveElement(java.lang.String name, java.lang.String value)voidsetCharEncoding(java.lang.String charEncoding)Sets the character encoding to use when interpreting thisForm.voidsetEncType(java.lang.String encType)Sets the encoding type for thisForm.voidsetMultipartFormBoundary(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 blankFormobject containing noHtmlElementobjects.
-
Form
public Form(Form other)
Creates a newFormobject which is an exact copy of the specifiedForm.- Parameters:
other- theFormto 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:
deletein classHtmlElement
-
getHtmlElement
public HtmlElement getHtmlElement(java.lang.String name) throws NoSuchValueException, java.io.UnsupportedEncodingException
Gets theHtmlElementwith the specifiedname.- Parameters:
name- the name of theHtmlElementto retrieve- Returns:
- the first
HtmlElementin the form that has the specifiedname - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anHtmlElementwith 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 theHtmlElementto retrievevalue- the value of theHtmlElementto retrieve- Returns:
- the first
HtmlElementin the form that has the specifiednameandvalue - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anHtmlElementwith the specifiednameandvalueBadValueException- 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 theInputElementwith the specifiedname.- Parameters:
name- the name of theInputElementto retrieve- Returns:
- the first
InputElementin the form that has the specifiedname - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anInputElementwith the specifiednameBadValueException- theHtmlElementwith the specifiednameis 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 theInputElementto retrievevalue- the value of theInputElementto retrieve- Returns:
- the first
InputElementin the form that has the specifiednameandvalue - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anInputElementwith the specifiednameandvalueBadValueException- theHtmlElementwith the specifiednameandvalueis 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 theFileElementwith the specifiedname.- Parameters:
name- the name of theFileElementto retrieve- Returns:
- the first
FileElementin the form that has the specifiedname - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain aFileElementwith the specifiednameBadValueException- theHtmlElementwith the specifiednameis 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 theFileElementto retrievevalue- the value of theFileElementto retrieve- Returns:
- the first
FileElementin the form that has the specifiednameandvalue - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain aFileElementwith the specifiednameandvalueBadValueException- theHtmlElementwith the specifiednameandvalueis 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 theOptionElementwith the specifiedname.- Parameters:
name- the name of theOptionElementto retrieve- Returns:
- the first
OptionElementin the form that has the specifiedname - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anOptionElementwith the specifiednameBadValueException- theHtmlElementwith the specifiednameis 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 theOptionElementto retrievevalue- the value of theOptionElementto retrieve- Returns:
- the first
OptionElementin the form that has the specifiednameandvalue - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anOptionElementwith the specifiednameandvalueBadValueException- theHtmlElementwith the specifiednameandvalueis 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 theSelectElementwith the specifiedname.- Parameters:
name- the name of theSelectElementto retrieve- Returns:
- the first
SelectElementin the form that has the specifiedname - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain aSelectElementwith the specifiednameBadValueException- theHtmlElementwith the specifiednameis 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 theSelectElementto retrievevalue- the value of theSelectElementto retrieve- Returns:
- the first
SelectElementin the form that has the specifiednameandvalue - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain aSelectElementwith the specifiednameandvalueBadValueException- theHtmlElementwith the specifiednameandvalueis 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 theTextAreaElementwith the specifiedname.- Parameters:
name- the name of theTextAreaElementto retrieve- Returns:
- the first
TextAreaElementin the form that has the specifiedname - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain aTextAreaElementwith the specifiednameBadValueException- theHtmlElementwith the specifiednameis 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 theTextAreaElementto retrievevalue- the value of theTextAreaElementto retrieve- Returns:
- the first
TextAreaElementin the form that has the specifiednameandvalue - Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain aTextAreaElementwith the specifiednameandvalueBadValueException- theHtmlElementwith the specifiednameandvalueis 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 theHtmlElementto removevalue- the value of theHtmlElementto remove- Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anHtmlElementwith the specifiednameandvalueBadValueException- 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.UnsupportedEncodingExceptionRemoves theHtmlElementwith the specifiednamefrom this form.- Parameters:
name- the name of theHtmlElementto remove- Throws:
java.io.UnsupportedEncodingException- The encoding is not supported.NoSuchValueException- the form does not contain anHtmlElementwith the specifiednameBadValueException- 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 theResponsefrom which it was extracted.e.g. the 1st
Formin aResponsewill have an index of zero, the 2nd form will have an index of one, etc.- Returns:
- the index of this
Formwithin theResponsefrom 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 thisFormto 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 theFormis 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 ofHtmlElementobjects in thisForm.- Returns:
- the number of
HtmlElementobjects in thisForm
-
getHtmlElement
public HtmlElement getHtmlElement(int index) throws NoSuchValueException
Gets the nthHtmlElementobject in this form.- Parameters:
index- the index of theHtmlElementto get- Returns:
- the nth
HtmlElementin this form - Throws:
NoSuchValueException- There is no element with the specified index.
-
addElement
public void addElement(HtmlElement element)
Appends the specifiedHtmlElementto the end of thisForm.- Parameters:
element- theHtmlElementto add
-
insertElement
public void insertElement(int index, HtmlElement element)Inserts the specifiedHtmlElementinto the form.- Parameters:
index- the position in the form at which the specifiedHtmlElementis to be insertedelement- theHtmlElementto insert
-
removeElement
public void removeElement(int index)
Removes the nthHtmlElementfrom thisForm.- Parameters:
index- the index of theHtmlElementto remove- See Also:
addElement(HtmlElement),removeElement(String),removeElement(int)
-
extractWasSuccessful
public boolean extractWasSuccessful()
Returnstrueif this form was successfully extracted from aResponseusing theResponse.extractForm(int)method.- Returns:
trueif this form was successfully extracted from aResponse- See Also:
Response.extractForm(int)
-
getMultipartFormBoundary
public java.lang.String getMultipartFormBoundary()
Gets theStringthat 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
Stringthat 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- theStringthat 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()
Returnstrueif this is a multi-part form. This method is equivalent togetEncType() == "multipart/form-data";- Returns:
trueif 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 aResponseusing theResponse.extractForm(int)method.- Overrides:
setCharEncodingin classHtmlElement- Parameters:
charEncoding- the character encoding to use when interpreting thisForm.- See Also:
HtmlElement.getCharEncoding()
-
-