-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat(debugger): make element explorer work with node 0.12.0 #1898
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var baseDebugger = require('_debugger'); | ||
|
||
/** | ||
* Create a debugger client and attach to a running protractor process. | ||
* Set a break point at webdriver executor. | ||
* @param {number} pid Pid of the process to attach the debugger to. | ||
* @param {number=} opt_port Port to set up the debugger connection over. | ||
* @return {!baseDebugger.Client} The connected debugger client. | ||
*/ | ||
exports.attachDebugger = function(pid, opt_port) { | ||
var client = new baseDebugger.Client(); | ||
var port = opt_port || process.debugPort; | ||
|
||
// Call this private function instead of sending SIGUSR1 because Windows. | ||
process._debugProcess(pid); | ||
|
||
client.once('ready', function() { | ||
client.setBreakpoint({ | ||
type: 'scriptRegExp', | ||
target: '.*executors\.js', //jshint ignore:line | ||
line: 37 | ||
}, function() { | ||
client.reqContinue(function() { | ||
// Intentionally blank. | ||
}); | ||
}); | ||
}); | ||
|
||
// Connect to debugger on port with retry 200ms apart. | ||
var connectWithRetry = function(attempts) { | ||
client.connect(port, 'localhost') | ||
.on('error', function(e) { | ||
if (attempts === 1) { | ||
throw e; | ||
} else { | ||
setTimeout(function() { | ||
connectWithRetry(attempts - 1); | ||
}, 200); | ||
} | ||
}); | ||
}; | ||
connectWithRetry(10); | ||
|
||
return client; | ||
}; | ||
|
||
/** | ||
* Trim excess symbols from the repl command so that it is consistent with | ||
* the user input. | ||
* @param {string} cmd Cmd provided by the repl server. | ||
* @return {string} The trimmed cmd. | ||
*/ | ||
exports.trimReplCmd = function(cmd) { | ||
// Given user input 'foobar', some versions of node provide '(foobar\n)', | ||
// while other versions of node provide 'foobar\n'. | ||
if (cmd.length >= 2 && cmd[0] === '(' && cmd[cmd.length - 1] === ')') { | ||
cmd = cmd.substring(1, cmd.length - 1); | ||
} | ||
return cmd.slice(0, cmd.length - 1); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -661,34 +661,30 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort) | |
return asString; | ||
}; | ||
|
||
if (opt_debugPort) { | ||
process.debugPort = opt_debugPort; | ||
} | ||
|
||
// Call this private function instead of sending SIGUSR1 because Windows. | ||
process._debugProcess(process.pid); | ||
|
||
var flow = webdriver.promise.controlFlow(); | ||
var self = this; | ||
var pausePromise = flow.execute(function() { | ||
log.puts('Starting WebDriver debugger in a child process. Pause is ' + | ||
'still beta, please report issues at github.com/angular/protractor\n'); | ||
var args = [process.debugPort]; | ||
if (self.debuggerServerPort_) { | ||
args.push(self.debuggerServerPort_); | ||
} | ||
var nodedebug = require('child_process').fork(debuggerClientPath, args); | ||
process.on('exit', function() { | ||
nodedebug.kill('SIGTERM'); | ||
// Invoke fn if port is available. | ||
var onPortAvailable = function(port, fn) { | ||
var net = require('net'); | ||
var tester = net.connect({port: port}, function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check that the error is EADDRINUSE? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not an error. I'm testing that I can connect to it and fail if so. I tried something like https://gist.github.com/timoxley/1689041 before, where I'm test creating a server to check for EADDRINUSE. However, that does not fail for some reason, until I try to create the debug process. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah sorry, I meant something like this: https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/net/portprober.js#L159 Except we don't actually want to create a server, so the method you had is the best I can think of. |
||
console.error('Port ' + port + ' is already in use. Please specify ' + | ||
'another port to debug.'); | ||
process.exit(1); | ||
}); | ||
}); | ||
tester.once('error', function (err) { | ||
if (err.code === 'ECONNREFUSED') { | ||
tester.once('close', fn).end(); | ||
} else { | ||
console.error('Unexpected failure testing for port ' + port + ': ', | ||
err); | ||
process.exit(1); | ||
} | ||
}); | ||
}; | ||
|
||
var vm_ = require('vm'); | ||
var flow = webdriver.promise.controlFlow(); | ||
|
||
var context = { require: require }; | ||
for (var key in global) { | ||
context[key] = global[key]; | ||
} | ||
context.list = function(locator) { | ||
global.list = function(locator) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? I think it's safer to avoid polluting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. otherwise autocomplete doesn't suggest it, since autocomplete doesn't know about context. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I don't think we need autocomplete to suggest it, since it's all of 4 characters, but I don't really care either way. |
||
/* globals browser */ | ||
return browser.findElements(locator).then(function(arr) { | ||
var found = []; | ||
|
@@ -700,9 +696,33 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort) | |
return found; | ||
}); | ||
}; | ||
for (var key in global) { | ||
context[key] = global[key]; | ||
} | ||
var sandbox = vm_.createContext(context); | ||
|
||
var browserUnderDebug = this; | ||
flow.execute(function() { | ||
log.puts('Starting WebDriver debugger in a child process. Pause is ' + | ||
'still beta, please report issues at github.com/angular/protractor\n'); | ||
process.debugPort = opt_debugPort || process.debugPort; | ||
onPortAvailable(process.debugPort, function() { | ||
var args = [process.pid, process.debugPort]; | ||
if (browserUnderDebug.debuggerServerPort_) { | ||
args.push(browserUnderDebug.debuggerServerPort_); | ||
} | ||
var nodedebug = require('child_process').fork(debuggerClientPath, args); | ||
process.on('exit', function() { | ||
nodedebug.kill('SIGTERM'); | ||
}); | ||
}); | ||
}); | ||
|
||
var pausePromise = flow.timeout(1000, 'waiting for debugger to attach') | ||
.then(function() { | ||
// Necessary for backward compatibility with node < 0.12.0 | ||
browserUnderDebug.executeScript_('', 'empty debugger hook'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why wasn't this necessary before? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because the way node debugger changed in 0.12. As a result of that, before, we call debug in this process. In the client, we take control once we attach. However, if we're using node 0.10, the program isn't paused when issued the debug, so the first reqContinue actually makes the program jump to the next break point. |
||
}); | ||
|
||
// Helper used only by debuggers at './debugger/modes/*.js' to insert code | ||
// into the control flow. | ||
|
@@ -718,7 +738,8 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort) | |
// A dummy repl server to make use of its completion function. | ||
replServer_: require('repl').start({ | ||
input: {on: function() {}, resume: function() {}}, // dummy readable stream | ||
output: {write: function() {}} // dummy writable stream | ||
output: {write: function() {}}, // dummy writable stream | ||
useGlobal: true | ||
}), | ||
|
||
// Execute a function, which could yield a value or a promise, | ||
|
@@ -802,8 +823,6 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort) | |
return this.execPromiseResult_; | ||
} | ||
}; | ||
|
||
flow.timeout(1000, 'waiting for debugger to attach'); | ||
}; | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to worry about versions <10 anymore, feel free to remove this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping this, as discussed offline.