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