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;
20  
21  
22  import java.io.IOException;
23  
24  import junit.framework.Assert;
25  import net.sf.jameleon.function.FunctionTag;
26  import net.sf.jameleon.plugin.htmlunit.util.HtmlUnitHelper;
27  import net.sf.jameleon.util.AssertLevel;
28  import net.sf.jameleon.util.JameleonUtility;
29  
30  import com.gargoylesoftware.htmlunit.html.HtmlElement;
31  import com.gargoylesoftware.htmlunit.html.HtmlForm;
32  import com.gargoylesoftware.htmlunit.html.HtmlInput;
33  import com.gargoylesoftware.htmlunit.html.HtmlPage;
34  
35  public abstract class HtmlUnitFunctionTag extends FunctionTag{
36  
37      protected HtmlUnitHelper helper;
38      protected HtmlUnitSessionTag sessionTag;
39      protected HtmlForm workingForm;
40  
41      /***
42       * Navigates to the provided URL
43       * @param url - the url to navigate to.
44       */
45      public void navigate(String url){
46          helper.navigate(url);
47      }
48  
49      /***
50       * Gets the references of the session and the test results from the parent TestCase
51       * This method gets called once all attributes are set from the macro language. Any required
52       * setup should go here.
53       */
54      public void setupEnvironment() {
55          super.setupEnvironment();
56          Object obj = findAncestorWithClass(HtmlUnitSessionTag.class);
57          if (obj instanceof HtmlUnitSessionTag) {
58              sessionTag = (HtmlUnitSessionTag) obj;
59          } else {
60              throw new ClassCastException("Can only execute an HtmlUnit function tag under the HtmlUnit session tag ( htmlunit-session )! " +
61                      "Please change the session tag surrounding function point " + getClass().getName());
62          }
63          helper = new HtmlUnitHelper(sessionTag);
64      }
65  
66      public void store(String fName, int event) throws IOException{
67          getFunctionResults().setErrorFile(helper.store(fName));
68      }
69  
70      // ASSERT METHODS
71  
72  
73      /***
74       * Validates that the provided text exists on the page.
75       *
76       * @param expectedText expected text
77       */
78      public void assertTextPresent(final String expectedText) {
79          String msg = "assertTextPresent: ";
80          int assertLevel = AssertLevel.NO_LEVEL;
81          assertTextPresent(msg, expectedText, assertLevel);
82      }                                                                       
83  
84      /***
85       * Validates that the title of the current html page matches the expected title
86       * If the test fails display the given <code>message</code>
87       *
88       * @param msg  - The message to display if <code>text</code> is not in the response.
89       * @param expectedText expected text
90       */
91      public void assertTextPresent(final String msg, final String expectedText) {
92          int assertLevel = AssertLevel.NO_LEVEL;
93          assertTextPresent(msg, expectedText, assertLevel);
94      }                                                                       
95  
96      /***
97       * Validates that the title of the current html page matches the expected title
98       *
99       * @param expectedText expected text
100      * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
101      */
102     public void assertTextPresent(final String expectedText, int assertLevel) {
103         String msg = "assertTextPresent: ";
104         assertTextPresent(msg, expectedText, assertLevel);
105     }                                                                       
106 
107     /***
108      * Validates that the title of the current html page matches the expected title
109      * If the test fails display the given <code>message</code>
110      *
111      * @param msg  - The message to display if <code>text</code> is not in the response.
112      * @param expectedText expected text
113      * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
114      */
115     public void assertTextPresent(final String msg, final String expectedText, int assertLevel) {
116         if ( expectedText == null ) {
117             fail("assertTextPresent: expectedText was null!", assertLevel);
118         }
119         assertMethodWithLevel(new Runnable() {
120                                   public void run() {
121                                       String html = helper.getCurrentPageContent();
122                                       if (html != null) {
123                                           Assert.assertTrue(JameleonUtility.createErrMsg(msg), html.indexOf(expectedText) >= 0);
124                                       }else{
125                                           fail("There is no current page to validate");
126                                       }
127                                   }
128                               }, assertLevel);
129     }                                                                       
130 
131     /***
132      * Validates that the title of the current html page matches the expected title
133      *
134      * @param expectedTitle expected title value
135      */
136     public void assertTitleEquals(final String expectedTitle) {
137         String msg = "assertTitleEquals: ";
138         int assertLevel = AssertLevel.NO_LEVEL;
139         assertTitleEquals(msg, expectedTitle, assertLevel);
140     }                                                                       
141 
142     /***
143      * Validates that the title of the current html page matches the expected title
144      * If the test fails display the given <code>message</code>
145      *
146      * @param msg  - The message to display if <code>text</code> is not in the response.
147      * @param expectedTitle expected title value
148      */
149     public void assertTitleEquals(final String msg, final String expectedTitle) {
150         int assertLevel = AssertLevel.NO_LEVEL;
151         assertTitleEquals(msg, expectedTitle, assertLevel);
152     }                                                                       
153 
154     /***
155      * Validates that the title of the current html page matches the expected title
156      *
157      * @param expectedTitle expected title value
158      * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
159      */
160     public void assertTitleEquals(final String expectedTitle, int assertLevel) {
161         String msg = "assertTitleEquals: ";
162         assertTitleEquals(msg, expectedTitle, assertLevel);
163     }                                                                       
164 
165     /***
166      * Validates that the title of the current html page matches the expected title
167      * If the test fails display the given <code>message</code>
168      *
169      * @param msg  - The message to display if <code>text</code> is not in the response.
170      * @param expectedTitle expected title value
171      * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
172      */
173     public void assertTitleEquals(final String msg, final String expectedTitle, int assertLevel) {
174         if ( expectedTitle == null ) {
175             fail("assertTitleEquals: Title was null!", assertLevel);
176         }
177         
178         assertMethodWithLevel(new Runnable() {
179                                   public void run() {
180                                       HtmlPage page = (HtmlPage)helper.getCurrentPage();
181                                       if (page != null) {
182                                           Assert.assertEquals(
183                                               JameleonUtility.createErrMsg(msg),
184                                               expectedTitle,
185                                               page.getTitleText());
186                                       }else{
187                                           fail("There is no current page to validate");
188                                       }
189                                   }
190                               }, assertLevel);
191     }                                                                       
192 
193     /***
194      * Validates that the provided xpath matches against the currently active page
195      *
196      * @param xpath The XPath to match
197      */
198     public void assertXPathMatches(final String xpath) {
199         String msg = "assertXPathMatches: ";
200         int assertLevel = AssertLevel.NO_LEVEL;
201         assertXPathMatches(msg, xpath, assertLevel);
202     }                                                                       
203 
204     /***
205      * Validates that the provided xpath matches against the currently active page
206      * If the test fails display the given <code>message</code>
207      *
208      * @param msg  - The message to display if <code>text</code> is not in the response.
209      * @param xpath The XPath to match
210      */
211     public void assertXPathMatches(final String msg, final String xpath) {
212         int assertLevel = AssertLevel.NO_LEVEL;
213         assertXPathMatches(msg, xpath, assertLevel);
214     }                                                                       
215 
216     /***
217      * Validates that the provided xpath matches against the currently active page
218      *
219      * @param xpath The XPath to match
220      * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
221      */
222     public void assertXPathMatches(final String xpath, int assertLevel) {
223         String msg = "assertXPathMatches: ";
224         assertXPathMatches(msg, xpath, assertLevel);
225     }                                                                       
226 
227     /***
228      * Validates that the provided xpath matches against the currently active page
229      * If the test fails display the given <code>message</code>
230      *
231      * @param msg  - The message to display if <code>text</code> is not in the response.
232      * @param xpath The XPath to match
233      * @param assertLevel - Only run this test under the assertLevel as described in {@link net.sf.jameleon.util.AssertLevel}
234      */
235     public void assertXPathMatches(final String msg, final String xpath, int assertLevel) {
236         if ( xpath == null ) {
237             fail("assertXPathMatches: xpath was null!", assertLevel);
238         }
239         assertMethodWithLevel(new Runnable() {
240                                   public void run() {
241                                       Assert.assertTrue(msg, helper.xPathMatches(xpath));
242                                   }
243                               }, assertLevel);
244     }  
245 
246     // Helper methods.
247 
248     /***
249      *  Clicks on the HTML element matching the provided XPath 
250      * @param xpath - the XPath expression that defines the HTML element to click.
251      */
252     public void clickElementWithXPath(String xpath){
253         helper.clickElementWithXPath(xpath);
254     }
255 
256     /***
257      * This is a power method to get a form element by the following order.
258      * <ol>
259      *     <li>getHtmlFormById</li>
260      *     <li>getHtmlFormByName</li>
261      *     <li>getHtmlFormByIndex</li>
262      *     <li>getHtmlElementByXPath</li>
263      * </ol>
264      * @param idNameIndexOrXpath - The id, name, index or XPath expression defining the location of the form
265      * 
266      * @return HtmlForm
267      */
268     public HtmlForm getHtmlForm(String idNameIndexOrXpath){
269         HtmlForm form = null;
270         if (idNameIndexOrXpath == null) {
271             fail("idNameIndexOrXpath must not be null");
272         }
273         form = getHtmlFormById(idNameIndexOrXpath);
274         if (form == null) {
275             form = getHtmlFormByName(idNameIndexOrXpath);
276             if (form == null) {
277                 try{
278                     int index = Integer.parseInt(idNameIndexOrXpath);
279                     form = getHtmlFormByIndex(index);
280                 }catch (NumberFormatException nfe){
281                     //Do nothing. This just means a number wasn't passed.
282                 }
283                 if (form == null) {
284                     form = getHtmlFormByXPath(idNameIndexOrXpath);
285                 }
286             }
287         }
288         return form;
289     }
290 
291     /***
292      * Gets a form element back by its id attribute.
293      * @param attributeValue - the value of the id attribute.
294      * 
295      * @return HtmlForm
296      */
297     public HtmlForm getHtmlFormById(String attributeValue){
298         return helper.getHtmlFormById(attributeValue);
299     }
300 
301     /***
302      * Gets a form element back by its index or location on the page.
303      * For the first form on the page, pass in '1'
304      * @param index - the nth form on the page
305      * 
306      * @return HtmlForm
307      */
308     public HtmlForm getHtmlFormByIndex(int index){
309         return helper.getHtmlFormByIndex(index);
310     }
311 
312     /***
313      * Gets a form element back by its name attribute.
314      * @param attributeValue - the value of the name attribute.
315      * 
316      * @return HtmlForm
317      */
318     public HtmlForm getHtmlFormByName(String attributeValue){
319         return helper.getHtmlFormByName(attributeValue);
320     }
321 
322     /***
323      * Gets a form element back by an XPath expression
324      * @param xpath - The XPath expression matching the desired form
325      * 
326      * @return HtmlForm
327      */
328     public HtmlForm getHtmlFormByXPath(String xpath){
329         return helper.getHtmlFormByXPath(xpath);
330     }
331 
332     /***
333      * Gets an HtmlElement matching the provided xpath expression
334      * @param xpath - An XPath expression that matches the desired HtmlElement
335      * 
336      * @return The matching HtmlElement
337      */
338     public HtmlElement getHtmlElementByXPath(String xpath){
339         return helper.getHtmlElementByXPath(xpath);
340     }
341 
342     /***
343      * Gets an HTMLElement by the tag name, attribute name and the attribute value.
344      * This then in turn gets translated to an XPath expression.
345      * An example of this might be to get a form with its id='ten'.
346      * @param tagname - The name of the tag to get back. In the above example, the value would be 'form'
347      * @param attributeName - The name of attribute to check against. In the above example, the value would be 'id'
348      * @param attributeValue - The value of the attribute to check against. In the above example, the value would be 'ten'
349      * 
350      * @return HtmlElement - An element that matches the criteria.
351      */
352     public HtmlElement getHtmlElementByAttributeNameAndValue(String tagname, String attributeName, String attributeValue){
353         return helper.getHtmlElementByAttributeNameAndValue(tagname, attributeName, attributeValue);
354     }
355 
356     /***
357      * Gets the current working form or null if one hasn't been set.
358      * 
359      * @return HtmlForm - The current working form
360      */
361     public HtmlForm getWorkingForm(){
362         return workingForm;
363     }
364 
365     /***
366      * Checks or unchecks the checkbox that either exists in the workingForm or in the current page
367      * @param fieldName - The name of the checkbox to check
368      * @param checked - set to <code>true</code> to check the checkbox and to <code>false</code> to uncheck it
369      */
370     public void setCheckBox(String fieldName, boolean checked){
371         helper.setCheckBox(fieldName, checked);
372     }
373 
374     /***
375      * Checks or unchecks the checkbox that either exists in the workingForm or in the current page
376      * @param fieldName - The name of the checkbox to check
377      * @param fieldValue - The value of the checkbox to check
378      * @param checked - set to <code>true</code> to check the checkbox and to <code>false</code> to uncheck it
379      */
380     public void setCheckBox(String fieldName, String fieldValue, boolean checked){
381         helper.setCheckBox(fieldName, fieldValue, checked);
382     }
383 
384     /***
385      * Sets the file field that either exists in the workingForm or in the current page
386      * @param fieldName - The name of the input field to set the value of
387      * @param value - The value to set the input field to
388      */
389     public void setFileField(String fieldName, String value){
390         helper.setFileField(fieldName, value);
391     }
392 
393     /***
394      * Sets the hidden field that either exists in the workingForm or in the current page
395      * @param fieldName - The name of the input field to set the value of
396      * @param value - The value to set the input field to
397      */
398     public void setHiddenField(String fieldName, String value){
399         helper.setHiddenField(fieldName, value);
400     }
401 
402     /***
403      * Sets the password field that either exists in the workingForm or in the current page
404      * @param fieldName - The name of the input field to set the value of
405      * @param value - The value to set the input field to
406      */
407     public void setPasswordField(String fieldName, String value){
408         helper.setPasswordField(fieldName, value);
409     }
410 
411     /***
412      * Checks or unchecks the radio button that either exists in the workingForm or in the current page.
413      * @param fieldName - The name of the radio button to check
414      * @param fieldValue - The value of the radio button to check
415      * @param checked - set to <code>true</code> to check the radio button and to <code>false</code> to uncheck it
416      */
417     public void setRadioButton(String fieldName, String fieldValue, boolean checked){
418         helper.setRadioButton(fieldName, fieldValue, checked);
419     }
420 
421     /***
422      * Selects or unselects the option by its index order that either exists in the workingForm or in the current page.
423      * @param fieldName - The name of the select field to select
424      * @param index - The nth option in the list. 1st option = 0
425      * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
426      */
427     public void setSelectFieldByIndex(String fieldName, int index, boolean selected){
428         helper.setSelectFieldByIndex(fieldName, index, selected);
429     }
430 
431     /***
432      * Selects or unselects the option with the displayed text that either exists in the workingForm or in the current page.
433      * @param fieldName - The name of the select field to select
434      * @param optionText - The text displayed in the drop-down
435      * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
436      */
437     public void setSelectFieldByOptionText(String fieldName, String optionText, boolean selected){
438         helper.setSelectFieldByOptionText(fieldName, optionText, selected);
439     }
440 
441     /***
442      * Selects or unselects the option with the given attribute value that either exists in the workingForm or in the current page.
443      * @param fieldName - The name of the select field to select
444      * @param valueAttribute - The value of option field to select
445      * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
446      */
447     public void setSelectFieldByValue(String fieldName, String valueAttribute, boolean selected){
448         helper.setSelectFieldByValue(fieldName, valueAttribute, selected);
449     }
450 
451     /***
452      * Selects or unselects the option defined by XPath.
453      * @param xpath - The XPath expression that matches an HtmlOption (not a select) to be selected
454      * @param selected - set to <code>true</code> to select and to <code>false</code> to unselect it
455      */
456     public void setSelectFieldByXPath(String xpath, boolean selected){
457         helper.setSelectFieldByXPath(xpath, selected);
458     }
459 
460     /***
461      * Sets the text area that either exists in the workingForm or in the current page
462      * @param fieldName - The name of the input field to set the value of
463      * @param value - The value to set the input field to
464      */
465     public void setTextArea(String fieldName, String value){
466         helper.setTextArea(fieldName, value);
467     }
468 
469     /***
470      * Sets the text field that either exists in the workingForm or in the current page
471      * @param fieldName - The name of the input field to set the value of
472      * @param value - The value to set the input field to
473      */
474     public void setTextField(String fieldName, String value){
475         helper.setTextField(fieldName, value);
476     }
477 
478     /***
479      * Sets an input field type's value
480      * @param input - The HtmlInput to set the value on
481      * @param value - The value to set the input element to.
482      * @param type - The input type. If this does not match the given field, then this method will fail
483      */
484     public void setHtmlInputValue(HtmlInput input, String value, String type){
485         helper.setHtmlInputValue(input, value, type);
486     }
487 
488     /***
489      * Sets an input field type's value
490      * @param xpath - An XPath expression that matches the desired HtmlInput Element.
491      * @param value - The value to set the input element to.
492      * @param type - The input type. If this does not match the given field, then this method will fail
493      */
494     public void setHtmlInputValueByXPath(String xpath, String value, String type){
495         helper.setHtmlInputValueByXPath(xpath, value, type);
496     }
497 
498     /***
499      * Sets the working form to form with the given id
500      * This is a power method to set a form element by the following order.
501      * <ol>
502      *     <li>getHtmlFormById</li>
503      *     <li>getHtmlFormByName</li>
504      *     <li>getHtmlFormByIndex</li>
505      *     <li>getHtmlElementByXPath</li>
506      * </ol>
507      * @param idNameIndexOrXpath - The id, name, index or XPath expression defining the location of the form
508      */
509     public void setWorkingForm(String idNameIndexOrXpath){
510         workingForm = getHtmlForm(idNameIndexOrXpath);
511     }
512 
513     /***
514      * Sets the working form to form with the given id
515      * @param id - The value of the form's HTML id attribute
516      */
517     public void setWorkingFormById(String id){
518         workingForm = getHtmlFormById(id);
519     }
520 
521     /***
522      * Sets the working form to nth form on the current page.
523      * This method is useful when the form doesn't have a name or id attribute
524      * @param index - The number of form on the page. The first form would be 1
525      */
526     public void setWorkingFormByIndex(int index){
527         workingForm = getHtmlFormByIndex(index);
528     }
529 
530     /***
531      * Sets the working form to nth form on the current page.
532      * @param name - The value of the name attribute in the form
533      */
534     public void setWorkingFormByName(String name){
535         workingForm = getHtmlFormByName(name);
536     }
537 
538     /***
539      * Sets the working form by the matching XPath expression
540      * @param xpath - The XPath expression that matches the form to use
541      */
542     public void setWorkingFormByXPath(String xpath){
543         workingForm = getHtmlFormByXPath(xpath);
544     }
545 
546 }