2015-04-04 09:53:59 -07:00
|
|
|
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
|
// distributed with this work for additional information
|
|
|
|
|
// regarding copyright ownership. The SFC licenses this file
|
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
|
// with the License. You may obtain a copy of the License at
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-12 19:33:37 +00:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
// KIND, either express or implied. See the License for the
|
|
|
|
|
// specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @fileoverview Tools for parsing and pretty printing error stack traces. This
|
|
|
|
|
* file is based on goog.testing.stacktrace.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
goog.provide('webdriver.stacktrace');
|
|
|
|
|
goog.provide('webdriver.stacktrace.Snapshot');
|
|
|
|
|
|
|
|
|
|
goog.require('goog.array');
|
2012-10-22 22:58:53 +00:00
|
|
|
goog.require('goog.string');
|
2013-06-14 07:03:52 -07:00
|
|
|
goog.require('goog.userAgent');
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
2012-09-09 05:20:57 +00:00
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* Stores a snapshot of the stack trace at the time this instance was created.
|
2012-10-22 22:58:53 +00:00
|
|
|
* The stack trace will always be adjusted to exclude this function call.
|
2012-07-02 19:46:57 +00:00
|
|
|
* @param {number=} opt_slice The number of frames to remove from the top of
|
2012-10-22 22:58:53 +00:00
|
|
|
* the generated stack trace.
|
2012-07-02 19:46:57 +00:00
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.Snapshot = function(opt_slice) {
|
|
|
|
|
|
2013-03-10 13:56:37 -07:00
|
|
|
/** @private {number} */
|
2012-07-02 19:46:57 +00:00
|
|
|
this.slice_ = opt_slice || 0;
|
|
|
|
|
|
2013-06-14 07:03:52 -07:00
|
|
|
var error;
|
2012-10-22 22:58:53 +00:00
|
|
|
if (webdriver.stacktrace.CAN_CAPTURE_STACK_TRACE_) {
|
2013-06-14 07:03:52 -07:00
|
|
|
error = Error();
|
|
|
|
|
Error.captureStackTrace(error, webdriver.stacktrace.Snapshot);
|
2012-10-22 22:58:53 +00:00
|
|
|
} else {
|
2013-06-14 07:03:52 -07:00
|
|
|
// Remove 1 extra frame for the call to this constructor.
|
2012-10-22 22:58:53 +00:00
|
|
|
this.slice_ += 1;
|
2013-06-14 07:03:52 -07:00
|
|
|
// IE will only create a stack trace when the Error is thrown.
|
|
|
|
|
// We use null.x() to throw an exception instead of throw this.error_
|
|
|
|
|
// because the closure compiler may optimize throws to a function call
|
|
|
|
|
// in an attempt to minimize the binary size which in turn has the side
|
|
|
|
|
// effect of adding an unwanted stack frame.
|
|
|
|
|
try {
|
|
|
|
|
null.x();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
2012-10-22 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-02-28 21:27:06 +01:00
|
|
|
* The error's stacktrace.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
2015-01-18 17:04:52 -08:00
|
|
|
this.stack_ = webdriver.stacktrace.getStack(error);
|
2012-07-02 19:46:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 22:58:53 +00:00
|
|
|
/**
|
|
|
|
|
* Whether the current environment supports the Error.captureStackTrace
|
|
|
|
|
* function (as of 10/17/2012, only V8).
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {boolean}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.CAN_CAPTURE_STACK_TRACE_ =
|
|
|
|
|
goog.isFunction(Error.captureStackTrace);
|
|
|
|
|
|
|
|
|
|
|
2013-06-14 07:03:52 -07:00
|
|
|
/**
|
|
|
|
|
* Whether the current browser supports stack traces.
|
|
|
|
|
*
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
* @const
|
|
|
|
|
*/
|
2013-11-28 08:53:49 -08:00
|
|
|
webdriver.stacktrace.BROWSER_SUPPORTED =
|
|
|
|
|
webdriver.stacktrace.CAN_CAPTURE_STACK_TRACE_ || (function() {
|
|
|
|
|
try {
|
|
|
|
|
throw Error();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return !!e.stack;
|
|
|
|
|
}
|
|
|
|
|
})();
|
2013-06-14 07:03:52 -07:00
|
|
|
|
|
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* The parsed stack trace. This list is lazily generated the first time it is
|
|
|
|
|
* accessed.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @private {Array.<!webdriver.stacktrace.Frame>}
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.Snapshot.prototype.parsedStack_ = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-06-14 07:03:52 -07:00
|
|
|
* @return {!Array.<!webdriver.stacktrace.Frame>} The parsed stack trace.
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.Snapshot.prototype.getStacktrace = function() {
|
|
|
|
|
if (goog.isNull(this.parsedStack_)) {
|
2013-06-14 07:03:52 -07:00
|
|
|
this.parsedStack_ = webdriver.stacktrace.parse_(this.stack_);
|
2012-07-02 19:46:57 +00:00
|
|
|
if (this.slice_) {
|
2013-06-14 07:03:52 -07:00
|
|
|
this.parsedStack_ = goog.array.slice(this.parsedStack_, this.slice_);
|
2012-07-02 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
delete this.slice_;
|
2012-10-22 22:58:53 +00:00
|
|
|
delete this.stack_;
|
2012-07-02 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
return this.parsedStack_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-09-09 05:20:57 +00:00
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* Class representing one stack frame.
|
2012-10-22 22:58:53 +00:00
|
|
|
* @param {(string|undefined)} context Context object, empty in case of global
|
|
|
|
|
* functions or if the browser doesn't provide this information.
|
|
|
|
|
* @param {(string|undefined)} name Function name, empty in case of anonymous
|
|
|
|
|
* functions.
|
|
|
|
|
* @param {(string|undefined)} alias Alias of the function if available. For
|
|
|
|
|
* example the function name will be 'c' and the alias will be 'b' if the
|
|
|
|
|
* function is defined as <code>a.b = function c() {};</code>.
|
|
|
|
|
* @param {(string|undefined)} path File path or URL including line number and
|
|
|
|
|
* optionally column number separated by colons.
|
2012-07-02 19:46:57 +00:00
|
|
|
* @constructor
|
|
|
|
|
*/
|
2013-06-14 07:03:52 -07:00
|
|
|
webdriver.stacktrace.Frame = function(context, name, alias, path) {
|
2012-10-22 22:58:53 +00:00
|
|
|
|
2013-03-10 13:56:37 -07:00
|
|
|
/** @private {string} */
|
2012-10-22 22:58:53 +00:00
|
|
|
this.context_ = context || '';
|
|
|
|
|
|
2013-03-10 13:56:37 -07:00
|
|
|
/** @private {string} */
|
2012-10-22 22:58:53 +00:00
|
|
|
this.name_ = name || '';
|
|
|
|
|
|
2013-03-10 13:56:37 -07:00
|
|
|
/** @private {string} */
|
2012-10-22 22:58:53 +00:00
|
|
|
this.alias_ = alias || '';
|
|
|
|
|
|
2013-03-10 13:56:37 -07:00
|
|
|
/** @private {string} */
|
2012-10-22 22:58:53 +00:00
|
|
|
this.path_ = path || '';
|
2013-06-14 07:03:52 -07:00
|
|
|
|
|
|
|
|
/** @private {string} */
|
|
|
|
|
this.url_ = this.path_;
|
|
|
|
|
|
|
|
|
|
/** @private {number} */
|
|
|
|
|
this.line_ = -1;
|
|
|
|
|
|
|
|
|
|
/** @private {number} */
|
|
|
|
|
this.column_ = -1;
|
|
|
|
|
|
|
|
|
|
if (path) {
|
|
|
|
|
var match = /:(\d+)(?::(\d+))?$/.exec(path);
|
|
|
|
|
if (match) {
|
|
|
|
|
this.line_ = Number(match[1]);
|
|
|
|
|
this.column = Number(match[2] || -1);
|
|
|
|
|
this.url_ = path.substr(0, match.index);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-02 19:46:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 22:58:53 +00:00
|
|
|
/**
|
|
|
|
|
* Constant for an anonymous frame.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @private {!webdriver.stacktrace.Frame}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.ANONYMOUS_FRAME_ =
|
2013-06-14 07:03:52 -07:00
|
|
|
new webdriver.stacktrace.Frame('', '', '', '');
|
2012-10-22 22:58:53 +00:00
|
|
|
|
|
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return {string} The function name or empty string if the function is
|
|
|
|
|
* anonymous and the object field which it's assigned to is unknown.
|
|
|
|
|
*/
|
2013-06-14 07:03:52 -07:00
|
|
|
webdriver.stacktrace.Frame.prototype.getName = function() {
|
2012-07-02 19:46:57 +00:00
|
|
|
return this.name_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-06-14 07:03:52 -07:00
|
|
|
/**
|
|
|
|
|
* @return {string} The url or empty string if it is unknown.
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.Frame.prototype.getUrl = function() {
|
|
|
|
|
return this.url_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return {number} The line number if known or -1 if it is unknown.
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.Frame.prototype.getLine = function() {
|
|
|
|
|
return this.line_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return {number} The column number if known and -1 if it is unknown.
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.Frame.prototype.getColumn = function() {
|
|
|
|
|
return this.column_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* @return {boolean} Whether the stack frame contains an anonymous function.
|
|
|
|
|
*/
|
2013-06-14 07:03:52 -07:00
|
|
|
webdriver.stacktrace.Frame.prototype.isAnonymous = function() {
|
2012-07-02 19:46:57 +00:00
|
|
|
return !this.name_ || this.context_ == '[object Object]';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-22 22:58:53 +00:00
|
|
|
* Converts this frame to its string representation using V8's stack trace
|
|
|
|
|
* format: http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
|
|
|
|
|
* @return {string} The string representation of this frame.
|
|
|
|
|
* @override
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
2013-06-14 07:03:52 -07:00
|
|
|
webdriver.stacktrace.Frame.prototype.toString = function() {
|
2012-10-22 22:58:53 +00:00
|
|
|
var context = this.context_;
|
|
|
|
|
if (context && context !== 'new ') {
|
|
|
|
|
context += '.';
|
|
|
|
|
}
|
|
|
|
|
context += this.name_;
|
|
|
|
|
context += this.alias_ ? ' [as ' + this.alias_ + ']' : '';
|
|
|
|
|
|
|
|
|
|
var path = this.path_ || '<anonymous>';
|
|
|
|
|
return ' at ' + (context ? context + ' (' + path + ')' : path);
|
|
|
|
|
};
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maximum length of a string that can be matched with a RegExp on
|
|
|
|
|
* Firefox 3x. Exceeding this approximate length will cause string.match
|
|
|
|
|
* to exceed Firefox's stack quota. This situation can be encountered
|
|
|
|
|
* when goog.globalEval is invoked with a long argument; such as
|
|
|
|
|
* when loading a module.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {number}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.MAX_FIREFOX_FRAMESTRING_LENGTH_ = 500000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RegExp pattern for JavaScript identifiers. We don't support Unicode
|
|
|
|
|
* identifiers defined in ECMAScript v3.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.IDENTIFIER_PATTERN_ = '[a-zA-Z_$][\\w$]*';
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 22:58:53 +00:00
|
|
|
/**
|
|
|
|
|
* Pattern for a matching the type on a fully-qualified name. Forms an
|
|
|
|
|
* optional sub-match on the type. For example, in "foo.bar.baz", will match on
|
|
|
|
|
* "foo.bar".
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.CONTEXT_PATTERN_ =
|
|
|
|
|
'(' + webdriver.stacktrace.IDENTIFIER_PATTERN_ +
|
|
|
|
|
'(?:\\.' + webdriver.stacktrace.IDENTIFIER_PATTERN_ + ')*)\\.';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pattern for matching a fully qualified name. Will create two sub-matches:
|
|
|
|
|
* the type (optional), and the name. For example, in "foo.bar.baz", will
|
|
|
|
|
* match on ["foo.bar", "baz"].
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.QUALIFIED_NAME_PATTERN_ =
|
|
|
|
|
'(?:' + webdriver.stacktrace.CONTEXT_PATTERN_ + ')?' +
|
|
|
|
|
'(' + webdriver.stacktrace.IDENTIFIER_PATTERN_ + ')';
|
|
|
|
|
|
|
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* RegExp pattern for function name alias in the V8 stack trace.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.V8_ALIAS_PATTERN_ =
|
|
|
|
|
'(?: \\[as (' + webdriver.stacktrace.IDENTIFIER_PATTERN_ + ')\\])?';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RegExp pattern for function names and constructor calls in the V8 stack
|
|
|
|
|
* trace.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.V8_FUNCTION_NAME_PATTERN_ =
|
2012-10-22 22:58:53 +00:00
|
|
|
'(?:' + webdriver.stacktrace.IDENTIFIER_PATTERN_ + '|<anonymous>)';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RegExp pattern for the context of a function call in V8. Creates two
|
|
|
|
|
* submatches, only one of which will ever match: either the namespace
|
|
|
|
|
* identifier (with optional "new" keyword in the case of a constructor call),
|
|
|
|
|
* or just the "new " phrase for a top level constructor call.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.V8_CONTEXT_PATTERN_ =
|
|
|
|
|
'(?:((?:new )?(?:\\[object Object\\]|' +
|
|
|
|
|
webdriver.stacktrace.IDENTIFIER_PATTERN_ +
|
|
|
|
|
'(?:\\.' + webdriver.stacktrace.IDENTIFIER_PATTERN_ + ')*)' +
|
|
|
|
|
')\\.|(new ))';
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RegExp pattern for function call in the V8 stack trace.
|
|
|
|
|
* Creates 3 submatches with context object (optional), function name and
|
|
|
|
|
* function alias (optional).
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.V8_FUNCTION_CALL_PATTERN_ =
|
2012-10-22 22:58:53 +00:00
|
|
|
' (?:' + webdriver.stacktrace.V8_CONTEXT_PATTERN_ + ')?' +
|
|
|
|
|
'(' + webdriver.stacktrace.V8_FUNCTION_NAME_PATTERN_ + ')' +
|
|
|
|
|
webdriver.stacktrace.V8_ALIAS_PATTERN_;
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RegExp pattern for an URL + position inside the file.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.URL_PATTERN_ =
|
2013-06-14 07:03:52 -07:00
|
|
|
'((?:http|https|file)://[^\\s]+|javascript:.*)';
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-22 22:58:53 +00:00
|
|
|
* RegExp pattern for a location string in a V8 stack frame. Creates two
|
|
|
|
|
* submatches for the location, one for enclosed in parentheticals and on
|
|
|
|
|
* where the location appears alone (which will only occur if the location is
|
|
|
|
|
* the only information in the frame).
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
* @see http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
2012-10-22 22:58:53 +00:00
|
|
|
webdriver.stacktrace.V8_LOCATION_PATTERN_ = ' (?:\\((.*)\\)|(.*))';
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Regular expression for parsing one stack frame in V8.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {!RegExp}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
2014-10-08 10:57:56 -07:00
|
|
|
webdriver.stacktrace.V8_STACK_FRAME_REGEXP_ = new RegExp('^\\s+at' +
|
|
|
|
|
// Prevent intersections with IE10 stack frame regex.
|
|
|
|
|
'(?! (?:Anonymous function|Global code|eval code) )' +
|
2012-07-02 19:46:57 +00:00
|
|
|
'(?:' + webdriver.stacktrace.V8_FUNCTION_CALL_PATTERN_ + ')?' +
|
2012-10-22 22:58:53 +00:00
|
|
|
webdriver.stacktrace.V8_LOCATION_PATTERN_ + '$');
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
2013-06-14 07:03:52 -07:00
|
|
|
/**
|
|
|
|
|
* RegExp pattern for function names in the Firefox stack trace.
|
|
|
|
|
* Firefox has extended identifiers to deal with inner functions and anonymous
|
|
|
|
|
* functions: https://bugzilla.mozilla.org/show_bug.cgi?id=433529#c9
|
|
|
|
|
* @private {string}
|
|
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.FIREFOX_FUNCTION_NAME_PATTERN_ =
|
|
|
|
|
webdriver.stacktrace.IDENTIFIER_PATTERN_ + '[\\w./<$]*';
|
|
|
|
|
|
|
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* RegExp pattern for function call in the Firefox stack trace.
|
2012-10-22 22:58:53 +00:00
|
|
|
* Creates a submatch for the function name.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.FIREFOX_FUNCTION_CALL_PATTERN_ =
|
2013-06-14 07:03:52 -07:00
|
|
|
'(' + webdriver.stacktrace.FIREFOX_FUNCTION_NAME_PATTERN_ + ')?' +
|
2012-10-22 22:58:53 +00:00
|
|
|
'(?:\\(.*\\))?@';
|
2012-07-02 19:46:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Regular expression for parsing one stack frame in Firefox.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {!RegExp}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.FIREFOX_STACK_FRAME_REGEXP_ = new RegExp('^' +
|
|
|
|
|
webdriver.stacktrace.FIREFOX_FUNCTION_CALL_PATTERN_ +
|
|
|
|
|
'(?::0|' + webdriver.stacktrace.URL_PATTERN_ + ')$');
|
|
|
|
|
|
|
|
|
|
|
2013-06-14 07:03:52 -07:00
|
|
|
/**
|
|
|
|
|
* RegExp pattern for function call in a Chakra (IE) stack trace. This
|
2014-09-12 01:45:40 -07:00
|
|
|
* expression creates 2 submatches on the (optional) context and function name,
|
|
|
|
|
* matching identifiers like 'foo.Bar.prototype.baz', 'Anonymous function',
|
|
|
|
|
* 'eval code', and 'Global code'.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @private {string}
|
|
|
|
|
* @const
|
|
|
|
|
*/
|
2014-09-12 01:45:40 -07:00
|
|
|
webdriver.stacktrace.CHAKRA_FUNCTION_CALL_PATTERN_ =
|
|
|
|
|
'(?:(' + webdriver.stacktrace.IDENTIFIER_PATTERN_ +
|
|
|
|
|
'(?:\\.' + webdriver.stacktrace.IDENTIFIER_PATTERN_ + ')*)\\.)?' +
|
|
|
|
|
'(' + webdriver.stacktrace.IDENTIFIER_PATTERN_ + '(?:\\s+\\w+)*)';
|
2013-06-14 07:03:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Regular expression for parsing on stack frame in Chakra (IE).
|
|
|
|
|
* @private {!RegExp}
|
|
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.CHAKRA_STACK_FRAME_REGEXP_ = new RegExp('^ at ' +
|
|
|
|
|
webdriver.stacktrace.CHAKRA_FUNCTION_CALL_PATTERN_ +
|
|
|
|
|
'\\s*(?:\\((.*)\\))$');
|
|
|
|
|
|
|
|
|
|
|
2012-10-22 22:58:53 +00:00
|
|
|
/**
|
|
|
|
|
* Placeholder for an unparsable frame in a stack trace generated by
|
|
|
|
|
* {@link goog.testing.stacktrace}.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.UNKNOWN_CLOSURE_FRAME_ = '> (unknown)';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Representation of an anonymous frame in a stack trace generated by
|
|
|
|
|
* {@link goog.testing.stacktrace}.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.ANONYMOUS_CLOSURE_FRAME_ = '> anonymous';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pattern for a function call in a Closure stack trace. Creates three optional
|
|
|
|
|
* submatches: the context, function name, and alias.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {string}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.CLOSURE_FUNCTION_CALL_PATTERN_ =
|
|
|
|
|
webdriver.stacktrace.QUALIFIED_NAME_PATTERN_ +
|
|
|
|
|
'(?:\\(.*\\))?' + // Ignore arguments if present.
|
|
|
|
|
webdriver.stacktrace.V8_ALIAS_PATTERN_;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Regular expression for parsing a stack frame generated by Closure's
|
|
|
|
|
* {@link goog.testing.stacktrace}.
|
2013-03-10 13:56:37 -07:00
|
|
|
* @private {!RegExp}
|
2012-10-22 22:58:53 +00:00
|
|
|
* @const
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.CLOSURE_STACK_FRAME_REGEXP_ = new RegExp('^> ' +
|
|
|
|
|
'(?:' + webdriver.stacktrace.CLOSURE_FUNCTION_CALL_PATTERN_ +
|
|
|
|
|
'(?: at )?)?' +
|
|
|
|
|
'(?:(.*:\\d+:\\d+)|' + webdriver.stacktrace.URL_PATTERN_ + ')?$');
|
|
|
|
|
|
|
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
/**
|
|
|
|
|
* Parses one stack frame.
|
|
|
|
|
* @param {string} frameStr The stack frame as string.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @return {webdriver.stacktrace.Frame} Stack frame object or null if the
|
2012-07-02 19:46:57 +00:00
|
|
|
* parsing failed.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.parseStackFrame_ = function(frameStr) {
|
|
|
|
|
var m = frameStr.match(webdriver.stacktrace.V8_STACK_FRAME_REGEXP_);
|
|
|
|
|
if (m) {
|
2013-06-14 07:03:52 -07:00
|
|
|
return new webdriver.stacktrace.Frame(
|
2012-10-22 22:58:53 +00:00
|
|
|
m[1] || m[2], m[3], m[4], m[5] || m[6]);
|
2012-07-02 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frameStr.length >
|
|
|
|
|
webdriver.stacktrace.MAX_FIREFOX_FRAMESTRING_LENGTH_) {
|
|
|
|
|
return webdriver.stacktrace.parseLongFirefoxFrame_(frameStr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m = frameStr.match(webdriver.stacktrace.FIREFOX_STACK_FRAME_REGEXP_);
|
|
|
|
|
if (m) {
|
2013-06-14 07:03:52 -07:00
|
|
|
return new webdriver.stacktrace.Frame('', m[1], '', m[2]);
|
2012-07-02 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-14 07:03:52 -07:00
|
|
|
m = frameStr.match(webdriver.stacktrace.CHAKRA_STACK_FRAME_REGEXP_);
|
|
|
|
|
if (m) {
|
2014-09-12 01:45:40 -07:00
|
|
|
return new webdriver.stacktrace.Frame(m[1], m[2], '', m[3]);
|
2012-10-22 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (frameStr == webdriver.stacktrace.UNKNOWN_CLOSURE_FRAME_ ||
|
|
|
|
|
frameStr == webdriver.stacktrace.ANONYMOUS_CLOSURE_FRAME_) {
|
|
|
|
|
return webdriver.stacktrace.ANONYMOUS_FRAME_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m = frameStr.match(webdriver.stacktrace.CLOSURE_STACK_FRAME_REGEXP_);
|
|
|
|
|
if (m) {
|
2013-06-14 07:03:52 -07:00
|
|
|
return new webdriver.stacktrace.Frame(m[1], m[2], m[3], m[4] || m[5]);
|
2012-07-02 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses a long firefox stack frame.
|
|
|
|
|
* @param {string} frameStr The stack frame as string.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @return {!webdriver.stacktrace.Frame} Stack frame object.
|
2012-07-02 19:46:57 +00:00
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.parseLongFirefoxFrame_ = function(frameStr) {
|
|
|
|
|
var firstParen = frameStr.indexOf('(');
|
|
|
|
|
var lastAmpersand = frameStr.lastIndexOf('@');
|
|
|
|
|
var lastColon = frameStr.lastIndexOf(':');
|
|
|
|
|
var functionName = '';
|
|
|
|
|
if ((firstParen >= 0) && (firstParen < lastAmpersand)) {
|
|
|
|
|
functionName = frameStr.substring(0, firstParen);
|
|
|
|
|
}
|
|
|
|
|
var loc = '';
|
|
|
|
|
if ((lastAmpersand >= 0) && (lastAmpersand + 1 < lastColon)) {
|
|
|
|
|
loc = frameStr.substring(lastAmpersand + 1);
|
|
|
|
|
}
|
2013-06-14 07:03:52 -07:00
|
|
|
return new webdriver.stacktrace.Frame('', functionName, '', loc);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an error's stack trace with the error string trimmed.
|
|
|
|
|
* V8 prepends the string representation of an error to its stack trace.
|
|
|
|
|
* This function trims the string so that the stack trace can be parsed
|
|
|
|
|
* consistently with the other JS engines.
|
2014-04-30 13:52:52 -07:00
|
|
|
* @param {(Error|goog.testing.JsUnitException)} error The error.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @return {string} The stack trace string.
|
|
|
|
|
*/
|
2015-01-18 17:04:52 -08:00
|
|
|
webdriver.stacktrace.getStack = function(error) {
|
2014-04-30 13:52:52 -07:00
|
|
|
if (!error) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2013-06-14 07:03:52 -07:00
|
|
|
var stack = error.stack || error.stackTrace || '';
|
|
|
|
|
var errorStr = error + '\n';
|
|
|
|
|
if (goog.string.startsWith(stack, errorStr)) {
|
|
|
|
|
stack = stack.substring(errorStr.length);
|
|
|
|
|
}
|
|
|
|
|
return stack;
|
2012-10-22 22:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Formats an error's stack trace.
|
|
|
|
|
* @param {!(Error|goog.testing.JsUnitException)} error The error to format.
|
|
|
|
|
* @return {!(Error|goog.testing.JsUnitException)} The formatted error.
|
|
|
|
|
*/
|
|
|
|
|
webdriver.stacktrace.format = function(error) {
|
2015-01-18 17:04:52 -08:00
|
|
|
var stack = webdriver.stacktrace.getStack(error);
|
2013-06-14 07:03:52 -07:00
|
|
|
var frames = webdriver.stacktrace.parse_(stack);
|
|
|
|
|
|
2014-10-08 10:57:56 -07:00
|
|
|
// If the original stack is in an unexpected format, our formatted stack
|
|
|
|
|
// trace will be a bunch of " at <anonymous>" lines. If this is the case,
|
|
|
|
|
// just return the error unmodified to avoid losing information. This is
|
|
|
|
|
// necessary since the user may have defined a custom stack formatter in
|
|
|
|
|
// V8 via Error.prepareStackTrace. See issue 7994.
|
|
|
|
|
var isAnonymousFrame = function(frame) {
|
|
|
|
|
return frame.toString() === ' at <anonymous>';
|
|
|
|
|
};
|
|
|
|
|
if (frames.length && goog.array.every(frames, isAnonymousFrame)) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-14 07:03:52 -07:00
|
|
|
// Older versions of IE simply return [object Error] for toString(), so
|
|
|
|
|
// only use that as a last resort.
|
|
|
|
|
var errorStr = '';
|
|
|
|
|
if (error.message) {
|
|
|
|
|
errorStr = (error.name ? error.name + ': ' : '') + error.message;
|
|
|
|
|
} else {
|
|
|
|
|
errorStr = error.toString();
|
|
|
|
|
}
|
2012-10-22 22:58:53 +00:00
|
|
|
|
|
|
|
|
// Ensure the error is in the V8 style with the error's string representation
|
|
|
|
|
// prepended to the stack.
|
2013-06-14 07:03:52 -07:00
|
|
|
error.stack = errorStr + '\n' + frames.join('\n');
|
2012-10-22 22:58:53 +00:00
|
|
|
return error;
|
2012-07-02 19:46:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-22 22:58:53 +00:00
|
|
|
* Parses an Error object's stack trace.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @param {string} stack The stack trace.
|
|
|
|
|
* @return {!Array.<!webdriver.stacktrace.Frame>} Stack frames. The
|
2012-07-02 19:46:57 +00:00
|
|
|
* unrecognized frames will be nulled out.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2013-06-14 07:03:52 -07:00
|
|
|
webdriver.stacktrace.parse_ = function(stack) {
|
2012-10-22 22:58:53 +00:00
|
|
|
if (!stack) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-02 19:46:57 +00:00
|
|
|
var lines = stack.
|
|
|
|
|
replace(/\s*$/, '').
|
|
|
|
|
split('\n');
|
|
|
|
|
var frames = [];
|
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
|
var frame = webdriver.stacktrace.parseStackFrame_(lines[i]);
|
2013-06-14 07:03:52 -07:00
|
|
|
// The first two frames will be:
|
|
|
|
|
// webdriver.stacktrace.Snapshot()
|
|
|
|
|
// webdriver.stacktrace.get()
|
2015-02-28 21:27:06 +01:00
|
|
|
frames.push(frame || webdriver.stacktrace.ANONYMOUS_FRAME_);
|
2012-07-02 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
return frames;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the native stack trace if available otherwise follows the call chain.
|
2012-10-22 22:58:53 +00:00
|
|
|
* The generated trace will exclude all frames up to and including the call to
|
|
|
|
|
* this function.
|
2013-06-14 07:03:52 -07:00
|
|
|
* @return {!Array.<!webdriver.stacktrace.Frame>} The frames of the stack trace.
|
2012-07-02 19:46:57 +00:00
|
|
|
*/
|
2012-10-22 22:58:53 +00:00
|
|
|
webdriver.stacktrace.get = function() {
|
|
|
|
|
return new webdriver.stacktrace.Snapshot(1).getStacktrace();
|
2012-07-02 19:46:57 +00:00
|
|
|
};
|