01: // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.form;
03:
04: import com.canoo.webtest.engine.StepFailedException;
05: import com.canoo.webtest.util.ConversionUtil;
06: import com.canoo.webtest.util.FormUtil;
07: import com.canoo.webtest.util.HtmlConstants;
08: import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
09: import com.gargoylesoftware.htmlunit.html.HtmlElement;
10: import com.gargoylesoftware.htmlunit.html.HtmlForm;
11: import org.apache.log4j.Logger;
12:
13: import java.io.IOException;
14:
15: /**
16: * Provides the ability to check / uncheck HTML checkboxes.
17: *
18: * @author Marc Guillemot
19: * @webtest.step category="Core"
20: * name="setCheckbox"
21: * alias="new_setcheckbox,setcheckbox"
22: * description="Provides the ability to check and uncheck checkboxes in <key>HTML</key> forms."
23: */
24: public class SetCheckbox extends AbstractSetNamedFieldStep {
25: private static final Logger LOG = Logger
26: .getLogger(SetCheckbox.class);
27: private String fChecked;
28:
29: protected HtmlForm findForm() {
30: return FormUtil.findFormForField(getContext(), getFormName(),
31: HtmlConstants.INPUT, HtmlConstants.CHECKBOX, getName(),
32: this );
33: }
34:
35: protected boolean keepField(final HtmlElement elt) {
36: if (!(elt instanceof HtmlCheckBoxInput)) {
37: return false;
38: }
39: return isValueNull()
40: || getValue().equals(
41: ((HtmlCheckBoxInput) elt).getValueAttribute());
42: }
43:
44: /**
45: * Set the value.
46: *
47: * @param value
48: * @webtest.parameter required="no"
49: * description="The value to use to select the desired checkbox. If not present, the first possible checkbox is selected."
50: */
51: public void setValue(final String value) {
52: super .setValue(value);
53: }
54:
55: /**
56: * Updates a field.
57: *
58: * @param field The field to update
59: */
60: protected void setField(final HtmlElement field) throws IOException {
61: if (!(field instanceof HtmlCheckBoxInput)) {
62: throw new StepFailedException("Found " + field.getTagName()
63: + " when looking for checkbox", this );
64: }
65: final boolean checked = isChecked();
66: final HtmlCheckBoxInput checkBox = (HtmlCheckBoxInput) field;
67: if (checkBox.isChecked() != checked) {
68: checkBox.click();
69: LOG.debug((checked ? "" : "un") + "checked checkbox "
70: + checkBox);
71: }
72: }
73:
74: /**
75: * Gets the boolean value of the argument {@link #setChecked(String)}.
76: * @return The boolean value.
77: */
78: private boolean isChecked() {
79: return ConversionUtil.convertToBoolean(getChecked(), true);
80: }
81:
82: /**
83: * Gets the value of the argument {@link #setChecked(String)}.
84: * @return The value as set by the user.
85: */
86: public String getChecked() {
87: return fChecked;
88: }
89:
90: /**
91: * @webtest.parameter required="false"
92: * default="true"
93: * description="Specifies if the checkbox shall be checked (true) or unchecked (false)."
94: */
95: public void setChecked(final String checked) {
96: fChecked = checked;
97: }
98:
99: }
|