1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.plugin.htmlunit.tags;
20
21 import com.gargoylesoftware.htmlunit.html.HtmlElement;
22 import com.gargoylesoftware.htmlunit.html.SubmittableElement;
23
24 import net.sf.jameleon.plugin.htmlunit.HtmlUnitFunctionTag;
25
26 /***
27 * An abstract class that is used to help set all <input/> form fields.
28 */
29 public abstract class AbstractHtmlUnitSetFormFieldTag extends HtmlUnitFunctionTag{
30
31 /***
32 * The identifier of the form.
33 * Value indentifiers are, the name, id, index or XPath expression
34 * Only the form or xpath attribute can be set at once
35 * @jameleon.attribute
36 */
37 protected String form;
38 /***
39 * The field name in the form.
40 * @jameleon.attribute
41 */
42 protected String fieldName;
43 /***
44 * The value to set the text field to
45 * @jameleon.attribute
46 */
47 protected String value;
48 /***
49 * The xpath that matches the desired text field
50 * @jameleon.attribute
51 */
52 protected String xpath;
53
54 public void testBlock(){
55 if (xpath != null) {
56 SubmittableElement input = getSubmitableElementByXPath(xpath);
57 setFieldValueFoundByXPath(input);
58 }else if (form != null) {
59 assertNotNull("If you specify a form, you must also set the fieldName to set the value to", fieldName);
60 setWorkingForm(form);
61 setFieldValueInForm();
62 }else{
63 fail("You must specify either an xpath or a form");
64 }
65 }
66
67 protected SubmittableElement getSubmitableElementByXPath(String xpath){
68 HtmlElement element = getHtmlElementByXPath(xpath);
69 SubmittableElement field = null;
70 assertNotNull("Could not find the input element defined by '"+xpath+"'", element);
71 assertTrue("The provided xpath '"+xpath+"' did not return a valid form field",element instanceof SubmittableElement);
72 field = (SubmittableElement) element;
73 return field;
74 }
75
76 protected abstract void setFieldValueFoundByXPath(SubmittableElement formField);
77
78 protected abstract void setFieldValueInForm();
79 }