wddebugger.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var repl = require('repl');
  2. var debuggerCommons = require('../debuggerCommons');
  3. var DebuggerRepl = require('../modes/debuggerRepl');
  4. /**
  5. * Custom protractor debugger which steps through one control flow task at a time.
  6. *
  7. * @constructor
  8. */
  9. var WdDebugger = function() {
  10. this.client;
  11. this.replServer;
  12. this.dbgRepl;
  13. };
  14. /**
  15. * Eval function for processing a single step in repl.
  16. * @private
  17. * @param {string} cmd
  18. * @param {object} context
  19. * @param {string} filename
  20. * @param {function} callback
  21. */
  22. WdDebugger.prototype.stepEval_ = function(cmd, context, filename, callback) {
  23. // The loop won't come back until 'callback' is called.
  24. // Note - node's debugger gets around this by adding custom objects
  25. // named 'c', 's', etc to the REPL context. They have getters which
  26. // perform the desired function, and the callback is stored for later use.
  27. // Think about whether this is a better pattern.
  28. cmd = debuggerCommons.trimReplCmd(cmd);
  29. this.dbgRepl.stepEval(cmd, callback);
  30. };
  31. /**
  32. * Instantiate all repl objects, and debuggerRepl as current and start repl.
  33. * @private
  34. */
  35. WdDebugger.prototype.initRepl_ = function() {
  36. var self = this;
  37. this.dbgRepl = new DebuggerRepl(this.client);
  38. // We want the prompt to show up only after the controlflow text prints.
  39. this.dbgRepl.printControlFlow_(function() {
  40. self.replServer = repl.start({
  41. prompt: self.dbgRepl.prompt,
  42. input: process.stdin,
  43. output: process.stdout,
  44. eval: self.stepEval_.bind(self),
  45. useGlobal: false,
  46. ignoreUndefined: true,
  47. completer: self.dbgRepl.complete.bind(self.dbgRepl)
  48. });
  49. self.replServer.on('exit', function() {
  50. console.log('Resuming code execution');
  51. self.client.req({command: 'disconnect'}, function() {
  52. process.exit();
  53. });
  54. });
  55. });
  56. };
  57. /**
  58. * Initiate the debugger.
  59. * @public
  60. */
  61. WdDebugger.prototype.init = function() {
  62. var self = this;
  63. this.client = debuggerCommons.attachDebugger(process.argv[2], process.argv[3]);
  64. this.client.once('ready', function() {
  65. debuggerCommons.setWebDriverCommandBreakpoint(self.client, function() {
  66. process.send('ready');
  67. self.client.reqContinue(function() {
  68. // Intentionally blank.
  69. });
  70. });
  71. self.initRepl_();
  72. });
  73. };
  74. var wdDebugger = new WdDebugger();
  75. wdDebugger.init();