001: package org.jzonic.webtester.commands;
002:
003: import org.jzonic.webtester.WebTestContext;
004:
005: import com.meterware.httpunit.WebTable;
006:
007: /**
008: * This command will check if cell of a table contains
009: * the required text. The x and y position are zero based.
010: * If the x or y position is out of bounds for the table
011: * then the test will fail. <br/>
012: * The test will fail if no table was selected before.
013: * <br/>
014: * The required text can also contain variables
015: * <br/>
016: * parameter: (xpos,ypos) = text
017: * <br/>
018: * example:
019: * <br/>
020: * check_tablecell | (1,1) = Hello world
021: * <br/>
022: * check_tablecell | (2,5) = ${username}
023: *
024: * @author Mecky
025: */
026: public class CheckTableCellCommand implements WebTestNode {
027:
028: public static final String COMMAND_NAME = "check_tablecell";
029: private String searchText;
030: private int x = -1;
031: private int y = -1;
032:
033: public WebTestNodeResult execute(WebTestContext context) {
034: WebTestNodeResult result = new WebTestNodeResult(COMMAND_NAME,
035: "(" + x + "," + y + ") " + searchText);
036: WebTable table = context.getTable();
037: if (x == -1 || y == -1) {
038: result.setSuccess(false);
039: result
040: .setErrorMessage("check_tablecell: no valid cell position defined: x="
041: + x + " y=" + y);
042: } else {
043: if (table != null) {
044: try {
045: String cellText = table.getCellAsText(x, y);
046: String sText = context.replaceVar(searchText);
047: if (cellText.equals(sText)) {
048: result.setSuccess(true);
049: } else {
050: result.setSuccess(false);
051: result
052: .setErrorMessage("check_tablecell: required:"
053: + searchText
054: + " but found:"
055: + cellText);
056: }
057: } catch (Exception e) {
058: result.setSuccess(false);
059: result
060: .setErrorMessage("check_tablecell: the position x="
061: + x
062: + " y="
063: + y
064: + " is out of bounds");
065: }
066: } else {
067: result.setSuccess(false);
068: result
069: .setErrorMessage("check_tablecell: no table selected");
070: }
071: }
072: return result;
073: }
074:
075: public void setParameter(String value) {
076: String tmp = value.substring(0, value.indexOf("="));
077: tmp = tmp.trim();
078: if (tmp.startsWith("(")) {
079: tmp = tmp.substring(1);
080: }
081: if (tmp.endsWith(")")) {
082: tmp = tmp.substring(0, tmp.length() - 1);
083: }
084: if (tmp.indexOf(",") != -1) {
085: String xp = tmp.substring(0, tmp.indexOf(","));
086: String yp = tmp.substring(tmp.indexOf(",") + 1);
087: try {
088: x = Integer.parseInt(xp.trim());
089: y = Integer.parseInt(yp.trim());
090: } catch (Exception e) {
091:
092: }
093: }
094: searchText = value.substring(value.indexOf("=") + 1);
095: searchText = searchText.trim();
096: }
097:
098: public String getName() {
099: return COMMAND_NAME;
100: }
101:
102: }
|