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 sets the text of a textfield or textarea field.
09: * The text can contain properties.
10: * <br/>
11: * parmater: name = text
12: * <br/>
13: * examples:
14: * <br/>
15: * set_textfield | comment = Hello world
16: * <br/>
17: * set_textfield | username = ${username}
18: *
19: * @author Mecky
20: */
21: public class SetTextFieldCommand implements WebTestNode {
22:
23: public static final String COMMAND_NAME = "set_textfield";
24: private String name;
25: private String cvalue;
26:
27: public void setParameter(String value) {
28: name = value.substring(0, value.indexOf("="));
29: name = name.trim();
30: cvalue = value.substring(value.indexOf("=") + 1);
31: cvalue = cvalue.trim();
32: }
33:
34: public WebTestNodeResult execute(WebTestContext context) {
35: WebTestNodeResult result = new WebTestNodeResult(COMMAND_NAME,
36: name + " = " + cvalue);
37: WebForm form = context.getWebForm();
38: if (form != null) {
39: try {
40: String trValue = context.replaceVar(cvalue);
41: form.setParameter(name, trValue);
42: result.setSuccess(true);
43: } catch (Exception e) {
44: result.setSuccess(false);
45: result.setException(e);
46: }
47: } else {
48: result.setErrorMessage("set_textfield: No form selected");
49: result.setSuccess(false);
50: }
51: return result;
52: }
53:
54: public String getName() {
55: return COMMAND_NAME;
56: }
57:
58: }
|