/** * Get Xpath Text Contents */ public List<String> getXpathTextContents(String xpathExpression) throws TransformerException, XPathExpressionException { NodeList nodeList = getNodeList(xpathExpression); if (nodeList.getLength() == 0) { return Collections.emptyList(); } List<String> list = new ArrayList<String>(); for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); list.add(element.getTextContent()); } LOG.createAppender() .appendBold("Get Xpath Text Contents:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Text Contents", String.valueOf(list)) .log(); return list; }
public int getJsonListLength(String jsonExpression) { validate(); try { ScriptEngineManager manager = new ScriptEngineManager(); engine = manager.getEngineByName("JavaScript"); engine.eval("var json = " + jsonString + ";"); engine.eval("var jsonExpr = json." + jsonExpression + ".length;"); int length = ((Number) engine.get("jsonExpr")).intValue(); LOG.createAppender() .appendBold("Get JSON List Length:") .appendProperty("Json Expression", jsonExpression) .appendProperty("Length", length) .log(); return length; } catch(Exception e) { throw new IllegalStateException("Response is not json.", e); } }
private NodeList getNodeList(Element base, String xpathExpression) throws TransformerException, IOException, SAXException, XPathExpressionException, ParserConfigurationException { validate(); String baseString = XMLFormatter.prettyPrint(base); LOG.createAppender() .appendBold("Node XML String:") .appendXML(baseString) .log(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(new InputSource(new StringReader(baseString))); NodeList nodeList = (NodeList) xPath.evaluate(xpathExpression, document, XPathConstants.NODESET); if (nodeList == null) { throw new IllegalArgumentException(String.format("Xpath Expression '%s' not found.", xpathExpression)); } LOG.createAppender() .appendBold("Get Node List:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Number of Elements Found", nodeList.getLength()) .log(); return nodeList; }
public void siteCatalystVariableMapShouldContain(HashMap<String,String> map, String scVar, String expectedValue){ try { String actualValue = map.get(scVar); LOG.createAppender() .appendBold("Site Catalyst Variable And Value:") .appendProperty("scVar", scVar) .appendProperty("Expected Value:", expectedValue) .appendProperty("Actual Value:", actualValue) .log(); if (!actualValue.equals(expectedValue)) { throw new AssertionError("The value of the variable contained in the map is not as expected."); } } catch (Exception e) { LOG.createAppender() .appendBold("No Site Catalyst Variable(s) Found In Map.") .log(); throw new IllegalStateException("No Site Catalyst Variable(s) Found In Map."); } }
public void setXmlString(String xmlString) throws IOException, SAXException, ParserConfigurationException { this.xmlString = xmlString; if(StringUtils.startsWith(xmlString, "file:") || StringUtils.startsWith(xmlString, "classpath:")) { ResourceEditor editor = new ResourceEditor(); editor.setAsText(xmlString); Resource resource = (Resource) editor.getValue(); xmlString = new String(IOUtils.toCharArray(resource.getInputStream())); } DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); LOG.createAppender() .appendProperty("DocumentBuilderFactory class", dbf.getClass().getName()) .appendProperty("XPathFactory class", xPathFactory.getClass().getName()) .appendBold("XML String:") .appendXML(XMLFormatter.prettyPrint(xmlString)) .log(); this.document = db.parse(new InputSource(new StringReader(xmlString))); }
public List<Object> getJsonValues(String jsonExpression) { validate(); Object jsonValue; if(StringUtils.startsWith(jsonExpression, "$")) { jsonValue = JsonPath.read(jsonString, jsonExpression); } else { jsonValue = JsonPath.read(jsonString, "$." + jsonExpression); } List items; if(jsonValue instanceof List) { items = (List) jsonValue; } else { items = Arrays.asList(jsonValue); } LOG.createAppender() .appendBold("Get JSON Values:") .appendProperty("Json Expression", jsonExpression) .appendProperty("Size", items.size()) .log(); return items; }
public void siteCatalystVariableListShouldContain(Collection<String> aList, String expectedKeyValuePair){ try { LOG.createAppender() .appendBold("Site Catalyst Variable And Value:") .appendProperty("Expected Key-Value Pair In List:", expectedKeyValuePair) .appendCode(printList((ArrayList<String>) aList)) .log(); if (!aList.contains(expectedKeyValuePair)) { throw new AssertionError("The expected key-value pair is not present in the list."); } } catch (Exception e) { LOG.createAppender() .appendBold("No Site Catalyst Variable(s) Found In List.") .log(); throw new IllegalStateException("No Site Catalyst Variable(s) Found In List."); } }
@Override public Object execute(Object[] params) { Element element = (Element) params[0]; String attributeName = String.valueOf(params[1]); String expectedValue = String.valueOf(params[2]); String result = element.getAttribute(attributeName); LOG.createAppender() .appendBold("Get Element Attribute:") .appendProperty("Attribute", attributeName) .appendProperty("Actual", result) .appendProperty("Expected", expectedValue) .log(); if(!StringUtils.equals(result, expectedValue)) { throw new IllegalArgumentException("Attribute name and expected value did not match."); } return null; } }
/** * Get Xpath Single Text Contents */ public String getXPathSingleTextContent(String xpathExpression) throws TransformerException, XPathExpressionException { validate(); Element element = (Element) xPath.evaluate(xpathExpression, document, XPathConstants.NODE); if (element == null) { throw new IllegalArgumentException(String.format("Xpath Expression '%s' not found.", xpathExpression)); } LOG.createAppender() .appendBold("Get Xpath Single Text Content:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Text Content", element.getTextContent()) .log(); return element.getTextContent(); }
@SuppressWarnings("unchecked") public List<Object> getJsonValues(JSONObject jsonObject, String jsonExpression) { validate(); Object jsonValue = JsonPath.read(jsonObject.toJSONString(), "$." + jsonExpression); List items; if(jsonValue instanceof List) { items = (List) jsonValue; } else { items = Arrays.asList(jsonValue); } LOG.createAppender() .appendBold("Get JSON Values:") .appendProperty("Json Expression", jsonExpression) .appendProperty("Size", items.size()) .log(); return items; }
public Object getJsonValue(String jsonExpression) { validate(); if(jsonExpression.equals("*")) { return getRoot(); } Object jsonValue = JsonPath.read(jsonString, jsonExpression); if(jsonValue instanceof List) { jsonValue = ((List) jsonValue).iterator().next(); } LOG.createAppender() .appendBold("Get JSON Value:") .appendProperty("Json Expression", jsonExpression) .appendProperty("Json Value", String.valueOf(jsonValue)) .log(); return jsonValue; }
/** * XPath Text Content Should be Equal */ public void xpathTextContentShouldBeEqual (String xpathExpression, String expectedValue) throws TransformerException, XPathExpressionException { String value = getXPathSingleTextContent(xpathExpression); LOG.createAppender() .appendBold("Xpath Text Content Should be Equal:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Expected Value", expectedValue) .appendProperty("Actual Value", value) .log(); if (!StringUtils.equals(value, expectedValue)) { throw new IllegalArgumentException(String.format("Expecting text content '%s' but was '%s'", expectedValue, value)); } }
@Override public Object execute(Object[] params) throws ParserConfigurationException, IOException, SAXException { Element element = (Element) params[0]; String attributeName = String.valueOf(params[1]); String result = element.getAttribute(attributeName); LOG.createAppender() .appendBold("Get Element Attribute:") .appendProperty("Attribute", attributeName) .appendProperty("Result", result) .log(); return result; } }
public void xpathShouldMatchXTimes (String xpathExpression, int numberOfTimes) throws TransformerException, XPathExpressionException { int nodeLength = getNodeList(xpathExpression).getLength(); LOG.createAppender() .appendBold("Xpath Should Match X Times:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Expected Number of Times", numberOfTimes) .appendProperty("Actual Number of Times", nodeLength) .log(); if (nodeLength != numberOfTimes) { throw new IllegalArgumentException(String.format("Expecting '%d' occurrences of '%s' expression but was '%d'", numberOfTimes, xpathExpression, nodeLength)); } }
private NodeList getNodeList(String xpathExpression) throws TransformerException, XPathExpressionException { validate(); NodeList nodeList = (NodeList) xPath.evaluate(xpathExpression, document, XPathConstants.NODESET); if (nodeList == null) { throw new IllegalArgumentException(String.format("Xpath Expression '%s' not found.", xpathExpression)); } LOG.createAppender() .appendBold("Get Node List:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Number of Elements Found", nodeList.getLength()) .log(); return nodeList; } }
public int getXpathMatchCount(String xpathExpression) throws TransformerException, XPathExpressionException { int nodeLength = getNodeList(xpathExpression).getLength(); LOG.createAppender() .appendBold("Get Xpath Match Count:") .appendProperty("Xpath Expression", xpathExpression) .appendProperty("Actual Found", nodeLength) .log(); return nodeLength; }