api-debugger-spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. const assert = require('assert')
  2. const http = require('http')
  3. const path = require('path')
  4. const {closeWindow} = require('./window-helpers')
  5. const BrowserWindow = require('electron').remote.BrowserWindow
  6. describe('debugger module', () => {
  7. const fixtures = path.resolve(__dirname, 'fixtures')
  8. let w = null
  9. beforeEach(() => {
  10. w = new BrowserWindow({
  11. show: false,
  12. width: 400,
  13. height: 400
  14. })
  15. })
  16. afterEach(() => closeWindow(w).then(() => { w = null }))
  17. describe('debugger.attach', () => {
  18. it('fails when devtools is already open', (done) => {
  19. w.webContents.on('did-finish-load', () => {
  20. w.webContents.openDevTools()
  21. try {
  22. w.webContents.debugger.attach()
  23. } catch (err) {
  24. assert(w.webContents.debugger.isAttached())
  25. done()
  26. }
  27. })
  28. w.webContents.loadURL(`file://${path.join(fixtures, 'pages', 'a.html')}`)
  29. })
  30. it('fails when protocol version is not supported', (done) => {
  31. try {
  32. w.webContents.debugger.attach('2.0')
  33. } catch (err) {
  34. assert(!w.webContents.debugger.isAttached())
  35. done()
  36. }
  37. })
  38. it('attaches when no protocol version is specified', (done) => {
  39. try {
  40. w.webContents.debugger.attach()
  41. } catch (err) {
  42. done(`unexpected error : ${err}`)
  43. }
  44. assert(w.webContents.debugger.isAttached())
  45. done()
  46. })
  47. })
  48. describe('debugger.detach', () => {
  49. it('fires detach event', (done) => {
  50. w.webContents.debugger.on('detach', (e, reason) => {
  51. assert.equal(reason, 'target closed')
  52. assert(!w.webContents.debugger.isAttached())
  53. done()
  54. })
  55. try {
  56. w.webContents.debugger.attach()
  57. } catch (err) {
  58. done(`unexpected error : ${err}`)
  59. }
  60. w.webContents.debugger.detach()
  61. })
  62. })
  63. describe('debugger.sendCommand', () => {
  64. let server
  65. afterEach(() => {
  66. if (server != null) {
  67. server.close()
  68. server = null
  69. }
  70. })
  71. it('retuns response', (done) => {
  72. w.webContents.loadURL('about:blank')
  73. try {
  74. w.webContents.debugger.attach()
  75. } catch (err) {
  76. return done('unexpected error : ' + err)
  77. }
  78. var callback = function (err, res) {
  79. assert(!err.message)
  80. assert(!res.wasThrown)
  81. assert.equal(res.result.value, 6)
  82. w.webContents.debugger.detach()
  83. done()
  84. }
  85. const params = {
  86. 'expression': '4+2'
  87. }
  88. w.webContents.debugger.sendCommand('Runtime.evaluate', params, callback)
  89. })
  90. it('fires message event', (done) => {
  91. const url = process.platform !== 'win32'
  92. ? `file://${path.join(fixtures, 'pages', 'a.html')}`
  93. : 'file:///' + path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/')
  94. w.webContents.loadURL(url)
  95. try {
  96. w.webContents.debugger.attach()
  97. } catch (err) {
  98. done('unexpected error : ' + err)
  99. }
  100. w.webContents.debugger.on('message', (e, method, params) => {
  101. if (method === 'Console.messageAdded') {
  102. assert.equal(params.message.level, 'log')
  103. assert.equal(params.message.url, url)
  104. assert.equal(params.message.text, 'a')
  105. w.webContents.debugger.detach()
  106. done()
  107. }
  108. })
  109. w.webContents.debugger.sendCommand('Console.enable')
  110. })
  111. it('returns error message when command fails', (done) => {
  112. w.webContents.loadURL('about:blank')
  113. try {
  114. w.webContents.debugger.attach()
  115. } catch (err) {
  116. done(`unexpected error : ${err}`)
  117. }
  118. w.webContents.debugger.sendCommand('Test', (err) => {
  119. assert.equal(err.message, "'Test' wasn't found")
  120. w.webContents.debugger.detach()
  121. done()
  122. })
  123. })
  124. it('handles valid unicode characters in message', (done) => {
  125. try {
  126. w.webContents.debugger.attach()
  127. } catch (err) {
  128. done(`unexpected error : ${err}`)
  129. }
  130. w.webContents.debugger.on('message', (event, method, params) => {
  131. if (method === 'Network.loadingFinished') {
  132. w.webContents.debugger.sendCommand('Network.getResponseBody', {
  133. requestId: params.requestId
  134. }, (_, data) => {
  135. assert.equal(data.body, '\u0024')
  136. done()
  137. })
  138. }
  139. })
  140. server = http.createServer((req, res) => {
  141. res.setHeader('Content-Type', 'text/plain; charset=utf-8')
  142. res.end('\u0024')
  143. })
  144. server.listen(0, '127.0.0.1', () => {
  145. w.webContents.debugger.sendCommand('Network.enable')
  146. w.loadURL(`http://127.0.0.1:${server.address().port}`)
  147. })
  148. })
  149. it('does not crash for invalid unicode characters in message', (done) => {
  150. try {
  151. w.webContents.debugger.attach()
  152. } catch (err) {
  153. done(`unexpected error : ${err}`)
  154. }
  155. w.webContents.debugger.on('message', (event, method, params) => {
  156. // loadingFinished indicates that page has been loaded and it did not
  157. // crash because of invalid UTF-8 data
  158. if (method === 'Network.loadingFinished') {
  159. done()
  160. }
  161. })
  162. server = http.createServer((req, res) => {
  163. res.setHeader('Content-Type', 'text/plain; charset=utf-8')
  164. res.end('\uFFFF')
  165. })
  166. server.listen(0, '127.0.0.1', () => {
  167. w.webContents.debugger.sendCommand('Network.enable')
  168. w.loadURL(`http://127.0.0.1:${server.address().port}`)
  169. })
  170. })
  171. })
  172. })