2019-10-14 05:32:11 +00:00
|
|
|
import { expect } from 'chai';
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as http from 'node:http';
|
|
|
|
import * as path from 'node:path';
|
2020-04-07 00:04:09 +00:00
|
|
|
import { BrowserWindow } from 'electron/main';
|
2023-01-25 21:01:25 +00:00
|
|
|
import { closeAllWindows } from './lib/window-helpers';
|
2023-02-23 23:53:53 +00:00
|
|
|
import { emittedUntil } from './lib/events-helpers';
|
2023-02-20 11:30:57 +00:00
|
|
|
import { listen } from './lib/spec-helpers';
|
2023-06-15 14:42:27 +00:00
|
|
|
import { once } from 'node:events';
|
2018-06-17 22:35:24 +00:00
|
|
|
|
2017-10-27 00:35:33 +00:00
|
|
|
describe('debugger module', () => {
|
2022-08-16 19:23:13 +00:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures');
|
2019-10-14 05:32:11 +00:00
|
|
|
let w: BrowserWindow;
|
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
|
|
|
|
2019-10-14 05:32:11 +00:00
|
|
|
afterEach(closeAllWindows);
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
describe('debugger.attach', () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
it('succeeds when devtools is already open', async () => {
|
|
|
|
await w.webContents.loadURL('about:blank');
|
|
|
|
w.webContents.openDevTools();
|
|
|
|
w.webContents.debugger.attach();
|
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.true();
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2023-11-17 09:44:03 +00:00
|
|
|
it('fails when protocol version is not supported', () => {
|
|
|
|
expect(() => w.webContents.debugger.attach('2.0')).to.throw();
|
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.false();
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2016-01-22 08:47:23 +00:00
|
|
|
|
2023-11-17 09:44:03 +00:00
|
|
|
it('attaches when no protocol version is specified', () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
w.webContents.debugger.attach();
|
2018-06-17 22:35:24 +00:00
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.true();
|
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.detach', () => {
|
2020-06-30 22:10:36 +00:00
|
|
|
it('fires detach event', async () => {
|
2023-02-23 23:53:53 +00:00
|
|
|
const detach = once(w.webContents.debugger, 'detach');
|
2020-06-30 22:10:36 +00:00
|
|
|
w.webContents.debugger.attach();
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.detach();
|
2020-06-30 22:10:36 +00:00
|
|
|
const [, reason] = await detach;
|
|
|
|
expect(reason).to.equal('target closed');
|
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.false();
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
2018-09-13 06:32:30 +00:00
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('doesn\'t disconnect an active devtools session', async () => {
|
2018-09-13 06:32:30 +00:00
|
|
|
w.webContents.loadURL('about:blank');
|
2023-02-23 23:53:53 +00:00
|
|
|
const detach = once(w.webContents.debugger, 'detach');
|
2020-06-30 22:10:36 +00:00
|
|
|
w.webContents.debugger.attach();
|
2018-09-13 06:32:30 +00:00
|
|
|
w.webContents.openDevTools();
|
|
|
|
w.webContents.once('devtools-opened', () => {
|
|
|
|
w.webContents.debugger.detach();
|
|
|
|
});
|
2020-06-30 22:10:36 +00:00
|
|
|
await detach;
|
|
|
|
expect(w.webContents.debugger.isAttached()).to.be.false();
|
2023-09-05 02:22:41 +00:00
|
|
|
expect(w.devToolsWebContents.isDestroyed()).to.be.false();
|
2018-09-13 06:32:30 +00:00
|
|
|
});
|
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', () => {
|
2019-10-14 05:32:11 +00:00
|
|
|
let server: http.Server;
|
2017-04-28 16:28:11 +00:00
|
|
|
|
2017-10-27 00:32:04 +00:00
|
|
|
afterEach(() => {
|
2017-04-28 16:28:11 +00:00
|
|
|
if (server != null) {
|
|
|
|
server.close();
|
2019-10-14 05:32:11 +00:00
|
|
|
server = null as any;
|
2017-04-28 16:28:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-13 17:23:53 +00:00
|
|
|
it('returns response', async () => {
|
|
|
|
w.webContents.loadURL('about:blank');
|
|
|
|
w.webContents.debugger.attach();
|
|
|
|
|
2020-03-20 15:12:18 +00:00
|
|
|
const params = { expression: '4+2' };
|
2019-02-13 17:23:53 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns response when devtools is opened', async () => {
|
|
|
|
w.webContents.loadURL('about:blank');
|
|
|
|
w.webContents.debugger.attach();
|
|
|
|
|
2023-02-23 23:53:53 +00:00
|
|
|
const opened = once(w.webContents, 'devtools-opened');
|
2019-02-13 17:23:53 +00:00
|
|
|
w.webContents.openDevTools();
|
2019-02-23 00:45:48 +00:00
|
|
|
await opened;
|
2019-02-13 17:23:53 +00:00
|
|
|
|
2020-03-20 15:12:18 +00:00
|
|
|
const params = { expression: '4+2' };
|
2019-02-13 17:23:53 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2020-06-30 22:10:36 +00:00
|
|
|
it('fires message event', async () => {
|
2017-10-27 00:32:04 +00:00
|
|
|
const url = process.platform !== 'win32'
|
|
|
|
? `file://${path.join(fixtures, 'pages', 'a.html')}`
|
2023-09-07 06:50:14 +00:00
|
|
|
: `file:///${path.join(fixtures, 'pages', 'a.html').replaceAll('\\', '/')}`;
|
2020-01-09 19:50:56 +00:00
|
|
|
w.webContents.loadURL(url);
|
2020-06-30 22:10:36 +00:00
|
|
|
w.webContents.debugger.attach();
|
|
|
|
const message = emittedUntil(w.webContents.debugger, 'message',
|
|
|
|
(event: Electron.Event, method: string) => method === 'Console.messageAdded');
|
2016-03-25 20:03:49 +00:00
|
|
|
w.webContents.debugger.sendCommand('Console.enable');
|
2020-06-30 22:10:36 +00:00
|
|
|
const [,, params] = await message;
|
|
|
|
w.webContents.debugger.detach();
|
2023-09-05 02:22:41 +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
|
|
|
});
|
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();
|
|
|
|
});
|
|
|
|
|
2022-03-21 23:38:46 +00:00
|
|
|
it('handles valid unicode characters in message', async () => {
|
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');
|
|
|
|
});
|
|
|
|
|
2023-02-20 11:30:57 +00:00
|
|
|
const { url } = await listen(server);
|
|
|
|
w.loadURL(url);
|
2022-03-21 23:38:46 +00:00
|
|
|
// If we do this synchronously, it's fast enough to attach and enable
|
|
|
|
// network capture before the load. If we do it before the loadURL, for
|
|
|
|
// some reason network capture doesn't get enabled soon enough and we get
|
|
|
|
// an error when calling `Network.getResponseBody`.
|
|
|
|
w.webContents.debugger.attach();
|
|
|
|
w.webContents.debugger.sendCommand('Network.enable');
|
|
|
|
const [,, { requestId }] = await emittedUntil(w.webContents.debugger, 'message', (_event: any, method: string, params: any) =>
|
|
|
|
method === 'Network.responseReceived' && params.response.url.startsWith('http://127.0.0.1'));
|
|
|
|
await emittedUntil(w.webContents.debugger, 'message', (_event: any, method: string, params: any) =>
|
|
|
|
method === 'Network.loadingFinished' && params.requestId === requestId);
|
|
|
|
const { body } = await w.webContents.debugger.sendCommand('Network.getResponseBody', { requestId });
|
|
|
|
expect(body).to.equal('\u0024');
|
2018-02-02 00:47:52 +00:00
|
|
|
});
|
|
|
|
|
2023-02-20 11:30:57 +00:00
|
|
|
it('does not crash for invalid unicode characters in message', async () => {
|
|
|
|
w.webContents.debugger.attach();
|
2018-02-02 00:47:52 +00:00
|
|
|
|
2023-02-20 11:30:57 +00:00
|
|
|
const loadingFinished = new Promise<void>(resolve => {
|
|
|
|
w.webContents.debugger.on('message', (event, method) => {
|
|
|
|
// loadingFinished indicates that page has been loaded and it did not
|
|
|
|
// crash because of invalid UTF-8 data
|
|
|
|
if (method === 'Network.loadingFinished') {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2018-02-02 00:47:52 +00:00
|
|
|
});
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2023-02-20 11:30:57 +00:00
|
|
|
const { url } = await listen(server);
|
|
|
|
w.webContents.debugger.sendCommand('Network.enable');
|
|
|
|
w.loadURL(url);
|
|
|
|
|
|
|
|
await loadingFinished;
|
2017-04-28 16:28:11 +00:00
|
|
|
});
|
2020-07-02 20:04:20 +00:00
|
|
|
|
|
|
|
it('uses empty sessionId by default', async () => {
|
|
|
|
w.webContents.loadURL('about:blank');
|
|
|
|
w.webContents.debugger.attach();
|
2023-02-23 23:53:53 +00:00
|
|
|
const onMessage = once(w.webContents.debugger, 'message');
|
2020-07-02 20:04:20 +00:00
|
|
|
await w.webContents.debugger.sendCommand('Target.setDiscoverTargets', { discover: true });
|
|
|
|
const [, method, params, sessionId] = await onMessage;
|
|
|
|
expect(method).to.equal('Target.targetCreated');
|
|
|
|
expect(params.targetInfo.targetId).to.not.be.empty();
|
|
|
|
expect(sessionId).to.be.empty();
|
|
|
|
w.webContents.debugger.detach();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates unique session id for each target', (done) => {
|
|
|
|
w.webContents.loadFile(path.join(__dirname, 'fixtures', 'sub-frames', 'debug-frames.html'));
|
|
|
|
w.webContents.debugger.attach();
|
|
|
|
let session: String;
|
|
|
|
|
|
|
|
w.webContents.debugger.on('message', (event, ...args) => {
|
|
|
|
const [method, params, sessionId] = args;
|
|
|
|
if (method === 'Target.targetCreated') {
|
|
|
|
w.webContents.debugger.sendCommand('Target.attachToTarget', { targetId: params.targetInfo.targetId, flatten: true }).then(result => {
|
|
|
|
session = result.sessionId;
|
|
|
|
w.webContents.debugger.sendCommand('Debugger.enable', {}, result.sessionId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (method === 'Debugger.scriptParsed') {
|
|
|
|
expect(sessionId).to.equal(session);
|
|
|
|
w.webContents.debugger.detach();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
w.webContents.debugger.sendCommand('Target.setDiscoverTargets', { discover: true });
|
|
|
|
});
|
2016-03-25 20:03:49 +00:00
|
|
|
});
|
|
|
|
});
|