View Javadoc

1   /*
2       Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
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  }