Last active
August 29, 2015 14:02
-
-
Save rhoegg/2029e51e298404e486e7 to your computer and use it in GitHub Desktop.
groovy methods for XPath assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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