Exemplo n.º 1
0
  /**
   * Method that reads a XML-file, and returns a Model that contains the information
   *
   * @param file
   * @return
   * @return
   */
  public static Model readXML(String file) {
    // initialize table to be filled with content of XML file
    Model t = new Model();
    try {
      // Create file to be parsed by document parser
      File xmlfile = new File(file);
      // create parser
      DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      // parse the file
      Document parsedfile = parser.parse(xmlfile);
      // normalize the parsed file (make it more user-friendly)
      parsedfile.getDocumentElement().normalize();

      NodeList cells = parsedfile.getElementsByTagName("CELL");
      for (int i = 0; i < cells.getLength(); i++) {
        // Get cell at list index i
        Node currentcell = cells.item(i);
        // read the elements "location" attributes row/column
        if (Node.ELEMENT_NODE == currentcell.getNodeType()) {
          Element cellinfo = (Element) currentcell;
          // get the row number from node attribute
          int row = Integer.parseInt(cellinfo.getAttribute("row")) - 1;
          // get the column number from the node attribute
          int col = Integer.parseInt(cellinfo.getAttribute("column")) - 1;
          // get content from node
          String content = cellinfo.getTextContent();
          if (content != null) {
            content = content.replace("\n", "");
          }
          // Make the content an Integer (if it is a number), easier
          // for
          // using it later on
          // put content in table, with row/column inserted as x/y
          t.setContent(row, col, (String) content);
        }
      }

    } catch (ParserConfigurationException e) {
      System.out.println("Fileparser could not be made");
    } catch (IOException f) {
      System.out.println("File could not be parsed, did you enter the correct file name?");
    } catch (SAXException g) {
      System.out.println("Something went wrong in parsing the file");
    }
    return t;
  }
  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;
  }
  public boolean runSuite(String filename) throws Exception {
    if (this.verbose) {
      System.out.println(
          "Running test suite "
              + filename
              + " against "
              + this.host
              + ":"
              + this.port
              + " with "
              + this.browser);
    }
    TestSuite suite = new TestSuite();
    long start = System.currentTimeMillis();
    suite.numTestPasses = 0;
    suite.file = new File(filename);
    File suiteDirectory = suite.file.getParentFile();
    this.document = parseDocument(filename);
    Element table = (Element) this.document.getElementsByTagName("table").item(0);
    NodeList tableRows = table.getElementsByTagName("tr");
    Element tableNameRow = (Element) tableRows.item(0);
    suite.name = tableNameRow.getTextContent();
    suite.result = true;
    suite.tests = new Test[tableRows.getLength() - 1];
    for (int i = 1; i < tableRows.getLength(); i++) {
      Element tableRow = (Element) tableRows.item(i);
      Element cell = (Element) tableRow.getElementsByTagName("td").item(0);
      Element link = (Element) cell.getElementsByTagName("a").item(0);
      Test test = new Test();
      test.label = link.getTextContent();
      test.file = new File(suiteDirectory, link.getAttribute("href"));

      SeleniumHtmlClient subclient = new SeleniumHtmlClient();
      subclient.setHost(this.host);
      subclient.setPort(this.port);
      subclient.setBrowser(this.browser);
      // subclient.setResultsWriter(this.resultsWriter);
      subclient.setBaseUrl(this.baseUrl);
      subclient.setVerbose(this.verbose);
      subclient.runTest(test);
      if (test.result) {
        suite.numTestPasses++;
      }
      suite.result &= test.result;
      suite.tests[i - 1] = test;
    }
    long end = System.currentTimeMillis();

    suite.totalTime = (end - start) / 1000;

    if (this.resultsWriter != null) {
      this.resultsWriter.write("<html><head>\n");
      this.resultsWriter.write("<style type='text/css'>\n");
      this.resultsWriter.write(
          "body, table {font-family: Verdana, Arial, sans-serif;font-size: 12;}\n");
      this.resultsWriter.write("table {border-collapse: collapse;border: 1px solid #ccc;}\n");
      this.resultsWriter.write("th, td {padding-left: 0.3em;padding-right: 0.3em;}\n");
      this.resultsWriter.write("a {text-decoration: none;}\n");
      this.resultsWriter.write(".title {font-style: italic;}");
      this.resultsWriter.write(".selected {background-color: #ffffcc;}\n");
      this.resultsWriter.write(".status_done {background-color: #eeffee;}\n");
      this.resultsWriter.write(".status_passed {background-color: #ccffcc;}\n");
      this.resultsWriter.write(".status_failed {background-color: #ffcccc;}\n");
      this.resultsWriter.write(
          ".breakpoint {background-color: #cccccc;border: 1px solid black;}\n");
      this.resultsWriter.write("</style>\n");
      this.resultsWriter.write("<title>" + suite.name + "</title>\n");
      this.resultsWriter.write("</head><body>\n");
      this.resultsWriter.write("<h1>Test suite results </h1>\n\n");
      this.resultsWriter.write("<table>\n");
      this.resultsWriter.write(
          "<tr>\n<td>result:</td>\n<td>" + (suite.result ? "passed" : "failed") + "</td>\n</tr>\n");
      this.resultsWriter.write(
          "<tr>\n<td>totalTime:</td>\n<td>" + suite.totalTime + "</td>\n</tr>\n");
      this.resultsWriter.write(
          "<tr>\n<td>numTestTotal:</td>\n<td>" + suite.tests.length + "</td>\n</tr>\n");
      this.resultsWriter.write(
          "<tr>\n<td>numTestPasses:</td>\n<td>" + suite.numTestPasses + "</td>\n</tr>\n");
      int numTestFailures = suite.tests.length - suite.numTestPasses;
      this.resultsWriter.write(
          "<tr>\n<td>numTestFailures:</td>\n<td>" + numTestFailures + "</td>\n</tr>\n");
      this.resultsWriter.write("<tr>\n<td>numCommandPasses:</td>\n<td>0</td>\n</tr>\n");
      this.resultsWriter.write("<tr>\n<td>numCommandFailures:</td>\n<td>0</td>\n</tr>\n");
      this.resultsWriter.write("<tr>\n<td>numCommandErrors:</td>\n<td>0</td>\n</tr>\n");
      this.resultsWriter.write("<tr>\n<td>Selenium Version:</td>\n<td>2.24</td>\n</tr>\n");
      this.resultsWriter.write("<tr>\n<td>Selenium Revision:</td>\n<td>.1</td>\n</tr>\n");

      // test suite
      this.resultsWriter.write("<tr>\n<td>\n");
      this.resultsWriter.write(
          "<table id=\"suiteTable\" class=\"selenium\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\"><tbody>\n");
      this.resultsWriter.write(
          "<tr class=\"title "
              + (suite.result ? "status_passed" : "status_failed")
              + "\"><td><b>Test Suite</b></td></tr>\n");

      int i = 0;
      for (Test test : suite.tests) {
        this.resultsWriter.write(
            "<tr class=\""
                + (test.result ? "status_passed" : "status_failed")
                + "\"><td><a href=\"#testresult"
                + i
                + "\">"
                + test.name
                + "</a></td></tr>");
        i++;
      }
      this.resultsWriter.write(
          "</tbody></table>\n</td>\n<td>&nbsp;</td>\n</tr>\n</table>\n<table>");
      int j = 0;
      for (Test test : suite.tests) {
        this.resultsWriter.write(
            "<tr><td><a name=\"testresult" + j + "\">" + test.file + "</a><br/><div>\n");
        this.resultsWriter.write("<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\">\n");
        this.resultsWriter.write(
            "<thead>\n<tr class=\"title "
                + (test.result ? "status_passed" : "status_failed")
                + "\"><td rowspan=\"1\" colspan=\"3\">"
                + test.name
                + "</td></tr>");
        this.resultsWriter.write("</thead><tbody>\n");
        for (Command command : test.commands) {
          boolean result = command.result.startsWith("OK");
          boolean isAssert = command.cmd.startsWith("assert") || command.cmd.startsWith("verify");
          ;
          if (!isAssert) {
            this.resultsWriter.write(
                "<tr class=\"" + (result ? "status_done" : "") + "\">\n<td>\n");
          } else {
            this.resultsWriter.write(
                "<tr class=\"" + (result ? "status_passed" : "status_failed") + "\">\n<td>\n");
          }
          this.resultsWriter.write(command.cmd);
          this.resultsWriter.write("</td>\n");
          if (command.args != null) {
            for (String arg : Arrays.asList(command.args)) {
              this.resultsWriter.write("<td>" + arg + "</td>\n");
            }
          }
        }
        this.resultsWriter.write("</tr>\n");
        this.resultsWriter.write("</tbody></table>\n");
        this.resultsWriter.write("</div></td>\n<td>&nbsp;</td>\n</tr>");

        j++;
      }

      int k = 0;
      for (Test test : suite.tests) {

        k++;
      }

      this.resultsWriter.write("</tbody></table>\n</td><td>&nbsp;</td>\n</tr>\n</table>\n");
      this.resultsWriter.write("</body></html>");
    }
    return suite.result;
  }
Exemplo n.º 4
0
  public ShowFileData loadShow(String s) {
    Vector<Pair> links = new Vector<Pair>();
    /*
    ShowFileData show = new ShowFileData();
    show.setSettings(new CameoSettings());
    show.setPatch(new CameoPatch(0,0));
    show.setCueList(new CueList(0));
    show.setMagicSheet(new MagicSheet());
    */
    SettingsData settingsData = new SettingsData();
    MagicSheetData magicSheetData = new MagicSheetData();
    CueListData cueListData = new CueListData();
    PatchData patchData = new PatchData();

    Document doc;
    try {
      doc = _builder.parse(s);

      Element root = doc.getDocumentElement();
      Element settings = getElement(root, _settings);
      Element patch = getElement(root, _patch);
      Element cues = getElement(root, _cues);
      Element magic = getElement(root, _magicSheet);
      // TODO test that all of these are non null and report incorrect file missing ...

      String recordMode = getElement(settings, _recordMode).getTextContent();
      settingsData.put(_recordMode, recordMode);
      /*
      if(recordMode.equals(RecordMode.TRACKING.toString()))
      	show._settings._mode = RecordMode.TRACKING;
      else if(recordMode.equals(RecordMode.CUE_ONLY.toString()))
      	show._settings._mode = RecordMode.CUE_ONLY;
      */
      settingsData.put(_totalChannels, getElement(settings, _totalChannels).getTextContent());
      settingsData.put(_totalDimmers, getElement(settings, _totalDimmers).getTextContent());
      settingsData.put(_dUpTime, getElement(settings, _dUpTime).getTextContent());
      settingsData.put(_dDownTime, getElement(settings, _dDownTime).getTextContent());
      settingsData.put(_gotoCueTime, getElement(settings, _gotoCueTime).getTextContent());
      settingsData.put(_title, getElement(settings, _title).getTextContent());
      settingsData.put(_comment, getElement(settings, _comment).getTextContent());
      settingsData.put(_channelPerLine, getElement(settings, _channelPerLine).getTextContent());
      settingsData.put(_channelHGroup, getElement(settings, _channelHGroup).getTextContent());
      settingsData.put(_channelVGroup, getElement(settings, _channelVGroup).getTextContent());

      /*
      show._settings._totalChannels = Integer.parseInt(getElement(settings, _totalChannels).getTextContent());
      show._settings._totalDimmers = Integer.parseInt(getElement(settings, _totalDimmers).getTextContent());
      show._settings._upTime = Integer.parseInt(getElement(settings, _dUpTime).getTextContent());
      show._settings._downTime = Integer.parseInt(getElement(settings, _dDownTime).getTextContent());
      show._settings._gotoCueTime = Integer.parseInt(getElement(settings, _gotoCueTime).getTextContent());
      show._settings._showTitle = getElement(settings, _title).getTextContent();
      show._settings._showComment = getElement(settings, _comment).getTextContent();

      show._settings._ChannelsPerLine = Integer.parseInt(getElement(settings, _channelPerLine).getTextContent());
      show._settings._ChannelGrouping = Integer.parseInt(getElement(settings, _channelHGroup).getTextContent());
      show._settings._LineGrouping = Integer.parseInt(getElement(settings, _channelVGroup).getTextContent());
      */

      int totalChannels = Integer.parseInt(settingsData.get(_totalChannels));

      patchData.setTotalChannels(totalChannels);
      patchData.setTotalDimmers(Integer.parseInt(settingsData.get(_totalDimmers)));

      cueListData.setTotalChannels(totalChannels);

      // show.setPatch(new CameoPatch(show._settings._totalChannels, show._settings._totalDimmers));
      // show.setCueList(new CueList(show._settings._totalChannels));

      NodeList list = patch.getElementsByTagName(_channel);
      for (int x = 0; x < list.getLength(); x++) {
        Element el = (Element) list.item(x);
        int number = Integer.parseInt(el.getAttribute(_number));
        patchDims(patchData, number, el.getTextContent());
      }

      list = cues.getElementsByTagName(_cue);
      for (int x = 0; x < list.getLength(); x++) {
        Element el = (Element) list.item(x);
        CueData cue = new CueData();
        cue.setDescription(el.getAttribute(_discription));
        NodeList list2 = el.getElementsByTagName(_channel);
        for (int y = 0; y < list2.getLength(); y++) {
          Element chan = (Element) list2.item(y);
          setLevel(cue, chan.getAttribute(_number), chan.getTextContent());
        }

        int number = Integer.parseInt(el.getAttribute(_number));

        int upTime = Integer.parseInt(el.getAttribute(_uptime));
        int downTime = Integer.parseInt(el.getAttribute(_downTime));
        int delayUpTime = Integer.parseInt(el.getAttribute(_upTimeDelay));
        int delayDownTime = Integer.parseInt(el.getAttribute(_downTimeDelay));

        FadeData fade = new FadeData(number, upTime, downTime, delayUpTime, delayDownTime, cue);

        if (el.hasAttribute(_followtime))
          fade.setFollowTime(Integer.parseInt(el.getAttribute(_followtime)));

        if (el.hasAttribute(_nextCue)) fade.setNextCue(Integer.parseInt(el.getAttribute(_nextCue)));

        cueListData.add(fade);
      }

      // TODO this needs to be done in the extractor
      // for(Pair p : links)
      //	show._cuelist.getCueNumbered(p._first).setNextCue(show._cuelist.getCueNumbered(p._second));

      list = magic.getElementsByTagName(_channel);
      for (int x = 0; x < list.getLength(); x++) {
        Element el = (Element) list.item(x);
        // Element xval = getElement(el, _x);
        // Element yval = getElement(el, _y);
        Integer number = new Integer(Integer.parseInt(el.getAttribute(_number)) - 1);
        double xpos = Double.parseDouble(el.getAttribute(_x));
        double ypos = Double.parseDouble(el.getAttribute(_y));
        magicSheetData.put(number, new MagicChannelData(xpos, ypos));
      }

    } catch (DOMException e) {
      System.err.println(e);
    } catch (SAXException e) {
      System.err.println(e);
    } catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(
          new JFrame(""), "The show file " + s + " was not found, loading default show.");
    } catch (IOException e) {
      System.err.println(e);
    }

    return new ShowFileData(settingsData, cueListData, patchData, magicSheetData);
  }