testing/marionette/marionette-common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/marionette/marionette-common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + *
    1.10 + * This file contains common code that is shared between marionette-server.js
    1.11 + * and marionette-listener.js.
    1.12 + *
    1.13 + */
    1.14 +
    1.15 +/**
    1.16 + * Creates an error message for a JavaScript exception thrown during
    1.17 + * execute_(async_)script.
    1.18 + *
    1.19 + * This will generate a [msg, trace] pair like:
    1.20 + *
    1.21 + * ['ReferenceError: foo is not defined',
    1.22 + *  'execute_script @test_foo.py, line 10
    1.23 + *   inline javascript, line 2
    1.24 + *   src: "return foo;"']
    1.25 + *
    1.26 + * @param error An Error object passed to a catch() clause.
    1.27 +          fnName The name of the function to use in the stack trace message
    1.28 +                 (e.g., 'execute_script').
    1.29 +          pythonFile The filename of the test file containing the Marionette
    1.30 +                  command that caused this exception to occur.
    1.31 +          pythonLine The line number of the above test file.
    1.32 +          script The JS script being executed in text form.
    1.33 + */
    1.34 +this.createStackMessage = function createStackMessage(error, fnName, pythonFile,
    1.35 +  pythonLine, script) {
    1.36 +  let python_stack = fnName + " @" + pythonFile;
    1.37 +  if (pythonLine !== null) {
    1.38 +    python_stack += ", line " + pythonLine;
    1.39 +  }
    1.40 +  let trace, msg;
    1.41 +  if (typeof(error) == "object" && 'name' in error && 'stack' in error) {
    1.42 +    let stack = error.stack.split("\n");
    1.43 +    let match = stack[0].match(/:(\d+):\d+$/);
    1.44 +    let line = match ? parseInt(match[1]) : 0;
    1.45 +    msg = error.name + ('message' in error ? ": " + error.message : "");
    1.46 +    trace = python_stack +
    1.47 +                "\ninline javascript, line " + line +
    1.48 +                "\nsrc: \"" + script.split("\n")[line] + "\"";
    1.49 +  }
    1.50 +  else {
    1.51 +    trace = python_stack;
    1.52 +    msg = error + "";
    1.53 +  }
    1.54 +  return [msg, trace];
    1.55 +}
    1.56 +
    1.57 +this.MarionetteLogObj = function MarionetteLogObj() {
    1.58 +  this.logs = [];
    1.59 +}
    1.60 +MarionetteLogObj.prototype = {
    1.61 +  /**
    1.62 +   * Log message. Accepts user defined log-level.
    1.63 +   * @param msg String
    1.64 +   *        The message to be logged
    1.65 +   * @param level String
    1.66 +   *        The logging level to be used
    1.67 +   */
    1.68 +  log: function ML_log(msg, level) {
    1.69 +    let lev = level ? level : "INFO";
    1.70 +    this.logs.push( [lev, msg, (new Date()).toString()]);
    1.71 +  },
    1.72 +
    1.73 +  /**
    1.74 +   * Add a list of logs to its list
    1.75 +   * @param msgs Object
    1.76 +   *        Takes a list of strings
    1.77 +   */
    1.78 +  addLogs: function ML_addLogs(msgs) {
    1.79 +    for (let i = 0; i < msgs.length; i++) {
    1.80 +      this.logs.push(msgs[i]);
    1.81 +    }
    1.82 +  },
    1.83 +  
    1.84 +  /**
    1.85 +   * Return all logged messages.
    1.86 +   */
    1.87 +  getLogs: function ML_getLogs() {
    1.88 +    let logs = this.logs;
    1.89 +    this.clearLogs();
    1.90 +    return logs;
    1.91 +  },
    1.92 +
    1.93 +  /**
    1.94 +   * Clears the logs
    1.95 +   */
    1.96 +  clearLogs: function ML_clearLogs() {
    1.97 +    this.logs = [];
    1.98 +  },
    1.99 +}

mercurial