01: // Copyright © 2002-2005 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.FormUtil;
06: import com.canoo.webtest.util.HtmlConstants;
07: import com.gargoylesoftware.htmlunit.html.HtmlElement;
08: import com.gargoylesoftware.htmlunit.html.HtmlForm;
09: import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
10:
11: import java.io.IOException;
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: /**
16: * Provides the ability to check a radio button.<p>
17: *
18: * @author Marc Guillemot
19: * @webtest.step
20: * category="Core"
21: * name="setRadioButton"
22: * alias="new_setradiobutton,setradiobutton"
23: * description="Provides the ability to check radio buttons in <key>HTML</key> forms."
24: */
25: public class SetRadioButton extends AbstractSetNamedFieldStep {
26: protected HtmlForm findForm() {
27: return FormUtil.findFormForField(getContext(), getFormName(),
28: HtmlConstants.INPUT, HtmlConstants.RADIO, getName(),
29: this );
30: }
31:
32: /**
33: * Finds the radio button with the given name and value in the form.
34: *
35: * @param form The form to search
36: * @return the list of fields with the given name
37: */
38: protected List findFields(final HtmlForm form) {
39: List fields = new ArrayList();
40: fields.add(form.getRadioButtonInput(getName(), getValue()));
41: return fields;
42: }
43:
44: /**
45: * Updates a field.
46: *
47: * @param elt the field to update
48: */
49: protected void setField(HtmlElement elt) throws IOException {
50: if (!(elt instanceof HtmlRadioButtonInput)) {
51: throw new StepFailedException("Found " + elt.getTagName()
52: + " when looking for radio button", this );
53: }
54: ((HtmlRadioButtonInput) elt).click();
55: }
56:
57: /**
58: * Set the value.
59: *
60: * @param value
61: * @webtest.parameter
62: * required="yes/no"
63: * description="The value to use to find the desired radio-button.
64: * Must be specified if <em>name</em> is specified."
65: */
66: public void setValue(final String value) {
67: super .setValue(value);
68: }
69:
70: /**
71: * Verifies the parameters.<p>
72: */
73: protected void verifyParameters() {
74: super .verifyParameters();
75: if (getHtmlId() == null && getXpath() == null
76: && getForLabel() == null) {
77: nullParamCheck(getValue(), "value");
78: } else {
79: paramCheck(getValue() != null,
80: "Can't specify attribute value when htmlid, xpath or forLabel is specified");
81: }
82: }
83: }
|