01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.form;
03:
04: import java.util.List;
05:
06: import org.apache.log4j.Logger;
07:
08: import com.canoo.webtest.engine.StepFailedException;
09: import com.canoo.webtest.util.FormUtil;
10: import com.gargoylesoftware.htmlunit.html.HtmlElement;
11: import com.gargoylesoftware.htmlunit.html.HtmlForm;
12: import com.gargoylesoftware.htmlunit.html.HtmlInput;
13: import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
14: import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
15: import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
16:
17: /**
18: * Sets the value of a text field (inputs of type 'text' and 'password' as well as textareas).
19: *
20: * @author Marc Guillemot
21: * @author Paul King
22: * @webtest.step
23: * category="Core"
24: * name="setInputField"
25: * alias="new_setinputfield,setinputfield"
26: * description="Provides the ability to update text-oriented input fields in <key>HTML</key> forms (supports input fields of type <em>text</em> and <em>password</em> as well as <em>textareas</em>)."
27: */
28: public class SetInputField extends AbstractSetNamedFieldStep {
29: private static final Logger LOG = Logger
30: .getLogger(SetInputField.class);
31:
32: protected HtmlForm findForm() {
33: return FormUtil.findFormForTextField(getContext(),
34: getFormName(), getName(), this );
35: }
36:
37: /**
38: * Finds all text and password inputs or textareas with the given name in the form.
39: *
40: * @param form The form to search
41: * @return The list of fields with the given name
42: */
43: protected List findFields(final HtmlForm form) {
44: final List fields = super .findFields(form);
45: List textAreaFields = form.getTextAreasByName(getName());
46: LOG.debug("Found " + textAreaFields.size()
47: + " textarea field(s)");
48: fields.addAll(textAreaFields);
49: return fields;
50: }
51:
52: protected boolean keepField(HtmlElement elt) {
53: return (elt instanceof HtmlTextInput)
54: || (elt instanceof HtmlPasswordInput)
55: || (elt instanceof HtmlTextArea);
56: }
57:
58: protected void setField(HtmlElement elt) {
59: if (elt instanceof HtmlTextArea) {
60: ((HtmlTextArea) elt).setText(getValue());
61: LOG.debug("Set text for textarea " + elt + " to value "
62: + getValue());
63: } else if (elt instanceof HtmlInput) {
64: ((HtmlInput) elt).setValueAttribute(getValue());
65: LOG.debug("Set text for input " + elt + " to value "
66: + getValue());
67: } else {
68: throw new StepFailedException("Found " + elt.getTagName()
69: + " when looking for input", this );
70: }
71: }
72:
73: protected void verifyParameters() {
74: super.verifyParameters();
75: paramCheck(isValueNull(), DEFAULT_VALUE_NULL_MESSAGE);
76: }
77: }
|