View Javadoc

1   /*
2       Jameleon Watij plug-in - A plug-in that uses Watij (http://www.watij.com/) to drive web sites
3       Copyright (C) 2008 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program 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
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19  */
20  package net.sf.jameleon.plugin.watij.tags;
21  
22  import net.sf.jameleon.exception.JameleonScriptException;
23  
24  import watij.elements.SelectList;
25  
26  /***
27   * Selects a given option of a select list.
28   * 
29   * For example, set a select field by the displayed text option (<option>two</option>):
30   * 
31   * <pre><source>
32   * &lt;testcase xmlns="jelly:jameleon" xmlns:j="jelly:core"&gt;
33   *     &lt;watij-session baseUrl="http://some.url/some/path" beginSession="true"&gt;
34   *         &lt;watij-set-select-list
35   *             functionId="Set the 'two' option of a select field"
36   *             how="name"
37   *             what="selectFieldName"
38   *             select="two"/>
39   *     &lt;/watij-session&gt;
40   * &lt;/testcase&gt;
41   * </source></pre>
42   * 
43   * To select the option by its value attribute setting (&lt;option value="2"&gt;two&lt;/option&gt;)
44   * 
45   * <pre><source>
46   * &lt;testcase xmlns="jelly:jameleon" xmlns:j="jelly:core"&gt;
47   *     &lt;watij-session baseUrl="http://some.url/some/path" beginSession="true"&gt;
48   *         &lt;watij-set-select-list
49   *             functionId="Set the 'two' option of a select field"
50   *             how="xpath"
51   *             what="//SELECT[@name='selectFieldName']"
52   *             selectValue="2"/>
53   *     &lt;/watij-session&gt;
54   * &lt;/testcase&gt;
55   * </source></pre>
56   * 
57   * To clear the selected option(s):
58   * 
59   * <pre><source>
60   * &lt;testcase xmlns="jelly:jameleon" xmlns:j="jelly:core"&gt;
61   *     &lt;watij-session baseUrl="http://some.url/some/path" beginSession="true"&gt;
62   *         &lt;watij-set-select-list
63   *             functionId="Clear the selected options"
64   *             how="xpath"
65   *             what="//SELECT[@name='selectFieldName']"
66   *             clearSelection="true"/>
67   *     &lt;/watij-session&gt;
68   * &lt;/testcase&gt;
69   * </source></pre>
70   * 
71   * To select multiple values (2, and 3) against a multi select list:
72   * 
73   * <pre><source>
74   * &lt;testcase xmlns="jelly:jameleon" xmlns:j="jelly:core"&gt;
75   *     &lt;watij-session baseUrl="http://some.url/some/path" beginSession="true"&gt;
76   *         &lt;watij-set-select-list
77   *             functionId="Set the 'two' option of a select field"
78   *             how="xpath"
79   *             what="//SELECT[@name='selectFieldName']"
80   *             selectValue="2"/>
81   *         &lt;watij-set-select-list
82   *             functionId="Set the 'two' option of a select field"
83   *             how="name"
84   *             what="selectFieldName"
85   *             select="three"/>
86   *     &lt;/watij-session&gt;
87   * &lt;/testcase&gt;
88   * </source></pre>
89   * 
90   * See the javadocs on watij's {@link  watij.finders.SymbolFactory SymbolFactory} for a complete list of supported symbols.
91   * @jameleon.function name="watij-set-select-list" type="action"
92   * @jameleon.step Find the matching select field
93   * @jameleon.step Verify it exists
94   * @jameleon.step Select the desired option
95   */
96  public class WatijSetSelectListTag extends WatijSelectListTag{
97  
98      /***
99       * Select the option with the matching displayed text
100      * @jameleon.attribute
101      */
102     protected String select;
103     /***
104      * Select the option with the matching value attribute's value
105      * @jameleon.attribute
106      */
107     protected String selectValue;
108     /***
109      * Clear the optioh selection. If this is true and select or selectValue 
110      * are set, then the list will be cleared first, and the new option will 
111      * be selected. This option is for multi select boxes only.
112      * @jameleon.attribute default="false"
113      */
114     protected boolean clearSelection;
115 
116     public void testBlock() throws Exception{
117         super.testBlock();
118         SelectList sl = (SelectList)getHtmlElement();
119         if (clearSelection) {
120             sl.clearSelection();
121         }
122         if (select != null && selectValue == null) {
123             sl.select(select);
124         }else if (select == null && selectValue != null) {
125             sl.selectValue(selectValue);
126         }else if (selectValue != null && selectValue != null) {
127             throw new JameleonScriptException("Both 'select' and 'selectValue' attributes can not be set! Please set only one.");
128         }
129     }
130 
131 }