Beispiel #1
0
  /**
   * Gets XML string for this definition so that it may be printed.
   *
   * @return XML node that can be saved of this word.
   */
  public Element getWordXMLNode(Document doc) {
    try {
      Element rootWordElement = doc.createElement("WordDefinitions");
      rootWordElement.setAttribute("word", this.word);
      rootWordElement.setAttribute("mainDefinition", this.getMainDefinition());
      if (this.otherDefinitions == null) {
        this.getDefinitions();
      }
      for (int i = 0; i < this.otherDefinitions.length; i++) {
        // main definition node
        Element definition = doc.createElement("Definition");

        // word
        Element word = doc.createElement("Word");
        word.setTextContent(otherDefinitions[i].getWord());

        // dictionary
        Element dictionary = doc.createElement("Dictionary");
        Element id = doc.createElement("Id");
        id.setTextContent(otherDefinitions[i].getId());
        Element source = doc.createElement("Name");
        source.setTextContent(otherDefinitions[i].getSource());
        dictionary.appendChild(id);
        dictionary.appendChild(source);

        // definition
        Element wordDefinition = doc.createElement("WordDefinition");
        wordDefinition.setTextContent(otherDefinitions[i].getDefinition());

        // append all the children
        definition.appendChild(word);
        definition.appendChild(dictionary);
        definition.appendChild(wordDefinition);
        rootWordElement.appendChild(definition);
      }
      return rootWordElement;
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    }
  }
 void appendCellToRow(Element tableRow, String content) {
   Element newCell = document.createElement("td");
   newCell.setTextContent(content);
   tableRow.appendChild(newCell);
 }
  public boolean runTest(Test test) throws Exception {
    String filename = test.file.toString();
    if (this.verbose) {
      System.out.println(
          "Running "
              + filename
              + " against "
              + this.host
              + ":"
              + this.port
              + " with "
              + this.browser);
    }
    this.document = parseDocument(filename);

    if (this.baseUrl == null) {
      NodeList links = this.document.getElementsByTagName("link");
      if (links.getLength() != 0) {
        Element link = (Element) links.item(0);
        setBaseUrl(link.getAttribute("href"));
      }
    }
    if (this.verbose) {
      System.out.println("Base URL=" + this.baseUrl);
    }

    Node body = this.document.getElementsByTagName("body").item(0);
    Element resultContainer = document.createElement("div");
    resultContainer.setTextContent("Result: ");
    Element resultElt = document.createElement("span");
    resultElt.setAttribute("id", "result");
    resultElt.setIdAttribute("id", true);
    resultContainer.appendChild(resultElt);
    body.insertBefore(resultContainer, body.getFirstChild());

    Element executionLogContainer = document.createElement("div");
    executionLogContainer.setTextContent("Execution Log:");
    Element executionLog = document.createElement("div");
    executionLog.setAttribute("id", "log");
    executionLog.setIdAttribute("id", true);
    executionLog.setAttribute("style", "white-space: pre;");
    executionLogContainer.appendChild(executionLog);
    body.appendChild(executionLogContainer);

    NodeList tableRows = document.getElementsByTagName("tr");
    Element theadRow = (Element) tableRows.item(0);
    test.name = theadRow.getTextContent();
    appendCellToRow(theadRow, "Result");

    this.commandProcessor =
        new HtmlCommandProcessor(this.host, this.port, this.browser, this.baseUrl);
    String resultState;
    String resultLog;
    test.result = true;
    try {
      this.commandProcessor.start();
      test.commands = new Command[tableRows.getLength() - 1];
      for (int i = 1; i < tableRows.getLength(); i++) {
        Element stepRow = (Element) tableRows.item(i);
        Command command = executeStep(stepRow);
        appendCellToRow(stepRow, command.result);
        test.commands[i - 1] = command;
        if (command.error) {
          test.result = false;
        }
        if (command.failure) {
          test.result = false;
          // break;
        }
      }
      resultState = test.result ? "PASSED" : "FAILED";
      resultLog = (test.result ? "Test Complete" : "Error");
      this.commandProcessor.stop();
    } catch (Exception e) {
      test.result = false;
      resultState = "ERROR";
      resultLog = "Failed to initialize session\n" + e;
      e.printStackTrace();
    }
    document.getElementById("result").setTextContent(resultState);
    Element log = document.getElementById("log");
    log.setTextContent(log.getTextContent() + resultLog + "\n");
    return test.result;
  }