2018-06-17 22:35:24 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
2017-04-28 16:28:11 +00:00
|
|
|
const http = require('http')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
2019-02-13 17:23:53 +00:00
|
|
|
const { emittedOnce } = require('./events-helpers')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
|
|
|
const { BrowserWindow } = require('electron').remote
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { expect } = chai
|
2018-06-17 22:35:24 +00:00
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2017-10-27 00:35:33 +00:00
|
|
|
describe('debugger module', () => {
|
2017-10-27 00:32:04 +00:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures')
|
|
|
|
let w = null
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
beforeEach(() => {
|
2016-01-22 08:47:23 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
width: 400,
|
|
|
|
height: 400
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
afterEach(() => closeWindow(w).then(() => { w = null }))
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
describe('debugger.attach', () => {
|
2018-09-06 14:44:22 +00:00
|
|
|
it('succeeds when devtools is already open', done => {
|
2017-10-27 00:32:04 +00:00
|
|
|
w.webContents.on('did-finish-load', () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.openDevTools()
|
2016-01-22 08:47:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.attach()
|
2016-03-28 23:19:18 +00:00
|
|
|
} catch (err) {
|
2018-09-06 14:44:22 +00:00
|
|
|
done(`unexpected error : ${err}`)
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2018-09-06 14:44:22 +00:00
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.true()
|
|
|
|
done()
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-09-04 14:50:53 +00:00
|
|
|
w.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'))
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2018-06-17 22:35:24 +00:00
|
|
|
it('fails when protocol version is not supported', done => {
|
2016-01-22 08:47:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.attach('2.0')
|
2016-03-28 23:19:18 +00:00
|
|
|
} catch (err) {
|
2018-06-17 22:35:24 +00:00
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.false()
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2018-06-17 22:35:24 +00:00
|
|
|
it('attaches when no protocol version is specified', done => {
|
2016-01-22 08:47:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.attach()
|
2016-03-28 23:19:18 +00:00
|
|
|
} catch (err) {
|
2017-10-27 00:32:04 +00:00
|
|
|
done(`unexpected error : ${err}`)
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2018-06-17 22:35:24 +00:00
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.true()
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
describe('debugger.detach', () => {
|
|
|
|
it('fires detach event', (done) => {
|
|
|
|
w.webContents.debugger.on('detach', (e, reason) => {
|
2018-06-17 22:35:24 +00:00
|
|
|
expect(reason).to.equal('target closed')
|
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.false()
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-06-17 22:35:24 +00:00
|
|
|
|
2016-01-22 08:47:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.attach()
|
2016-03-28 23:19:18 +00:00
|
|
|
} catch (err) {
|
2017-10-27 00:32:04 +00:00
|
|
|
done(`unexpected error : ${err}`)
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.detach()
|
|
|
|
})
|
2018-09-13 06:32:30 +00:00
|
|
|
|
|
|
|
it('doesn\'t disconnect an active devtools session', done => {
|
|
|
|
w.webContents.loadURL('about:blank')
|
|
|
|
try {
|
|
|
|
w.webContents.debugger.attach()
|
|
|
|
} catch (err) {
|
|
|
|
return done(`unexpected error : ${err}`)
|
|
|
|
}
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
w.webContents.once('devtools-opened', () => {
|
|
|
|
w.webContents.debugger.detach()
|
|
|
|
})
|
|
|
|
w.webContents.debugger.on('detach', (e, reason) => {
|
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.false()
|
|
|
|
expect(w.devToolsWebContents.isDestroyed()).to.be.false()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
describe('debugger.sendCommand', () => {
|
2017-04-28 16:28:11 +00:00
|
|
|
let server
|
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
afterEach(() => {
|
2017-04-28 16:28:11 +00:00
|
|
|
if (server != null) {
|
|
|
|
server.close()
|
|
|
|
server = null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-02-13 17:23:53 +00:00
|
|
|
it('returns response', async () => {
|
|
|
|
w.webContents.loadURL('about:blank')
|
|
|
|
w.webContents.debugger.attach()
|
|
|
|
|
|
|
|
const params = { 'expression': '4+2' }
|
|
|
|
const res = await w.webContents.debugger.sendCommand('Runtime.evaluate', params)
|
|
|
|
|
|
|
|
expect(res.wasThrown).to.be.undefined()
|
|
|
|
expect(res.result.value).to.equal(6)
|
|
|
|
|
|
|
|
w.webContents.debugger.detach()
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(miniak): remove when promisification is complete
|
|
|
|
it('returns response (callback)', done => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.loadURL('about:blank')
|
2016-01-22 08:47:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.attach()
|
2016-03-28 23:19:18 +00:00
|
|
|
} catch (err) {
|
2018-06-17 22:35:24 +00:00
|
|
|
return done(`unexpected error : ${err}`)
|
2016-03-25 20:03:49 +00:00
|
|
|
}
|
2018-06-17 22:35:24 +00:00
|
|
|
|
|
|
|
const callback = (err, res) => {
|
2018-09-27 07:43:45 +00:00
|
|
|
expect(err).to.be.null()
|
2018-06-17 22:35:24 +00:00
|
|
|
expect(res.wasThrown).to.be.undefined()
|
|
|
|
expect(res.result.value).to.equal(6)
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.detach()
|
|
|
|
done()
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2018-06-17 22:35:24 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const params = { 'expression': '4+2' }
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.sendCommand('Runtime.evaluate', params, callback)
|
|
|
|
})
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2019-02-13 17:23:53 +00:00
|
|
|
it('returns response when devtools is opened', async () => {
|
|
|
|
w.webContents.loadURL('about:blank')
|
|
|
|
w.webContents.debugger.attach()
|
|
|
|
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
await emittedOnce(w.webContents, 'devtools-opened')
|
|
|
|
|
|
|
|
const params = { 'expression': '4+2' }
|
|
|
|
const res = await w.webContents.debugger.sendCommand('Runtime.evaluate', params)
|
|
|
|
|
|
|
|
expect(res.wasThrown).to.be.undefined()
|
|
|
|
expect(res.result.value).to.equal(6)
|
|
|
|
|
|
|
|
w.webContents.debugger.detach()
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(miniak): remove when promisification is complete
|
|
|
|
it('returns response when devtools is opened (callback)', done => {
|
2018-09-13 06:32:30 +00:00
|
|
|
w.webContents.loadURL('about:blank')
|
|
|
|
try {
|
|
|
|
w.webContents.debugger.attach()
|
|
|
|
} catch (err) {
|
|
|
|
return done(`unexpected error : ${err}`)
|
|
|
|
}
|
|
|
|
const callback = (err, res) => {
|
2018-09-27 07:43:45 +00:00
|
|
|
expect(err).to.be.null()
|
2018-09-13 06:32:30 +00:00
|
|
|
expect(res.wasThrown).to.be.undefined()
|
|
|
|
expect(res.result.value).to.equal(6)
|
|
|
|
w.webContents.debugger.detach()
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
w.webContents.once('devtools-opened', () => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const params = { 'expression': '4+2' }
|
2018-09-13 06:32:30 +00:00
|
|
|
w.webContents.debugger.sendCommand('Runtime.evaluate', params, callback)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-06-17 22:35:24 +00:00
|
|
|
it('fires message event', done => {
|
2017-10-27 00:32:04 +00:00
|
|
|
const url = process.platform !== 'win32'
|
|
|
|
? `file://${path.join(fixtures, 'pages', 'a.html')}`
|
2018-06-17 22:35:24 +00:00
|
|
|
: `file:///${path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/')}`
|
2018-09-04 14:50:53 +00:00
|
|
|
w.webContents.loadFile(path.join(fixtures, 'pages', 'a.html'))
|
2018-06-17 22:35:24 +00:00
|
|
|
|
2016-01-22 08:47:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.attach()
|
2016-03-28 23:19:18 +00:00
|
|
|
} catch (err) {
|
2018-06-17 22:35:24 +00:00
|
|
|
done(`unexpected error : ${err}`)
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2018-06-17 22:35:24 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
w.webContents.debugger.on('message', (e, method, params) => {
|
2016-03-28 23:31:06 +00:00
|
|
|
if (method === 'Console.messageAdded') {
|
2018-06-17 22:35:24 +00:00
|
|
|
expect(params.message.level).to.equal('log')
|
|
|
|
expect(params.message.url).to.equal(url)
|
|
|
|
expect(params.message.text).to.equal('a')
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.detach()
|
|
|
|
done()
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
w.webContents.debugger.sendCommand('Console.enable')
|
|
|
|
})
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2019-02-13 17:23:53 +00:00
|
|
|
it('returns error message when command fails', async () => {
|
|
|
|
w.webContents.loadURL('about:blank')
|
|
|
|
w.webContents.debugger.attach()
|
|
|
|
|
|
|
|
const promise = w.webContents.debugger.sendCommand('Test')
|
|
|
|
await expect(promise).to.be.eventually.rejectedWith(Error, "'Test' wasn't found")
|
|
|
|
|
|
|
|
w.webContents.debugger.detach()
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO(miniak): remove when promisification is complete
|
|
|
|
it('returns error message when command fails (callback)', done => {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.loadURL('about:blank')
|
2016-01-22 08:47:23 +00:00
|
|
|
try {
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.attach()
|
2016-03-28 23:19:18 +00:00
|
|
|
} catch (err) {
|
2017-10-27 00:32:04 +00:00
|
|
|
done(`unexpected error : ${err}`)
|
2016-01-22 08:47:23 +00:00
|
|
|
}
|
2018-06-17 22:35:24 +00:00
|
|
|
|
2019-02-13 17:23:53 +00:00
|
|
|
w.webContents.debugger.sendCommand('Test', (err, res) => {
|
|
|
|
expect(err).to.be.an.instanceOf(Error).with.property('message', "'Test' wasn't found")
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.detach()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-04-28 16:28:11 +00:00
|
|
|
|
2018-09-06 14:44:22 +00:00
|
|
|
it('handles valid unicode characters in message', (done) => {
|
2017-04-28 16:28:11 +00:00
|
|
|
try {
|
|
|
|
w.webContents.debugger.attach()
|
|
|
|
} catch (err) {
|
2017-10-27 00:32:04 +00:00
|
|
|
done(`unexpected error : ${err}`)
|
2017-04-28 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.webContents.debugger.on('message', (event, method, params) => {
|
|
|
|
if (method === 'Network.loadingFinished') {
|
|
|
|
w.webContents.debugger.sendCommand('Network.getResponseBody', {
|
|
|
|
requestId: params.requestId
|
2018-02-02 00:47:52 +00:00
|
|
|
}, (_, data) => {
|
2018-06-17 22:35:24 +00:00
|
|
|
expect(data.body).to.equal('\u0024')
|
2017-04-28 16:28:11 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-02-02 00:47:52 +00:00
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
|
|
|
|
res.end('\u0024')
|
|
|
|
})
|
|
|
|
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
w.webContents.debugger.sendCommand('Network.enable')
|
|
|
|
w.loadURL(`http://127.0.0.1:${server.address().port}`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-09-06 14:44:22 +00:00
|
|
|
it('does not crash for invalid unicode characters in message', (done) => {
|
2018-02-02 00:47:52 +00:00
|
|
|
try {
|
|
|
|
w.webContents.debugger.attach()
|
|
|
|
} catch (err) {
|
|
|
|
done(`unexpected error : ${err}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.webContents.debugger.on('message', (event, method, params) => {
|
|
|
|
// loadingFinished indicates that page has been loaded and it did not
|
|
|
|
// crash because of invalid UTF-8 data
|
|
|
|
if (method === 'Network.loadingFinished') {
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-04-28 16:28:11 +00:00
|
|
|
server = http.createServer((req, res) => {
|
|
|
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
|
|
|
|
res.end('\uFFFF')
|
|
|
|
})
|
|
|
|
|
|
|
|
server.listen(0, '127.0.0.1', () => {
|
|
|
|
w.webContents.debugger.sendCommand('Network.enable')
|
|
|
|
w.loadURL(`http://127.0.0.1:${server.address().port}`)
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|