01: /*
02: * Created on 15.10.2004
03: *
04: * TODO To change the template for this generated file go to
05: * Window - Preferences - Java - Code Style - Code Templates
06: */
07: package org.jzonic.webtester.commands;
08:
09: import org.jzonic.webtester.WebTestContext;
10:
11: /**
12: * This command will check if the specified text is found on the page.
13: * The test will fail if the text is not found. If you want to negotiate
14: * the result then put a ! in front of the text. This test will now fail
15: * if the text is present. The required text can also contain properties
16: * that are defined in a properties file.
17: * <br/>
18: * parameter: required text
19: * examples:<br/>
20: * check_text | Hello world
21: * <br/>
22: * check_text | !Hello
23: * <br/>
24: * check_text | Welcome ${username}
25: *
26: * @author Mecky
27: */
28: public class CheckTextCommand implements WebTestNode {
29:
30: public static final String COMMAND_NAME = "check_text";
31: private String searchText;
32:
33: public WebTestNodeResult execute(WebTestContext context) {
34: String text = context.getHtmlText();
35: WebTestNodeResult result = new WebTestNodeResult(COMMAND_NAME,
36: searchText);
37: result.setSuccess(true);
38: boolean required = true;
39: String stext = searchText;
40: if (searchText.startsWith("!")) {
41: stext = searchText.substring(1);
42: required = false;
43: }
44: String findMe = context.replaceVar(stext);
45: int pos = text.indexOf(findMe);
46: if (required && pos == -1) {
47: result.setSuccess(false);
48: result.setErrorMessage("check_text: The text:'"
49: + searchText + "' was not found");
50: }
51: if (!required && pos != -1) {
52: result.setSuccess(false);
53: result.setErrorMessage("check_text: The text:'"
54: + searchText + "' was found");
55: }
56: return result;
57: }
58:
59: public void setParameter(String value) {
60: searchText = value;
61: }
62:
63: public String getName() {
64: return COMMAND_NAME;
65: }
66:
67: }
|