001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps.store;
003:
004: import org.apache.log4j.Logger;
005: import org.jaxen.JaxenException;
006: import org.jaxen.XPath;
007:
008: import com.canoo.webtest.engine.StepFailedException;
009: import com.canoo.webtest.engine.xpath.XPathHelper;
010: import com.gargoylesoftware.htmlunit.Page;
011:
012: /**
013: * Attributes:
014: * description The description of this test step optional
015: * select The XPath expression to evaluate mandatory
016: * property The name of the property mandatory
017: * propertyType The type of the property optional
018: *
019: * @author Walter Rumsby
020: * @author Marc Guillemot
021: * @author Paul King
022: * @since 1.5
023: * @webtest.step
024: * category="Core"
025: * name="storeXPath"
026: * alias="storexpath"
027: * description="This step stores the result of an <key>XPATH</key> expression into a property."
028: */
029: public class StoreXPath extends BaseStoreStep {
030: private static final Logger LOG = Logger
031: .getLogger(StoreXPath.class);
032: private String fXpath;
033: private String fDefault;
034:
035: public String getXpath() {
036: return fXpath;
037: }
038:
039: /**
040: * @webtest.parameter
041: * required="yes"
042: * description="The <key>XPATH</key> that shall be evaluated."
043: */
044: public void setXpath(final String xpath) {
045: fXpath = xpath;
046: }
047:
048: /**
049: * @webtest.parameter
050: * required="no"
051: * description="The value to store in the property when the xpath evaluation returns no result
052: * (if not set, the step will fail if the xpath evaluation returns no result)."
053: */
054: public void setDefault(final String defaultValue) {
055: fDefault = defaultValue;
056: }
057:
058: public String getDefault() {
059: return fDefault;
060: }
061:
062: public void doExecute() throws JaxenException {
063: final Page currentResponse = getContext().getCurrentResponse();
064:
065: final XPathHelper xpathHelper = getContext().getXPathHelper();
066: final XPath xpath = xpathHelper.getXPath(currentResponse,
067: getXpath());
068: final Object document = xpathHelper
069: .getDocument(currentResponse);
070:
071: storeProperty(evaluateXPath(xpath, document));
072: }
073:
074: protected String evaluateXPath(final XPath xpath,
075: final Object document) throws JaxenException {
076: final String result = xpath.stringValueOf(document);
077:
078: // seems that result is "" and not null when nothing is found
079: if (result == null
080: || (result.length() == 0 && xpath
081: .selectSingleNode(document) == null)) {
082:
083: if (getDefault() == null) {
084: throw new StepFailedException(
085: "No match for xpath expression <" + fXpath
086: + ">", this );
087: } else {
088: LOG.debug("No result, using default value");
089: return getDefault();
090: }
091: }
092:
093: LOG.debug("Xpath result: " + result);
094: return result;
095: }
096:
097: protected void verifyParameters() {
098: super .verifyParameters();
099: emptyParamCheck(getProperty(), "property");
100: nullParamCheck(getXpath(), "xpath");
101: }
102:
103: }
|