001: // Copyright © 2004-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.extension;
003:
004: import org.apache.log4j.Logger;
005: import org.apache.tools.ant.Task;
006: import org.xml.sax.SAXException;
007:
008: import com.canoo.webtest.boundary.AntBoundary;
009: import com.canoo.webtest.engine.Context;
010: import com.canoo.webtest.engine.ContextHelper;
011: import com.canoo.webtest.engine.StepFailedException;
012: import com.canoo.webtest.interfaces.IContentFilter;
013: import com.canoo.webtest.interfaces.IStepWithTableLocator;
014: import com.canoo.webtest.interfaces.ITableLocator;
015: import com.canoo.webtest.steps.AbstractStepContainer;
016: import com.canoo.webtest.steps.locator.TableLocator;
017: import com.canoo.webtest.steps.locator.TableNotFoundException;
018:
019: /**
020: * Abstract class used for processing content
021: */
022: public abstract class AbstractProcessFiltersStep extends
023: AbstractStepContainer implements IStepWithTableLocator {
024: private static final Logger LOG = Logger
025: .getLogger(AbstractProcessFiltersStep.class);
026:
027: private ITableLocator fTableLocator;
028: private String fHtmlId;
029: private String fXpath;
030:
031: public String getHtmlId() {
032: return fHtmlId;
033: }
034:
035: /**
036: * Sets the id attribute of the element to process.<p>
037: *
038: * @param str the new value
039: * @webtest.parameter required="no"
040: * description="The id of an <key>HTML</key> element. If present, processing will be limited to this element. Will be ignored if a nested <stepref name='table'/> step is used."
041: */
042: public void setHtmlId(final String str) {
043: fHtmlId = str;
044: }
045:
046: public String getXpath() {
047: return fXpath;
048: }
049:
050: /**
051: * Sets the XPath used to identify the element to process.<p>
052: *
053: * @param path the new value
054: * @webtest.parameter required="no"
055: * description="An XPath expression identifying an <key>HTML</key> element. If present, processing will be limited to this element. Will be ignored if <em>htmlId</em> is set or if a nested <stepref name='table'/> step is used."
056: */
057: public void setXpath(final String path) {
058: fXpath = path;
059: }
060:
061: public ITableLocator getTableLocator() {
062: return fTableLocator;
063: }
064:
065: public void addTask(final Task newTask) {
066: final Task task = AntBoundary.maybeConfigure(newTask);
067: if (task instanceof IContentFilter) {
068: super .addTask(task);
069: } else {
070: LOG
071: .warn("Ignoring inner step because it is not a content filter: "
072: + task.getClass().getName());
073: }
074: }
075:
076: /**
077: *
078: * @param tableLocator
079: * @webtest.nested.parameter
080: * required="no"
081: * description="To locate a specific cell in a specific table on the page."
082: */
083: public void addTable(final TableLocator tableLocator) {
084: addTableInternal(tableLocator);
085: }
086:
087: private void addTableInternal(final ITableLocator tableLocator) {
088: fTableLocator = tableLocator;
089: }
090:
091: protected void applyTableFilterIfNeeded(final Context context) {
092: if (getTableLocator() != null) {
093: tryLocateText(context);
094: }
095: }
096:
097: protected void applyExtractionIfNeeded(final Context context) {
098: final String origType = context.getCurrentResponse()
099: .getWebResponse().getContentType();
100: if (fXpath != null || fHtmlId != null) {
101: ContextHelper.defineAsCurrentResponse(context,
102: StoreElementAttribute.findElement(
103: context.getCurrentResponse(), getHtmlId(),
104: getXpath(), LOG, this ).asXml(), origType,
105: "http:" + getClass().getName());
106: }
107: }
108:
109: private void tryLocateText(final Context context) {
110: final String origType = context.getCurrentResponse()
111: .getWebResponse().getContentType();
112: try {
113: ContextHelper.defineAsCurrentResponse(context,
114: getTableLocator().locateText(context, this ),
115: origType, "http:" + getClass().getName());
116: } catch (TableNotFoundException tableNotFound) {
117: throw new StepFailedException("Cannot find table: "
118: + tableNotFound.toString(), this );
119: } catch (IndexOutOfBoundsException ioobe) {
120: throw new StepFailedException(
121: "Cannot find cell with supplied index in table",
122: this );
123: } catch (SAXException se) {
124: throw new StepFailedException("Can't parse table: "
125: + se.getMessage(), this);
126: }
127: }
128: }
|