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   * A generic validate tag used to validate HTML.
25   * If no attributes are set, then no validation occurs. If
26   * no validation occurs, then this tag will fail.
27   * <br/>
28   * To validate a page with the title <b>Yo World!</b>:
29   * <pre><source>
30   *     &lt;htmlunit-validate
31   *            functionId="Verify that the title is 'Yo World!'"
32   *            title="Yo World!"/>
33   * </source></pre>
34   * If you want a better error message when a failure occurs, then pass in
35   * the <b>msg</b> attribute.
36   * <pre><source>
37   *     &lt;htmlunit-validate
38   *            functionId="Verify that the title is 'Yo World!'"
39   *            title="Yo World!"
40   *            msg="We must be on the wrong page."/>
41   * </source></pre>
42   * If you want validate that some text simple exists on the page anywhere:
43   * <pre><source>
44   *     &lt;htmlunit-validate
45   *            functionId="Verify that 'some text' exists on the current page"
46   *            textPresent="some text"/>
47   * </source></pre>
48   * If you want to use xpath to validate, then simply use the <b>xpath</b> attribute:
49   * <pre><source>
50   *     &lt;htmlunit-validate
51   *            functionId="Verify that 'some text' exists on the current page"
52   *            xpath="//body/table[2]/tr[@id='checking_account' and text=()='234333433']"/>
53   * </source></pre>
54   * You can, supply multiple attributes. If you supply multiple attributes
55   * and the <b>msg</b> attribute, then the <b>msg</b> will be used on any failure.
56   * @jameleon.function name="htmlunit-validate" type="validation"
57   */
58  public class HtmlUnitValidateTag extends HtmlUnitFunctionTag{
59  
60      /***
61       * The expected title of the page
62       * @jameleon.attribute
63       */
64      protected String title;
65      /***
66       * The expected text in the page
67       * @jameleon.attribute
68       */
69      protected String textPresent;
70      /***
71       * The xpath to evaluate
72       * @jameleon.attribute
73       */
74      protected String xpath;
75      /***
76       * The message to display in if a failure occurs
77       * @jameleon.attribute
78       */
79      protected String msg;
80  
81      public void testBlock(){
82          boolean assertOccured = false;
83          if (title != null) {
84              assertOccured = true;
85              assertTitleEquals(getMessage("Title"), title);
86          }
87          if (textPresent != null) {
88              assertOccured = true;
89              assertTextPresent(getMessage("Text '"+textPresent+"' not found on current page"), textPresent);
90          }
91          if (xpath != null) {
92              assertOccured = true;
93              assertXPathMatches(getMessage("XPath '"+xpath+"' not found on current page"), xpath);
94          }
95          assertTrue("No tests occured", assertOccured);
96      }
97  
98      private String getMessage(String defaultMsg){
99          String returnMsg = defaultMsg;
100         if (msg != null) {
101             returnMsg = msg;
102         }
103         return returnMsg;
104     }
105 }