Feature Requests item #1637716, was opened at 2007-01-18 02:36
Message generated for change (Comment added) made by jevonwright
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=497985&aid=1637716&group_id=61302
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core API
Group: Release 2.1
Status: Closed
Priority: 5
Private: No
Submitted By: Peter Becker (peterbecker)
Assigned to: Jevon Wright (jevonwright)
Summary: XPath Assertions for boolean and text expressions
Initial Comment:
A feature we miss coming from Canoo Webtest is the option to do the following:
* assert that the booleanValue of an XPath is true
* assert that the textValue of an XPath is equal to an expected value
* assert that the textValue of an XPath matches a regular expression
We converted all our Webtests to HTMLUnit and are now considering moving to
JWebUnit, but while we could achieve these assertions with HTMLUnit+Jaxen, I
can't see easily how to do it with JWebUnit.
Here is a code sample for the boolean assertion on top of HTMLUnit and Jaxen:
protected void verifyXPath(String booleanXpath) {
String expandedXPath = expandConstants(booleanXpath);
Page curPage = this.session.getCurrentPage();
try {
boolean xpathEval;
if (curPage instanceof HtmlPage) {
XPath xpath = new HtmlUnitXPath(expandedXPath);
xpathEval = xpath.booleanValueOf(this.session.getDocumentElement());
} else {
// assume XmlPage
XPath xpath = new DOMXPath(expandedXPath);
xpath.setNamespaceContext(WebtestCase.XML_NAMESPACE_CONTEXT);
xpathEval = xpath.booleanValueOf(((XmlPage) curPage).getXmlDocument());
}
if (xpathEval != true) {
Assert.fail("XPath '" + expandedXPath + "' evaluates to false.");
}
} catch (JaxenException e) {
Assert.fail("Could not evaluate xpath '" + expandedXPath + "' (" +
e.getMessage() + ")");
}
}
The session object retrieves either the HTMLUnit Page object or the document
element of an HTML page. The first line just expands inline properties -- it is
not relevant here. It is pretty hacked but works for all the cases it has to
work (~300 converted Webtest XML files). A proper solution would require
factoring the HTML/XML distinction out, but shouldn't be too hard otherwise.
----------------------------------------------------------------------
Comment By: Jevon Wright (jevonwright)
Date: 2008-11-18 15:19
Message:
Hi, you can now do this in the latest SVN trunk of JWebUnit:
IElement element = getElementByXPath("//li"); // get only first element
assertNotNull(element);
assertEquals("hello, world!", element.getTextContent());
assertMatch("o.+o", element.getTextContent());
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=497985&aid=1637716&group_id=61302
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
JWebUnit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development