01: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
02: package com.canoo.webtest.steps.verify;
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:
12: import java.util.Iterator;
13: import java.util.List;
14:
15: /**
16: * @author Carsten Seibert
17: * @author Marc Guillemot
18: * @author Paul King
19: * @webtest.step
20: * category="Core"
21: * name="verifyCheckbox"
22: * alias="verifycheckbox"
23: * description="This step is used to verify the state of a checkbox (checked/unchecked). A particular checkbox can be specified via <em>name</em> and optionally <em>value</em> (or alternatively by <em>fieldIndex</em>)."
24: */
25: public class VerifyCheckbox extends AbstractVerifyFormStep {
26: private String fChecked;
27:
28: public String getChecked() {
29: return fChecked;
30: }
31:
32: /**
33: * @webtest.parameter
34: * required="yes"
35: * description="Specifies if the checkbox shall be checked (true) or unchecked (false)."
36: */
37: public void setChecked(final String checked) {
38: fChecked = checked;
39: }
40:
41: /**
42: * Finds the relevant form.
43: *
44: */
45: protected HtmlForm findForm() {
46: return FormUtil.findFormForField(getContext(), getFormName(),
47: HtmlConstants.INPUT, HtmlConstants.CHECKBOX, getName(),
48: this );
49: }
50:
51: /**
52: * Finds then verifies the field of interest.
53: * @param form
54: */
55: protected List findFields(final HtmlForm form) {
56: List li = form.getInputsByName(getName());
57: // remove those not having the right name, and optionally value
58: for (Iterator iter = li.iterator(); iter.hasNext();) {
59: final HtmlElement elt = (HtmlElement) iter.next();
60: if (elt instanceof HtmlCheckBoxInput) {
61: if (getValue() == null
62: || getValue().equals(
63: ((HtmlCheckBoxInput) elt)
64: .getValueAttribute())) {
65: continue;
66: }
67: }
68: iter.remove();
69: }
70: return li;
71: }
72:
73: protected void verifyField(final HtmlElement element) {
74: HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) element;
75: if (checkbox.isChecked() != ConversionUtil.convertToBoolean(
76: getChecked(), false)) {
77: throw new StepFailedException("Checkbox is "
78: + (checkbox.isChecked() ? "" : "not") + "checked!",
79: this);
80: }
81: }
82:
83: }
|