01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.verify;
03:
04: import java.io.IOException;
05: import java.util.List;
06:
07: import com.canoo.webtest.engine.StepFailedException;
08: import com.canoo.webtest.util.ConversionUtil;
09: import com.canoo.webtest.util.FormUtil;
10: import com.canoo.webtest.util.HtmlConstants;
11: import com.gargoylesoftware.htmlunit.html.HtmlElement;
12: import com.gargoylesoftware.htmlunit.html.HtmlForm;
13: import com.gargoylesoftware.htmlunit.html.HtmlInput;
14:
15: /**
16: * @author Unknown
17: * @author Marc Guillemot
18: * @author Paul King
19: * @webtest.step category="Core"
20: * name="verifyInputField"
21: * alias="verifyinputfield"
22: * description="This step verifies the value of a particular input field.
23: * It is typically used to check that input field defaults are correctly set."
24: */
25: public class VerifyInputField extends AbstractVerifyFormStep {
26: private String fRegex;
27:
28: public String getRegex() {
29: return fRegex;
30: }
31:
32: /**
33: * @param regex
34: * @webtest.parameter
35: * required="no"
36: * default="'false'"
37: * description="Specifies whether the value represents a <key>regex</key>."
38: */
39: public void setRegex(final String regex) {
40: fRegex = regex;
41: }
42:
43: protected HtmlForm findForm() {
44: return FormUtil.findFormForField(getContext(), getFormName(),
45: HtmlConstants.INPUT, null, getName(), this );
46: }
47:
48: protected List findFields(HtmlForm form) {
49: return form.getInputsByName(getName());
50: }
51:
52: protected void verifyField(final HtmlElement field)
53: throws IOException {
54: final HtmlInput input = (HtmlInput) field;
55: final String fieldContents = input.getValueAttribute();
56: if (!verifyStrings(getValue(), fieldContents, getRegex())) {
57: throw new StepFailedException(
58: "Wrong contents found in input field: " + input,
59: getValue(), fieldContents);
60: }
61:
62: }
63:
64: private static boolean verifyStrings(String expectedValue,
65: String actualValue, String regex) {
66: return getVerifier(
67: ConversionUtil.convertToBoolean(regex, false))
68: .verifyStrings(expectedValue, actualValue);
69: }
70:
71: protected void verifyParameters() {
72: super .verifyParameters();
73: paramCheck(isValueNull(),
74: "Required parameter \"value\" not set!");
75: }
76:
77: }
|