This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcommandRepl.js
127 lines (119 loc) · 3.43 KB
/
commandRepl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var REPL_INITIAL_SUGGESTIONS = [
'element(by.id(\'\'))',
'element(by.css(\'\'))',
'element(by.name(\'\'))',
'element(by.binding(\'\'))',
'element(by.xpath(\'\'))',
'element(by.tagName(\'\'))',
'element(by.className(\'\'))'
];
/**
* Repl to interactively run commands in the context of the test.
*
* @param {Client} node debugger client.
* @constructor
*/
var CommandRepl = function(client) {
this.client = client;
this.prompt = '> ';
};
/**
* Eval function for processing a single step in repl.
* Call callback with the result when complete.
*
* @public
* @param {string} expression
* @param {function} callback
*/
CommandRepl.prototype.stepEval = function(expression, callback) {
expression = expression.replace(/"/g, '\\\"');
var expr = 'browser.debugHelper.dbgCodeExecutor.execute("' + expression + '")';
this.evaluate_(expr, callback);
};
/**
* Autocomplete user entries.
* Call callback with the suggestions.
*
* @public
* @param {string} line Initial user entry
* @param {function} callback
*/
CommandRepl.prototype.complete = function(line, callback) {
if (line === '') {
callback(null, [REPL_INITIAL_SUGGESTIONS, '']);
} else {
// TODO(juliemr): This is freezing the program!
line = line.replace(/"/g, '\\\"');
var expr = 'browser.debugHelper.dbgCodeExecutor.complete("' + line + '")';
this.evaluate_(expr, function(err, res) {
// Result is a JSON representation of the autocomplete response.
var result = res === undefined ? undefined : JSON.parse(res);
callback(err, result);
});
}
};
/**
* Helper function to evaluate an expression remotely, and callback with
* the result. The expression can be a promise, in which case, the method
* will wait for the result and callback with the resolved value.
*
* @private
* @param {string} expression Expression to evaluate
* @param {function} callback
*/
CommandRepl.prototype.evaluate_ = function(expression, callback) {
var self = this;
var onbreak_ = function() {
self.client.req({
command: 'evaluate',
arguments: {
frame: 0,
maxStringLength: 1000,
expression: 'browser.debugHelper.dbgCodeExecutor.resultReady()'
}
}, function(err, res) {
if (err) {
throw new Error('Error while checking if debugger expression result was ready.' +
'Expression: ' + expression + ' Error: ' + err);
}
// If code finished executing, get result.
if (res.value) {
self.client.req({
command: 'evaluate',
arguments: {
frame: 0,
maxStringLength: -1,
expression: 'browser.debugHelper.dbgCodeExecutor.getResult()'
}
}, function(err, res) {
try {
callback(err, res.value);
} catch (e) {
callback(e, undefined);
}
self.client.removeListener('break', onbreak_);
});
} else {
// If we need more loops for the code to finish executing, continue
// until the next execute step.
self.client.reqContinue(function() {
// Intentionally blank.
});
}
});
};
this.client.on('break', onbreak_);
this.client.req({
command: 'evaluate',
arguments: {
frame: 0,
maxStringLength: 1000,
expression: expression
}
}, function() {
self.client.reqContinue(function() {
// Intentionally blank.
});
});
};
module.exports = CommandRepl;