electron/spec-main/api-debugger-spec.ts

198 lines
6.5 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import * as http from 'http';
import * as path from 'path';
import { AddressInfo } from 'net';
import { BrowserWindow } from 'electron/main';
2020-03-20 20:28:31 +00:00
import { closeAllWindows } from './window-helpers';
import { emittedOnce, emittedUntil } from './events-helpers';
2018-06-17 22:35:24 +00:00
2017-10-27 00:35:33 +00:00
describe('debugger module', () => {
2020-03-20 20:28:31 +00:00
const fixtures = path.resolve(__dirname, '..', 'spec', 'fixtures');
let w: BrowserWindow;
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
2020-03-20 20:28:31 +00:00
});
});
2020-03-20 20:28:31 +00:00
afterEach(closeAllWindows);
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
describe('debugger.attach', () => {
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();
2020-03-20 20:28:31 +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 {
2020-03-20 20:28:31 +00:00
w.webContents.debugger.attach('2.0');
2016-03-28 23:19:18 +00:00
} catch (err) {
2020-03-20 20:28:31 +00:00
expect(w.webContents.debugger.isAttached()).to.be.false();
done();
2016-01-22 08:47:23 +00:00
}
2020-03-20 20:28:31 +00:00
});
2016-01-22 08:47:23 +00:00
it('attaches when no protocol version is specified', async () => {
w.webContents.debugger.attach();
2020-03-20 20:28:31 +00:00
expect(w.webContents.debugger.isAttached()).to.be.true();
});
});
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
describe('debugger.detach', () => {
it('fires detach event', async () => {
const detach = emittedOnce(w.webContents.debugger, 'detach');
w.webContents.debugger.attach();
2020-03-20 20:28:31 +00:00
w.webContents.debugger.detach();
const [, reason] = await detach;
expect(reason).to.equal('target closed');
expect(w.webContents.debugger.isAttached()).to.be.false();
2020-03-20 20:28:31 +00:00
});
it('doesn\'t disconnect an active devtools session', async () => {
2020-03-20 20:28:31 +00:00
w.webContents.loadURL('about:blank');
const detach = emittedOnce(w.webContents.debugger, 'detach');
w.webContents.debugger.attach();
2020-03-20 20:28:31 +00:00
w.webContents.openDevTools();
w.webContents.once('devtools-opened', () => {
2020-03-20 20:28:31 +00:00
w.webContents.debugger.detach();
});
await detach;
expect(w.webContents.debugger.isAttached()).to.be.false();
expect((w as any).devToolsWebContents.isDestroyed()).to.be.false();
2020-03-20 20:28:31 +00:00
});
});
2016-01-22 08:47:23 +00:00
2017-10-27 00:32:04 +00:00
describe('debugger.sendCommand', () => {
2020-03-20 20:28:31 +00:00
let server: http.Server;
2017-10-27 00:32:04 +00:00
afterEach(() => {
if (server != null) {
2020-03-20 20:28:31 +00:00
server.close();
server = null as any;
}
2020-03-20 20:28:31 +00:00
});
it('returns response', async () => {
2020-03-20 20:28:31 +00:00
w.webContents.loadURL('about:blank');
w.webContents.debugger.attach();
2020-03-20 20:28:31 +00:00
const params = { expression: '4+2' };
const res = await w.webContents.debugger.sendCommand('Runtime.evaluate', params);
2020-03-20 20:28:31 +00:00
expect(res.wasThrown).to.be.undefined();
expect(res.result.value).to.equal(6);
2020-03-20 20:28:31 +00:00
w.webContents.debugger.detach();
});
it('returns response when devtools is opened', async () => {
2020-03-20 20:28:31 +00:00
w.webContents.loadURL('about:blank');
w.webContents.debugger.attach();
2020-03-20 20:28:31 +00:00
const opened = emittedOnce(w.webContents, 'devtools-opened');
w.webContents.openDevTools();
await opened;
2020-03-20 20:28:31 +00:00
const params = { expression: '4+2' };
const res = await w.webContents.debugger.sendCommand('Runtime.evaluate', params);
2020-03-20 20:28:31 +00:00
expect(res.wasThrown).to.be.undefined();
expect(res.result.value).to.equal(6);
2020-03-20 20:28:31 +00:00
w.webContents.debugger.detach();
});
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')}`
2020-03-20 20:28:31 +00:00
: `file:///${path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/')}`;
w.webContents.loadURL(url);
w.webContents.debugger.attach();
const message = emittedUntil(w.webContents.debugger, 'message',
(event: Electron.Event, method: string) => method === 'Console.messageAdded');
2020-03-20 20:28:31 +00:00
w.webContents.debugger.sendCommand('Console.enable');
const [,, params] = await message;
w.webContents.debugger.detach();
expect((params as any).message.level).to.equal('log');
expect((params as any).message.url).to.equal(url);
expect((params as any).message.text).to.equal('a');
2020-03-20 20:28:31 +00:00
});
2016-01-22 08:47:23 +00:00
it('returns error message when command fails', async () => {
2020-03-20 20:28:31 +00:00
w.webContents.loadURL('about:blank');
w.webContents.debugger.attach();
2020-03-20 20:28:31 +00:00
const promise = w.webContents.debugger.sendCommand('Test');
await expect(promise).to.be.eventually.rejectedWith(Error, "'Test' wasn't found");
2020-03-20 20:28:31 +00:00
w.webContents.debugger.detach();
});
// TODO(deepak1556): Fix and enable with upgrade
it.skip('handles valid unicode characters in message', (done) => {
try {
2020-03-20 20:28:31 +00:00
w.webContents.debugger.attach();
} catch (err) {
2020-03-20 20:28:31 +00:00
done(`unexpected error : ${err}`);
}
2020-03-20 20:28:31 +00:00
let requestId : number;
w.webContents.debugger.on('message', (event, method, params) => {
if (method === 'Network.responseReceived' &&
params.response.url.startsWith('http://127.0.0.1')) {
2020-03-20 20:28:31 +00:00
requestId = params.requestId;
} else if (method === 'Network.loadingFinished' &&
params.requestId === requestId) {
w.webContents.debugger.sendCommand('Network.getResponseBody', {
requestId: params.requestId
}).then(data => {
2020-03-20 20:28:31 +00:00
expect(data.body).to.equal('\u0024');
done();
}).catch(result => done(result));
}
2020-03-20 20:28:31 +00:00
});
2018-02-02 00:47:52 +00:00
server = http.createServer((req, res) => {
2020-03-20 20:28:31 +00:00
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end('\u0024');
});
2018-02-02 00:47:52 +00:00
server.listen(0, '127.0.0.1', () => {
2020-03-20 20:28:31 +00:00
w.webContents.debugger.sendCommand('Network.enable');
w.loadURL(`http://127.0.0.1:${(server.address() as AddressInfo).port}`);
});
});
2018-02-02 00:47:52 +00:00
it('does not crash for invalid unicode characters in message', (done) => {
2018-02-02 00:47:52 +00:00
try {
2020-03-20 20:28:31 +00:00
w.webContents.debugger.attach();
2018-02-02 00:47:52 +00:00
} catch (err) {
2020-03-20 20:28:31 +00:00
done(`unexpected error : ${err}`);
2018-02-02 00:47:52 +00:00
}
2019-11-01 20:37:02 +00:00
w.webContents.debugger.on('message', (event, method) => {
2018-02-02 00:47:52 +00:00
// loadingFinished indicates that page has been loaded and it did not
// crash because of invalid UTF-8 data
if (method === 'Network.loadingFinished') {
2020-03-20 20:28:31 +00:00
done();
2018-02-02 00:47:52 +00:00
}
2020-03-20 20:28:31 +00:00
});
2018-02-02 00:47:52 +00:00
server = http.createServer((req, res) => {
2020-03-20 20:28:31 +00:00
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end('\uFFFF');
});
server.listen(0, '127.0.0.1', () => {
2020-03-20 20:28:31 +00:00
w.webContents.debugger.sendCommand('Network.enable');
w.loadURL(`http://127.0.0.1:${(server.address() as AddressInfo).port}`);
});
});
});
});