AbstractHtmlUnitSetFormFieldTag.java from Jameleon at Krugle
Show AbstractHtmlUnitSetFormFieldTag.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 com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.SubmittableElement;
import net.sf.jameleon.plugin.htmlunit.HtmlUnitFunctionTag;
/**
* An abstract class that is used to help set all <input/> form fields.
*/
public abstract class AbstractHtmlUnitSetFormFieldTag extends HtmlUnitFunctionTag{
/**
* The identifier of the form.
* Value indentifiers are, the name, id, index or XPath expression
* Only the form or xpath attribute can be set at once
* @jameleon.attribute
*/
protected String form;
/**
* The field name in the form.
* @jameleon.attribute
*/
protected String fieldName;
/**
* The value to set the text field to
* @jameleon.attribute
*/
protected String value;
/**
* The xpath that matches the desired text field
* @jameleon.attribute
*/
protected String xpath;
public void testBlock(){
if (xpath != null) {
SubmittableElement input = getSubmitableElementByXPath(xpath);
setFieldValueFoundByXPath(input);
}else if (form != null) {
assertNotNull("If you specify a form, you must also set the fieldName to set the value to", fieldName);
setWorkingForm(form);
setFieldValueInForm();
}else{
fail("You must specify either an xpath or a form");
}
}
protected SubmittableElement getSubmitableElementByXPath(String xpath){
HtmlElement element = getHtmlElementByXPath(xpath);
SubmittableElement field = null;
assertNotNull("Could not find the input element defined by '"+xpath+"'", element);
assertTrue("The provided xpath '"+xpath+"' did not return a valid form field",element instanceof SubmittableElement);
field = (SubmittableElement) element;
return field;
}
protected abstract void setFieldValueFoundByXPath(SubmittableElement formField);
protected abstract void setFieldValueInForm();
}
See more files for this project here