api-debugger-spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const assert = require('assert')
  2. const path = require('path')
  3. const {closeWindow} = require('./window-helpers')
  4. const BrowserWindow = require('electron').remote.BrowserWindow
  5. describe('debugger module', function () {
  6. var fixtures = path.resolve(__dirname, 'fixtures')
  7. var w = null
  8. beforeEach(function () {
  9. w = new BrowserWindow({
  10. show: false,
  11. width: 400,
  12. height: 400
  13. })
  14. })
  15. afterEach(function () {
  16. return closeWindow(w).then(function () { w = null })
  17. })
  18. describe('debugger.attach', function () {
  19. it('fails when devtools is already open', function (done) {
  20. w.webContents.on('did-finish-load', function () {
  21. w.webContents.openDevTools()
  22. try {
  23. w.webContents.debugger.attach()
  24. } catch (err) {
  25. assert(w.webContents.debugger.isAttached())
  26. done()
  27. }
  28. })
  29. w.webContents.loadURL('file://' + path.join(fixtures, 'pages', 'a.html'))
  30. })
  31. it('fails when protocol version is not supported', function (done) {
  32. try {
  33. w.webContents.debugger.attach('2.0')
  34. } catch (err) {
  35. assert(!w.webContents.debugger.isAttached())
  36. done()
  37. }
  38. })
  39. it('attaches when no protocol version is specified', function (done) {
  40. try {
  41. w.webContents.debugger.attach()
  42. } catch (err) {
  43. done('unexpected error : ' + err)
  44. }
  45. assert(w.webContents.debugger.isAttached())
  46. done()
  47. })
  48. })
  49. describe('debugger.detach', function () {
  50. it('fires detach event', function (done) {
  51. w.webContents.debugger.on('detach', function (e, reason) {
  52. assert.equal(reason, 'target closed')
  53. assert(!w.webContents.debugger.isAttached())
  54. done()
  55. })
  56. try {
  57. w.webContents.debugger.attach()
  58. } catch (err) {
  59. done('unexpected error : ' + err)
  60. }
  61. w.webContents.debugger.detach()
  62. })
  63. })
  64. describe('debugger.sendCommand', function () {
  65. it('retuns response', function (done) {
  66. w.webContents.loadURL('about:blank')
  67. try {
  68. w.webContents.debugger.attach()
  69. } catch (err) {
  70. return done('unexpected error : ' + err)
  71. }
  72. var callback = function (err, res) {
  73. assert(!err.message)
  74. assert(!res.wasThrown)
  75. assert.equal(res.result.value, 6)
  76. w.webContents.debugger.detach()
  77. done()
  78. }
  79. const params = {
  80. 'expression': '4+2'
  81. }
  82. w.webContents.debugger.sendCommand('Runtime.evaluate', params, callback)
  83. })
  84. it('fires message event', function (done) {
  85. var url = process.platform !== 'win32'
  86. ? 'file://' + path.join(fixtures, 'pages', 'a.html')
  87. : 'file:///' + path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/')
  88. w.webContents.loadURL(url)
  89. try {
  90. w.webContents.debugger.attach()
  91. } catch (err) {
  92. done('unexpected error : ' + err)
  93. }
  94. w.webContents.debugger.on('message', function (e, method, params) {
  95. if (method === 'Console.messageAdded') {
  96. assert.equal(params.message.level, 'log')
  97. assert.equal(params.message.url, url)
  98. assert.equal(params.message.text, 'a')
  99. w.webContents.debugger.detach()
  100. done()
  101. }
  102. })
  103. w.webContents.debugger.sendCommand('Console.enable')
  104. })
  105. it('returns error message when command fails', function (done) {
  106. w.webContents.loadURL('about:blank')
  107. try {
  108. w.webContents.debugger.attach()
  109. } catch (err) {
  110. done('unexpected error : ' + err)
  111. }
  112. w.webContents.debugger.sendCommand('Test', function (err) {
  113. assert.equal(err.message, "'Test' wasn't found")
  114. w.webContents.debugger.detach()
  115. done()
  116. })
  117. })
  118. })
  119. })