1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 * <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 * <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 * <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 * <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 }