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 an additional parameter in the form. This can
09: * be used to set hidden fields.
10: * <br/>
11: * parameter: parameter name = value
12: * <br/>
13: * examples:
14: * <br/>
15: * set_parameter | id = 100
16: *
17: * @author Mecky
18: */
19: public class SetParameterCommand implements WebTestNode {
20:
21: public static final String COMMAND_NAME = "set_parameter";
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: form.setParameter(name, cvalue);
39: result.setSuccess(true);
40: } catch (Exception e) {
41: result.setSuccess(false);
42: result.setException(e);
43: }
44: } else {
45: result.setErrorMessage(COMMAND_NAME + ": no form selected");
46: result.setSuccess(false);
47: }
48: return result;
49: }
50:
51: public String getName() {
52: return COMMAND_NAME;
53: }
54:
55: }
|