001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.locator;
003:
004: import com.canoo.webtest.engine.Context;
005: import com.canoo.webtest.engine.StepFailedException;
006: import com.canoo.webtest.interfaces.ITableLocator;
007: import com.canoo.webtest.steps.Step;
008: import com.gargoylesoftware.htmlunit.ElementNotFoundException;
009: import com.gargoylesoftware.htmlunit.html.HtmlElement;
010: import com.gargoylesoftware.htmlunit.html.HtmlTable;
011: import org.xml.sax.SAXException;
012:
013: import java.util.Iterator;
014:
015: /**
016: * @author Dierk König, Carsten Seibert
017: * @author Marc Guillemot
018: * @author Paul King
019: * @webtest.nested
020: * category="Core"
021: * name="table"
022: * description="This optional nested element can be used to locate a specific cell in a specific table on the page."
023: */
024: public class TableLocator implements ITableLocator {
025: private String fHtmlId;
026: private String fDescription;
027: private int fRow;
028: private int fColumn;
029:
030: /**
031: * @webtest.parameter
032: * required="no"
033: * description="The table's id attribute, if present. It is used instead of the name attribute here, because HTML 4.0 does not support that attribute on tables."
034: */
035: public void setHtmlId(final String id) {
036: fHtmlId = id;
037: }
038:
039: public String getHtmlId() {
040: return fHtmlId;
041: }
042:
043: /**
044: * @webtest.parameter
045: * required="no"
046: * description="Deprecated. Same as htmlid."
047: * @deprecated use {@link #setHtmlId(String)} instead
048: */
049: public void setId(final String id) {
050: setHtmlId(id);
051: }
052:
053: /**
054: * @deprecated use {@link #setHtmlId(String)} instead
055: */
056: public String getId() {
057: return getHtmlId();
058: }
059:
060: /**
061: * @webtest.parameter
062: * required="yes"
063: * description="The cell's row index in the table, starting at 0."
064: */
065: public void setRow(final int row) {
066: fRow = row;
067: }
068:
069: public int getRow() {
070: return fRow;
071: }
072:
073: /**
074: * @webtest.parameter
075: * required="yes"
076: * description="The cell's column index in the table, starting at 0."
077: */
078: public void setColumn(final int column) {
079: fColumn = column;
080: }
081:
082: public int getColumn() {
083: return fColumn;
084: }
085:
086: /**
087: * @webtest.parameter
088: * required="no"
089: * description="Description for this locator."
090: */
091: public void setDescription(final String description) {
092: this .fDescription = description;
093: }
094:
095: public String getDescription() {
096: return fDescription;
097: }
098:
099: public String locateText(final Context context, Step step)
100: throws TableNotFoundException, IndexOutOfBoundsException,
101: SAXException {
102: try {
103: final HtmlElement htmlElement;
104: if (getHtmlId() == null) {
105: htmlElement = findFirstTable(context, step);
106: } else {
107: htmlElement = context.getCurrentHtmlResponse(step)
108: .getHtmlElementById(getHtmlId());
109: }
110: if (!(htmlElement instanceof HtmlTable)) {
111: throw new StepFailedException(
112: "Found '"
113: + htmlElement.getTagName()
114: + "' element when looking for 'table' element using htmlId "
115: + getHtmlId(), step);
116: }
117: final HtmlTable table = (HtmlTable) htmlElement;
118: return table.getRow(getRow()).getCell(getColumn()).asText();
119: } catch (final ElementNotFoundException e) {
120: throw new TableNotFoundException(getHtmlId());
121: }
122: }
123:
124: private static HtmlElement findFirstTable(final Context context,
125: final Step step) {
126: final Iterator allHtmlChildElements = context
127: .getCurrentHtmlResponse(step).getAllHtmlChildElements();
128: while (allHtmlChildElements.hasNext()) {
129: final HtmlElement element = (HtmlElement) allHtmlChildElements
130: .next();
131: if (element instanceof HtmlTable) {
132: return element;
133: }
134: }
135: throw new ElementNotFoundException("*", "*", "*");
136: }
137: }
|