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 net.sf.jameleon.plugin.htmlunit.HtmlUnitFunctionTag;
22  
23  /***
24   * This tag is used to select or unselect options in a select field.
25   * <br/>
26   * <p>
27   * To use XPath to select a select field named sf_1 of an option with the value attribute of <b>sfo_1</b> that exists in a form with the name <b>testform</b>:<br/>
28   * <pre><source>
29   *     &lt;htmlunit-set-select-field
30   *            functionId="Select the select field option with the value 'sfo_1'"
31   *            xpath="//form[@name='testform']/select[@name='sf_1']/option[@value='sfo_1']"
32   *            selected="true"/&gt;
33   * </source></pre>
34   * </p>
35   * <p>
36   * To use XPath to unselect a sf_1 select field option with the value attribute of <b>sfo_1</b> that exists in a form with the name <b>testform</b>:
37   * <pre><source>
38   *     &lt;htmlunit-set-select-field
39   *            functionId="Select the select field option with the value 'sfo_1'"
40   *            xpath="//form[@name='testform']/select[@name='sf_1']/option[@value='sfo_1']"
41   *            selected="false"/&gt;
42   * </source></pre>
43   * </p>
44   * <p>
45   * To use the form, field name and option value to select an option with the value <b>sfo_1</b> that exists in a form with the name <b>testform</b>:
46   * <pre><source>
47   *     &lt;htmlunit-set-select-field
48   *            functionId="select the select field option with the value attribute of 'sfo_1'"
49   *            form="testform"
50   *            fieldName="sf_1"
51   *            optionValue="sfo_1"
52   *            selected="true"/&gt;
53   * </source></pre>
54   * </p>
55   * <p>
56   * To use the form, field name and option text to select an option by it's displayed text <b>Select Me</b> that exists in a form with the name <b>testform</b>:
57   * <pre><source>
58   *     &lt;htmlunit-set-select-field
59   *            functionId="select the select field option with the displayed text 'Select Me'"
60   *            form="testform"
61   *            fieldName="sf_1"
62   *            optionText="Select Me"
63   *            selected="true"/&gt;
64   * </source></pre>
65   * </p>
66   * <p>
67   * To use the form, field name and option text to select the 3rd option that exists in a form with the name <b>testform</b>:
68   * <pre><source>
69   *     &lt;htmlunit-set-select-field
70   *            functionId="select the third select field option"
71   *            form="testform"
72   *            fieldName="sf_1"
73   *            optionIndex="2"
74   *            selected="true"/&gt;
75   * </source></pre>
76   * </p>
77   * @jameleon.function name="htmlunit-set-select-field" type="action"
78   */
79  public class HtmlUnitSetSelectFieldTag extends HtmlUnitFunctionTag{
80  
81      /***
82       * The identifier of the form.
83       * Value indentifiers are, the name, id, index or XPath expression
84       * Only the form or xpath attribute can be set at once
85       * @jameleon.attribute
86       */
87      protected String form;
88      /***
89       * The field name in the form.
90       * @jameleon.attribute
91       */
92      protected String fieldName;
93      /***
94       * The the value of the option's value attribute
95       * @jameleon.attribute
96       */
97      protected String optionValue;
98      /***
99       * The position of the option to select. The first displayed option would be 1
100      * @jameleon.attribute default='-1'
101      */
102     protected int optionIndex;
103     /***
104      * The the displayed text of the option's value attribute
105      * @jameleon.attribute
106      */
107     protected String optionText;
108     /***
109      * The xpath that matches the desired <b>option</b> to select.
110      * @jameleon.attribute
111      */
112     protected String xpath;
113     /***
114      * The xpath that matches the desired <b>option</b> to select.
115      * @jameleon.attribute required="true"
116      */
117     protected Boolean selected;
118 
119     public void testBlock(){
120         boolean select = selected.booleanValue();
121         if (xpath != null) {
122             setSelectFieldByXPath(xpath, select);
123         }else if (form != null) {
124             assertNotNull("If you specify a form, you must also set the fieldName to set the value to", fieldName);
125             setWorkingForm(form);
126             if (optionValue != null) {
127                 setSelectFieldByValue(fieldName, optionValue, select);
128             }else if (optionText != null) {
129                 setSelectFieldByOptionText(fieldName, optionText, select);
130             }else if (optionIndex > -1) {
131                 setSelectFieldByIndex(fieldName, optionIndex, select);
132             }else{
133                 fail("If you specify a form and fieldName, you must specify optionValue, optionText or optionIndex!");
134             }
135         }else{
136             fail("You must specify either an xpath or a form");
137         }
138     }
139 
140 }