debuggerRepl.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var util = require('util');
  2. var DBG_INITIAL_SUGGESTIONS =
  3. ['repl', 'c', 'frame', 'scopes', 'scripts', 'source', 'backtrace'];
  4. /**
  5. * Repl to step through webdriver test code.
  6. *
  7. * @param {Client} node debugger client.
  8. * @constructor
  9. */
  10. var DebuggerRepl = function(client) {
  11. this.client = client;
  12. this.prompt = '>>> ';
  13. };
  14. /**
  15. * Eval function for processing a single step in repl.
  16. * Call callback with the result when complete.
  17. *
  18. * @public
  19. * @param {string} cmd
  20. * @param {function} callback
  21. */
  22. DebuggerRepl.prototype.stepEval = function(cmd, callback) {
  23. switch (cmd) {
  24. case 'c':
  25. this.printNextStep_(callback);
  26. this.client.reqContinue(function() {
  27. // Intentionally blank.
  28. });
  29. break;
  30. case 'repl':
  31. console.log('Error: using repl from browser.pause() has been removed. ' +
  32. 'Please use browser.enterRepl instead.');
  33. callback();
  34. break;
  35. case 'schedule':
  36. this.printControlFlow_(callback);
  37. break;
  38. case 'frame':
  39. this.client.req({command: 'frame'}, function(err, res) {
  40. console.log(util.inspect(res, {colors: true}));
  41. callback();
  42. });
  43. break;
  44. case 'scopes':
  45. this.client.req({command: 'scopes'}, function(err, res) {
  46. console.log(util.inspect(res, {depth: 4, colors: true}));
  47. callback();
  48. });
  49. break;
  50. case 'scripts':
  51. this.client.req({command: 'scripts'}, function(err, res) {
  52. console.log(util.inspect(res, {depth: 4, colors: true}));
  53. callback();
  54. });
  55. break;
  56. case 'source':
  57. this.client.req({command: 'source'}, function(err, res) {
  58. console.log(util.inspect(res, {depth: 4, colors: true}));
  59. callback();
  60. });
  61. break;
  62. case 'backtrace':
  63. this.client.req({command: 'backtrace'}, function(err, res) {
  64. console.log(util.inspect(res, {depth: 4, colors: true}));
  65. callback();
  66. });
  67. break;
  68. default:
  69. console.log('Unrecognized command.');
  70. callback();
  71. break;
  72. }
  73. };
  74. /**
  75. * Autocomplete user entries.
  76. * Call callback with the suggestions.
  77. *
  78. * @public
  79. * @param {string} line Initial user entry
  80. * @param {function} callback
  81. */
  82. DebuggerRepl.prototype.complete = function(line, callback) {
  83. var suggestions = DBG_INITIAL_SUGGESTIONS.filter(function(suggestion) {
  84. return suggestion.indexOf(line) === 0;
  85. });
  86. console.log('suggestions');
  87. callback(null, [suggestions, line]);
  88. };
  89. /**
  90. * Print the next command and setup the next breakpoint.
  91. *
  92. * @private
  93. * @param {function} callback
  94. */
  95. DebuggerRepl.prototype.printNextStep_ = function(callback) {
  96. var self = this;
  97. var onBreak_ = function() {
  98. self.client.req({
  99. command: 'evaluate',
  100. arguments: {
  101. frame: 0,
  102. maxStringLength: 1000,
  103. expression: 'command.getName()'
  104. }
  105. }, function(err, res) {
  106. // We ignore errors here because we'll get one from the initial break.
  107. if (res.value) {
  108. console.log('-- Next command: ' + res.value);
  109. }
  110. callback();
  111. });
  112. };
  113. this.client.once('break', onBreak_);
  114. };
  115. /**
  116. * Print the controlflow.
  117. *
  118. * @private
  119. * @param {function} callback
  120. */
  121. DebuggerRepl.prototype.printControlFlow_ = function(callback) {
  122. this.client.req({
  123. command: 'evaluate',
  124. arguments: {
  125. frame: 0,
  126. maxStringLength: 4000,
  127. expression: 'protractor.promise.controlFlow().getSchedule()'
  128. }
  129. }, function(err, controlFlowResponse) {
  130. if (controlFlowResponse.value) {
  131. console.log(controlFlowResponse.value);
  132. }
  133. callback();
  134. });
  135. };
  136. module.exports = DebuggerRepl;