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 = value
12: * <br/>
13: * example:
14: * <br/>
15: * check_option | mode = edit
16: *
17: * @author Mecky
18: */
19: public class CheckOptionCommand implements WebTestNode {
20:
21: public static final String COMMAND_NAME = "check_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: boolean found = false;
40: for (int i = 0; i < values.length; i++) {
41: if (values[i].equals(cvalue)) {
42: found = true;
43: }
44: }
45: if (found) {
46: result.setSuccess(true);
47: } else {
48: result.setSuccess(false);
49: result.setErrorMessage("check_option: the value:'"
50: + cvalue + "' was not found in the option");
51: }
52: } catch (Exception e) {
53: result.setSuccess(false);
54: result.setException(e);
55: }
56: } else {
57: result.setErrorMessage("check_option: no form selected");
58: result.setSuccess(false);
59: }
60: return result;
61: }
62:
63: public String getName() {
64: return COMMAND_NAME;
65: }
66:
67: }
|