001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.verify;
003:
004: import java.io.IOException;
005:
006: import org.xml.sax.SAXException;
007:
008: import com.canoo.webtest.engine.Context;
009: import com.canoo.webtest.engine.StepFailedException;
010: import com.canoo.webtest.interfaces.IStepWithTableLocator;
011: import com.canoo.webtest.interfaces.ITableLocator;
012: import com.canoo.webtest.steps.locator.TableLocator;
013: import com.canoo.webtest.steps.locator.TableNotFoundException;
014:
015: /**
016: * @author Carsten Seibert
017: * @author Marc Guillemot
018: * @webtest.step
019: * category="Core"
020: * name="verifyText"
021: * alias="verifytext"
022: * description="This step verifies the existence of the specified string somewhere in the response received from server
023: * (ie the changes that may have occured since the page has been loaded are not seen by this step).
024: * The text could represent an <key>HTML</key> fragment like \"<BODY>\" or a arbitrary static text in the page (\"An application error has occurred!\")."
025: */
026: public class VerifyText extends AbstractVerifyTextStep implements
027: IStepWithTableLocator {
028: private ITableLocator fTableLocator;
029:
030: /**
031: * Called by Ant to set the text nested between opening and closing tags.
032: * @param text the text to set
033: * @webtest.nested.parameter
034: * required="no"
035: * description="Alternative way to set the 'text' attribute."
036: */
037: public void addText(final String text) {
038: if (getText() == null)
039: setText(getProject().replaceProperties(text));
040: }
041:
042: /**
043: * @param tableLocator
044: * @webtest.nested.parameter
045: * required="no"
046: * description="To locate a specific cell in a specific table on the page."
047: */
048: public void addTable(final TableLocator tableLocator) {
049: addTableInternal(tableLocator);
050: }
051:
052: public void addTableInternal(final ITableLocator tableLocator) {
053: fTableLocator = tableLocator;
054: }
055:
056: public void doExecute() throws Exception {
057: if (!isExpectedStringPresent()) {
058: throw new StepFailedException(getStepLabel() + ": "
059: + getFailedMessage(), this );
060: }
061: }
062:
063: protected String getFailedMessage() {
064: return "Text not found in page. Expected <" + getText() + ">";
065: }
066:
067: /**
068: * @deprecated Use {@link #isExpectedStringPresent()}
069: */
070: protected boolean isExpectedStringPresent(final Context context)
071: throws SAXException, IOException {
072: return isExpectedStringPresent();
073: }
074:
075: protected boolean isExpectedStringPresent() throws SAXException,
076: IOException {
077: Context context = getContext();
078: try {
079: String text;
080:
081: if (getTableLocator() == null) {
082: text = context.getCurrentResponse().getWebResponse()
083: .getContentAsString();
084: } else {
085: text = getTableLocator().locateText(context, this );
086: }
087: if (isRegex()) {
088: return verifyText(text);
089: }
090: return text.indexOf(getText()) > -1;
091: } catch (TableNotFoundException tnf) {
092: throw new StepFailedException("Cannot find table: "
093: + tnf.toString(), this );
094: } catch (IndexOutOfBoundsException ioobe) {
095: throw new StepFailedException(
096: "Cannot find cell with supplied index in table",
097: this );
098: }
099: }
100:
101: public ITableLocator getTableLocator() {
102: return fTableLocator;
103: }
104: }
|