Skip to content

Instantly share code, notes, and snippets.

@rhoegg
Last active August 29, 2015 14:02
Show Gist options
  • Save rhoegg/2029e51e298404e486e7 to your computer and use it in GitHub Desktop.
Save rhoegg/2029e51e298404e486e7 to your computer and use it in GitHub Desktop.
groovy methods for XPath assertions
static final Map<String, String> NAMESPACES = [
env: 'http://schemas.xmlsoap.org/soap/envelope/',
xsd: 'http://www.w3.org/2001/XMLSchema',
xsi: 'http://www.w3.org/2001/XMLSchema-instance'
]
@Before
void initXml() {
evaluator = XPathFactory.newInstance().newXPath()
evaluator.setNamespaceContext([
getNamespaceURI: { prefix ->
NAMESPACES[prefix]
}
] as NamespaceContext)
def factory = DocumentBuilderFactory.newInstance()
factory.namespaceAware = true
builder = factory.newDocumentBuilder()
assert builder.namespaceAware
}
def evalXpath(String xpath, String xml) {
evalXpath(xpath, xml, XPathConstants.STRING)
}
def evalXpath(String xpath, String xml, QName resultType) {
def rootElement = builder.parse(new ByteArrayInputStream(xml.bytes)).documentElement
evaluator.evaluate(xpath, rootElement, resultType)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment