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 test if a specific option box contains
09: * the required entry
10: * <br/>
11: * parameter: name of the option = value
12: * <br/>
13: * example:
14: * <br/>
15: * set_option | mode = edit
16: *
17: * @author Mecky
18: */
19: public class SetOptionCommand implements WebTestNode {
20:
21: public static final String COMMAND_NAME = "set_option";
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: String[] values = form.getOptions(name);
39: int found = -1;
40: for (int i = 0; i < values.length; i++) {
41: if (values[i].equals(cvalue)) {
42: found = i;
43: }
44: }
45: if (found != -1) {
46: result.setSuccess(true);
47: String[] vals = form.getOptionValues(name);
48: form.setParameter(name, vals[found]);
49: } else {
50: result.setSuccess(false);
51: result.setErrorMessage("set_option: the value:'"
52: + cvalue + "' was not found in the option");
53: }
54: } catch (Exception e) {
55: result.setSuccess(false);
56: result.setException(e);
57: }
58: } else {
59: result.setErrorMessage("set_option: no form selected");
60: result.setSuccess(false);
61: }
62: return result;
63: }
64:
65: public String getName() {
66: return COMMAND_NAME;
67: }
68:
69: }
|