001: package org.jzonic.webtester;
002:
003: import java.util.HashMap;
004: import java.util.List;
005: import java.util.Vector;
006:
007: import org.jzonic.webtester.commands.*;
008:
009: /**
010: * WebTester covers all methods to build and run a single test. A test is
011: * a set of WebTestNodes. Each node defining a step in the entire testcase.
012: *
013: * @author Mecky
014: */
015: public class WebTester {
016:
017: private String webTestName;
018: private List tests;
019: private static HashMap knownTestNodes = new HashMap();
020: static {
021: knownTestNodes.put(GetHtmlCommand.COMMAND_NAME,
022: GetHtmlCommand.class);
023: knownTestNodes.put(CheckTextCommand.COMMAND_NAME,
024: CheckTextCommand.class);
025: knownTestNodes.put(SetProxyCommand.COMMAND_NAME,
026: SetProxyCommand.class);
027: knownTestNodes.put(SelectFormCommand.COMMAND_NAME,
028: SelectFormCommand.class);
029: knownTestNodes.put(SubmitFormCommand.COMMAND_NAME,
030: SubmitFormCommand.class);
031: knownTestNodes.put(SetTextFieldCommand.COMMAND_NAME,
032: SetTextFieldCommand.class);
033: knownTestNodes.put(PrintHtmlCommand.COMMAND_NAME,
034: PrintHtmlCommand.class);
035: knownTestNodes.put(CheckTitleCommand.COMMAND_NAME,
036: CheckTitleCommand.class);
037: knownTestNodes.put(ClickLinkCommand.COMMAND_NAME,
038: ClickLinkCommand.class);
039: knownTestNodes.put(ReadPropertiesCommand.COMMAND_NAME,
040: ReadPropertiesCommand.class);
041: knownTestNodes.put(CheckOptionCommand.COMMAND_NAME,
042: CheckOptionCommand.class);
043: knownTestNodes.put(SetOptionCommand.COMMAND_NAME,
044: SetOptionCommand.class);
045: knownTestNodes.put(SetCheckboxCommand.COMMAND_NAME,
046: SetCheckboxCommand.class);
047: knownTestNodes.put(SelectTableCommand.COMMAND_NAME,
048: SelectTableCommand.class);
049: knownTestNodes.put(CheckTableCellCommand.COMMAND_NAME,
050: CheckTableCellCommand.class);
051: knownTestNodes.put(SetParameterCommand.COMMAND_NAME,
052: SetParameterCommand.class);
053: knownTestNodes.put(SetUrlPrefixCommand.COMMAND_NAME,
054: SetUrlPrefixCommand.class);
055: knownTestNodes.put(EnableJavaScriptCommand.COMMAND_NAME,
056: EnableJavaScriptCommand.class);
057: }
058:
059: public WebTester(String name) {
060: tests = new Vector();
061: webTestName = name;
062: }
063:
064: /**
065: * This method will run a test. The test is a set of WebTestNodes.
066: *
067: * @return the WebTestResult containing all informations for this test
068: */
069: public WebTestResult runTest() {
070: WebTestContext ctx = new WebTestContext();
071: WebTestResult result = new WebTestResult(webTestName);
072: result.setSuccess(true);
073: result.startTiming();
074: boolean cont = true;
075: for (int i = 0; i < tests.size(); i++) {
076: if (cont) {
077: WebTestNode node = (WebTestNode) tests.get(i);
078: WebTestNodeResult res = node.execute(ctx);
079: res.setLineNumber(i);
080: result.addResult(res);
081: if (!res.isSuccess()) {
082: result.setErrorNode(res);
083: cont = false;
084: result.setSuccess(false);
085: }
086: }
087: }
088: result.stopTiming();
089: return result;
090: }
091:
092: public void parseLine(String line) {
093: String cmd = null;
094: String param = null;
095: if (line.indexOf("|") != -1) {
096: cmd = line.substring(0, line.indexOf("|"));
097: param = line.substring(line.indexOf("|") + 1);
098:
099: param = param.trim();
100: } else {
101: cmd = line;
102: }
103: cmd = cmd.trim();
104: if (knownTestNodes.containsKey(cmd)) {
105: Class clazz = (Class) knownTestNodes.get(cmd);
106: try {
107: WebTestNode node = (WebTestNode) clazz.newInstance();
108: if (param != null) {
109: node.setParameter(param);
110: }
111: tests.add(node);
112: } catch (InstantiationException e) {
113: // TODO Auto-generated catch block
114: e.printStackTrace();
115: } catch (IllegalAccessException e) {
116: // TODO Auto-generated catch block
117: e.printStackTrace();
118: }
119: } else {
120: System.out.println("The following cmd is not supported:"
121: + cmd);
122: }
123: }
124:
125: /**
126: * Returns the name of the test
127: *
128: * @return the name of the test
129: */
130: public String getWebTestName() {
131: return webTestName;
132: }
133:
134: /**
135: * Sets the test name
136: * @param webTestName name of the test
137: */
138: public void setWebTestName(String webTestName) {
139: this.webTestName = webTestName;
140: }
141: }
|