01: package org.jzonic.webtester.commands;
02:
03: import org.jzonic.webtester.WebTestContext;
04:
05: import com.meterware.httpunit.WebForm;
06:
07: /**
08: * This command will set or unset a checkbox in a form.
09: * If the value is true then the checkbox will be set.
10: * <br/>
11: * parameter: name of the checkbox = true or false
12: * <br/>
13: * examples:
14: * <br/>
15: * set_checkbox | get_newsletter = true
16: *
17: * @author Mecky
18: */
19: public class SetCheckboxCommand implements WebTestNode {
20:
21: public static final String COMMAND_NAME = "set_checkbox";
22: private String name;
23: private String cvalue;
24:
25: public void setParameter(String value) {
26: name = value.substring(0, value.indexOf("="));
27: name = name.trim();
28: cvalue = value.substring(value.indexOf("=") + 1);
29: cvalue = cvalue.trim();
30: }
31:
32: public WebTestNodeResult execute(WebTestContext context) {
33: WebTestNodeResult result = new WebTestNodeResult(COMMAND_NAME,
34: name + " = " + cvalue);
35: WebForm form = context.getWebForm();
36: if (form != null) {
37: try {
38: if (cvalue.equalsIgnoreCase("true")) {
39: form.setCheckbox(name, true);
40: } else {
41: form.setCheckbox(name, false);
42: }
43: result.setSuccess(true);
44: } catch (Exception e) {
45: result.setSuccess(false);
46: result.setException(e);
47: }
48: } else {
49: result.setErrorMessage("set_checkbox: no form selected");
50: result.setSuccess(false);
51: }
52: return result;
53: }
54:
55: public String getName() {
56: return COMMAND_NAME;
57: }
58:
59: }
|