-
Notifications
You must be signed in to change notification settings - Fork 2
Creating XPath Extensions
Madcow delegates to WebTest (which in turn uses the apache xpath functions) to process the xpath queries in both mappings and test scripts.
WebTest supports much of the XPath 1.0 specification, but sometimes there are xpath functions from other specifications (such as ends-with from XPath 2.0),
or simply a helper function to make your xpath cleaner, which will help in the test/mappings creation.
In order to implement a custom XPath function, there must be a class which extends one of the apache xpath function base classes - see Xalan-Java Documentation. For this example, we will create an xpath function, ends-with, which takes two parameters - one for the xpath query to search within, and the other for the string to check if the supplied xpath query has that text at the end of it.
The ends-with function is already implemented in Madcow, accessible through
madcow:ends-with
package au.com.ts4impact.madcow.extension.webtest.xpath
import javax.xml.transform.TransformerException
import org.apache.xpath.XPathContext
import org.apache.xpath.functions.Function2Args
import org.apache.xpath.objects.XObject
import org.apache.xpath.objects.XBoolean
public class EndsWith extends Function2Args {
public XObject execute(final XPathContext context) throws TransformerException {
final String xpathQuery = getArg0().execute(context).xstr().toString();
final String endsWithString = getArg1().execute(context).xstr().toString();
// returns an xpath boolean, testing that the xpath query ends with the specified string
return new XBoolean(xpathQuery.endsWith(endsWithString));
}
}In order for the xpath function to be accessible from the mappings/test scripts, it must be registered with the XPathHelper object. This contains static methods to register the namespace and function details. Continuing with the example of the ends-with function, it will be registered with the XPathHelper, in the madcow namespace, accessed through the syntax of madcow:ends-with.
// register the root namespace, so all functions are prefixed with madcow:
XPathHelper.registerGlobalNamespace("madcow", "http://4impact.com.au")
// register the ends with function to delegate to the EndsWith class
XPathHelper.registerGlobalFunction("http://4impact.com.au", "ends-with", EndsWith.class)Registration of XPath Namespace and Functions must occur in the setUp() phase of the test script running
- Home
- Setting Up
- Configuration
- Writing Madcow Tests
- Running Madcow Tests
- Data Parameters
- Templates
- Macros
- Disabling A Test
- Spreadsheet Scenario Testing
Madcow Operations
- Madcow Operations
- Madcow Operations - Table
- Madcow Operations - XPath Extras
- List of Madcow Operations
Extending and Customising Madcow
Reference
For Developers