HtmlUnitValidateTag.java from Jameleon at Krugle
Show HtmlUnitValidateTag.java syntax highlighted
/*
Jameleon HtmlUnit plug-in - A plug-in that uses HtmlUnit to drive web sites
Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
*/
package net.sf.jameleon.plugin.htmlunit.tags;
import net.sf.jameleon.plugin.htmlunit.HtmlUnitFunctionTag;
/**
* A generic validate tag used to validate HTML.
* If no attributes are set, then no validation occurs. If
* no validation occurs, then this tag will fail.
* <br/>
* To validate a page with the title <b>Yo World!</b>:
* <pre><source>
* <htmlunit-validate
* functionId="Verify that the title is 'Yo World!'"
* title="Yo World!"/>
* </source></pre>
* If you want a better error message when a failure occurs, then pass in
* the <b>msg</b> attribute.
* <pre><source>
* <htmlunit-validate
* functionId="Verify that the title is 'Yo World!'"
* title="Yo World!"
* msg="We must be on the wrong page."/>
* </source></pre>
* If you want validate that some text simple exists on the page anywhere:
* <pre><source>
* <htmlunit-validate
* functionId="Verify that 'some text' exists on the current page"
* textPresent="some text"/>
* </source></pre>
* If you want to use xpath to validate, then simply use the <b>xpath</b> attribute:
* <pre><source>
* <htmlunit-validate
* functionId="Verify that 'some text' exists on the current page"
* xpath="//body/table[2]/tr[@id='checking_account' and text=()='234333433']"/>
* </source></pre>
* You can, supply multiple attributes. If you supply multiple attributes
* and the <b>msg</b> attribute, then the <b>msg</b> will be used on any failure.
* @jameleon.function name="htmlunit-validate" type="validation"
*/
public class HtmlUnitValidateTag extends HtmlUnitFunctionTag{
/**
* The expected title of the page
* @jameleon.attribute
*/
protected String title;
/**
* The expected text in the page
* @jameleon.attribute
*/
protected String textPresent;
/**
* The xpath to evaluate
* @jameleon.attribute
*/
protected String xpath;
/**
* The message to display in if a failure occurs
* @jameleon.attribute
*/
protected String msg;
public void testBlock(){
boolean assertOccured = false;
if (title != null) {
assertOccured = true;
assertTitleEquals(getMessage("Title"), title);
}
if (textPresent != null) {
assertOccured = true;
assertTextPresent(getMessage("Text '"+textPresent+"' not found on current page"), textPresent);
}
if (xpath != null) {
assertOccured = true;
assertXPathMatches(getMessage("XPath '"+xpath+"' not found on current page"), xpath);
}
assertTrue("No tests occured", assertOccured);
}
private String getMessage(String defaultMsg){
String returnMsg = defaultMsg;
if (msg != null) {
returnMsg = msg;
}
return returnMsg;
}
}
See more files for this project here