001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.verify;
003:
004: import com.canoo.webtest.engine.StepFailedException;
005: import com.canoo.webtest.extension.StoreElementAttribute;
006: import com.canoo.webtest.steps.Step;
007: import com.canoo.webtest.steps.form.AbstractSetFieldStep;
008: import com.gargoylesoftware.htmlunit.html.HtmlElement;
009: import com.gargoylesoftware.htmlunit.html.HtmlForm;
010: import org.apache.log4j.Logger;
011:
012: import java.io.IOException;
013: import java.util.List;
014:
015: /**
016: * @author Marc Guillemot
017: * @author Paul King
018: * @author Denis N. Antonioli
019: */
020: public abstract class AbstractVerifyFormStep extends Step {
021: private static final Logger LOG = Logger
022: .getLogger(AbstractVerifyFormStep.class);
023:
024: private String fName;
025: private String fValue;
026: private String fFormName;
027: private String fFieldIndex;
028: private String fHtmlId;
029: private String fXPath;
030:
031: /**
032: * @param name
033: * @webtest.parameter
034: * required="yes"
035: * description="The xpath of the input field of interest. One of <em>name</em>, <em>htmlId</em> or <em>xpath</em> is required."
036: */
037: public void setName(final String name) {
038: fName = name;
039: }
040:
041: public String getName() {
042: return fName;
043: }
044:
045: /**
046: * @param value
047: * @webtest.parameter
048: * required="no"
049: * description="The value of the input field of interest."
050: */
051: public void setValue(final String value) {
052: fValue = value;
053: }
054:
055: public String getValue() {
056: return fValue;
057: }
058:
059: /**
060: * @param formName
061: * @webtest.parameter
062: * required="no"
063: * description="The name of the form containing the field of interest."
064: */
065: public void setFormName(final String formName) {
066: fFormName = formName;
067: }
068:
069: public String getFormName() {
070: return fFormName;
071: }
072:
073: /**
074: * @param index
075: * @webtest.parameter
076: * required="no"
077: * description="The index (starting at 0) of the field of interest (if more than one)."
078: */
079: public void setFieldIndex(final String index) {
080: fFieldIndex = index;
081: }
082:
083: public String getFieldIndex() {
084: return fFieldIndex;
085: }
086:
087: /**
088: * Set the xpath.
089: *
090: * @param xpath
091: * @webtest.parameter required="yes/no"
092: * description="The xpath of the input field of interest. One of <em>name</em>, <em>htmlId</em> or <em>xpath</em> is required."
093: */
094: public void setXpath(final String xpath) {
095: fXPath = xpath;
096: }
097:
098: public String getXpath() {
099: return fXPath;
100: }
101:
102: /**
103: * Set the html id.
104: *
105: * @param htmlId
106: * @webtest.parameter required="yes/no"
107: * description="The id of the input field of interest. One of <em>name</em>, <em>htmlId</em> or <em>xpath</em> is required."
108: */
109: public void setHtmlId(final String htmlId) {
110: fHtmlId = htmlId;
111: }
112:
113: public String getHtmlId() {
114: return fHtmlId;
115: }
116:
117: public void doExecute() throws IOException {
118: if (getName() != null) {
119: final HtmlForm form = findForm();
120: if (form == null) {
121: throw new StepFailedException(
122: "No suitable form found having field named \""
123: + getName() + "\"", this );
124: }
125: verifyField(AbstractSetFieldStep.selectField(
126: findFields(form), getFieldIndex(), this ));
127: } else {
128: verifyField(StoreElementAttribute.findElement(getContext()
129: .getCurrentResponse(), getHtmlId(), getXpath(),
130: LOG, this ));
131: }
132: }
133:
134: /**
135: * Finds the relevant form.
136: *
137: */
138: protected abstract HtmlForm findForm();
139:
140: /**
141: * Finds then verifies the field of interest.
142: * @param form
143: */
144: protected abstract List findFields(final HtmlForm form);
145:
146: /**
147: * Verifies a field according to the step.
148: * It is up to the step's implementation to decide how to verify the step.
149: *
150: * @param field The field to verify.
151: */
152: protected abstract void verifyField(final HtmlElement field)
153: throws IOException;
154:
155: protected void verifyParameters() {
156: super .verifyParameters();
157: nullResponseCheck();
158:
159: int count = 0;
160: if (getXpath() != null) {
161: count++;
162: }
163: if (getName() != null) {
164: count++;
165: }
166: if (getHtmlId() != null) {
167: count++;
168: }
169: paramCheck(count == 0,
170: AbstractSetFieldStep.MESSAGE_ARGUMENT_MISSING);
171: paramCheck(count > 1,
172: AbstractSetFieldStep.MESSAGE_ARGUMENT_REDUNDANT);
173: if (getName() == null) {
174: paramCheck(getFieldIndex() != null,
175: "The attribute 'fieldIndex' is only valid with the attribute 'name'.");
176: } else {
177: optionalIntegerParamCheck(getFieldIndex(), "fieldIndex",
178: false);
179: }
180: }
181:
182: protected boolean isValueNull() {
183: return getValue() == null;
184: }
185: }
|