[Webtest] stupid webtest question #426. Can I store a constant
for later use?
Marc Guillemot
webtest@lists.canoo.com
Tue, 30 Sep 2003 09:09:16 +0200
This is a multi-part message in MIME format.
--------------080100010501080104020709
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Hi Bill,
attached is my custom step that extends the com.canoo.webtest.steps.StoreXPath
to allow xpath expression to eval to something else as a node.
In my test cases it works as expected but I can't 100% certify that it evaluates
like the Canoo version for nodes and attributes as I'm not sure of the exact way
the dom methods work.
Marc.
Bill Milbratz wrote:
>>In our current project we have the very same issue.
>>You need to find an xpath expression that evaluates to your 'constant',
> eg. xpath="concat(''MYCONSTANT,'')"
>
> This sounded good, but such a concat (and all combinations I tried)
> gave me the following errors. Any suggestions?
> e.g. these xpath's:
> xpath="concat(''MYCONSTANT','')"
> xpath="concat(//title,'MYCONSTANT')"
>
> Yielded these such errors:
>
> [testSpec] org.dom4j.XPathException: Exception occurred evaluting
> XPath: The result of the
> XPath expression is not a Node. It was: a of type: java.lang.String. You
> might want to use a
> different method such as selectObject() to evaluate this XPath expression
> [testSpec] at
> org.dom4j.xpath.DefaultXPath.selectSingleNode(DefaultXPath.java:163)
> [testSpec] at
> org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:188)
> [testSpec] at
> com.canoo.webtest.steps.StoreXPath.getXPath(StoreXPath.java:83)
> [testSpec] at
> com.canoo.webtest.steps.StoreXPath.doExecute(StoreXPath.java:58)
> [testSpec] at com.canoo.webtest.steps.Step.execute(Step.java:56)
> [testSpec] at
> com.canoo.webtest.engine.Engine.executeSteps(Engine.java:32)
> [testSpec] at
> com.canoo.webtest.ant.TestSpecificationTask.execute(TestSpecificationTask
>
> thanks,
>
> bill m
>
>
--------------080100010501080104020709
Content-Type: text/plain;
name="StoreXPath.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="StoreXPath.java"
package de.internetzky.webtests.extension;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.tree.AbstractAttribute;
import com.canoo.webtest.engine.StepFailedException;
/**
* Improved {@link #getXPath} to allow evaluation of expressions
* @author Marc Guillemot
* @version $Revision: 1.2 $ $Date: 2003/08/25 14:30:49 $ Last change by $Author: cvs-marc $
*/
public class StoreXPath extends com.canoo.webtest.steps.StoreXPath
{
/**
* same as {@link com.canoo.webtest.steps.StoreXPath#getXPath} plus:
* <ul>
* <li>xpath can be get something else as a node / node list</li>
* </ul>
*/
protected String getXPath(Document document)
{
Object o = document.selectObject(getXpath());
if (o == null)
{
throw new StepFailedException(
"No match for xpath expression <" + getXpath() + ">",
this);
}
// if list with single element, then we take the element
if (o instanceof List && ((List) o).size() == 1)
{
o = ((List) o).get(0);
}
// for an attribute, take the value
if (o instanceof AbstractAttribute)
{
o = ((AbstractAttribute) o).getValue();
}
return o.toString();
}
}
--------------080100010501080104020709--