/** * Return the result of an XPath query on an XML Document in int format * * @param query - The XPath query * @return int - Result of the query * @throws Exception */ public int performXPathQueryReturnInteger(String query) throws Exception { return Integer.parseInt(performXPathQueryReturnString(query)); }
/** * Create an Element reference. * * @param locator - XPath location of the element. * @return Element - Element object referenced by the XPath locator. * @throws XPathExpressionException */ private Element getElement(String locator) throws XPathExpressionException { return (Element) constructAnXPathExpression(locator).evaluate(this.xmlDocument, XPathConstants.NODE); }
public XMLHandler(File absoluteFile) throws Exception { if (absoluteFile.exists()) { createDocumentAndCollectPrefixes(new InputSource(new FileReader(absoluteFile)), false); } else { throw new IOException("File does not exist!"); } }
XMLHandler testResults = new XMLHandler(new File(this.htmlTestTemplate)); testResults.addTextToElement(this.testSuiteName, "//p[@id='name']/span"); testResults.addTextToElement(this.testSuiteAuthor, "//p[@id='author']/span"); testResults.addTextToElement(stories, "//p[@id='story']/span"); testResults.addTextToElement(formattedTime(this.suiteRunTime), "//p[@id='time']/span"); while (test.hasNext()) { Map.Entry pairs = (Map.Entry) test.next(); testResults.addChildElement("tr", "//table[@id='testResults']/tbody"); rowNumber++; TestData individualTestResults = (TestData) pairs.getValue(); testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]"); testResults.addTextToElement(individualTestResults.getTestName(), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[1]"); testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]"); testResults.addTextToElement(Integer.valueOf(individualTestResults.getFailures()).toString(), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[2]"); String result = ""; if (individualTestResults.getFailures() == 0) { testResults.addAttribute("class", result.toLowerCase(), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]"); testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]"); testResults.addTextToElement(result, "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[3]"); testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]"); testResults.addTextToElement(formattedTime(individualTestResults.getTimeTaken()), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[4]"); testResults.addTextToElement(overallResult, "//h2[@id='overallResult']/span");
/** * Reads in a sky.sns.selenium IDE file and creates Sky Selenium format test code * * @param filename - Selenium IDE file to convert * @return Name of the Selenium IDE file * @throws Exception */ public String generateTestCode(String filename) throws Exception { FileHandler convertFrom = new FileHandler(convertToXML(filename)); XMLHandler seleniumXMLFile = new XMLHandler(convertFrom.getFile()); int commandCount = seleniumXMLFile.performXPathQueryReturnInteger("count(/html/body/table/tbody/tr)"); for (int i = 1; i <= commandCount; i++) { String command = ""; String target = ""; String value = ""; try { command = seleniumXMLFile.performXPathQueryReturnString("//table/tbody/tr[" + i + "]/td[1]"); target = seleniumXMLFile.performXPathQueryReturnString("/html/body/table/tbody/tr[" + i + "]/td[2]"); value = seleniumXMLFile.performXPathQueryReturnString("/html/body/table/tbody/tr[" + i + "]/td[3]"); } catch (Exception Ex) { LOGGER.warn("Invalid command '{}' found", command); } addToTestCode(codeGenerator.convertCommandToEbselenCode(command, target, value)); } convertFrom.close(); return convertFrom.getFileName().split("\\.")[0]; }
/** * Add an attribute to an element. * * @param attributeType - Attribute to add (e.g. class). * @param value - Value to assign to the attribute. * @param locator - XPath location of the attribute. * @throws Exception */ public void addAttribute(String attributeType, String value, String locator) throws Exception { Element element = getElement(locator); element.setAttribute(attributeType, value); }
/** * Add text to an element. * * @param text - Text to add to the element. * @param locator - XPath location of the element. * @throws Exception */ public void addTextToElement(String text, String locator) throws Exception { Element element = getElement(locator); Node textNode = xmlDocument.createTextNode(text); element.appendChild(textNode); }
public XMLHandler(File absoluteFile, boolean scanEntireDocumentForNamespaces) throws Exception { if (absoluteFile.exists()) { createDocumentAndCollectPrefixes(new InputSource(new FileReader(absoluteFile)), scanEntireDocumentForNamespaces); } else { throw new IOException("File does not exist!"); } }
/** * Add a child element to an existing element (Will go to the end of the list) * * @param elementType - Type of child element to add (e.g. div) * @param locator - XPath location of the element. * @throws Exception */ public void addChildElement(String elementType, String locator) throws Exception { Element element = getElement(locator); Node childNode = xmlDocument.createElement(elementType); element.appendChild(childNode); }
/** * Return the result of an XPath query on an XML Document in String format * * @param query - The XPath query * @return String - Result of the query * @throws Exception */ public String performXPathQueryReturnString(String query) throws Exception { return constructAnXPathExpression(query).evaluate(this.xmlDocument, XPathConstants.STRING).toString(); }
public XMLHandler(String sourceXML) throws Exception { createDocumentAndCollectPrefixes(new InputSource(new StringReader(sourceXML.trim().replaceFirst("^([\\W]+)<", "<"))), false); }
public XMLHandler(String sourceXML, boolean scanEntireDocumentForNamespaces) throws Exception { createDocumentAndCollectPrefixes(new InputSource(new StringReader(sourceXML.trim().replaceFirst("^([\\W]+)<", "<"))), scanEntireDocumentForNamespaces); }