electron/spec/api-debugger-spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

219 строки
8.0 KiB
TypeScript
Исходник Обычный вид История

import { expect } from 'chai';
import * as http from 'http';
import * as path from 'path';
import { AddressInfo } from 'net';
import { BrowserWindow } from 'electron/main';
import { closeAllWindows } from './window-helpers';
import { emittedOnce, emittedUntil } from './events-helpers';
2018-06-18 01:35:24 +03:00
2017-10-27 03:35:33 +03:00
describe('debugger module', () => {
const fixtures = path.resolve(__dirname, 'fixtures');
let w: BrowserWindow;
2017-10-27 03:32:04 +03:00
beforeEach(() => {
2016-01-22 11:47:23 +03:00
w = new BrowserWindow({
show: false,
width: 400,
height: 400
2016-03-25 23:03:49 +03:00
});
});
afterEach(closeAllWindows);
2016-01-22 11:47:23 +03:00
2017-10-27 03:32:04 +03: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();
2016-03-25 23:03:49 +03:00
});
2016-01-22 11:47:23 +03:00
2018-06-18 01:35:24 +03:00
it('fails when protocol version is not supported', done => {
2016-01-22 11:47:23 +03:00
try {
2016-03-25 23:03:49 +03:00
w.webContents.debugger.attach('2.0');
2016-03-29 02:19:18 +03:00
} catch (err) {
2018-06-18 01:35:24 +03:00
expect(w.webContents.debugger.isAttached()).to.be.false();
2016-03-25 23:03:49 +03:00
done();
2016-01-22 11:47:23 +03:00
}
2016-03-25 23:03:49 +03:00
});
2016-01-22 11:47:23 +03:00
it('attaches when no protocol version is specified', async () => {
w.webContents.debugger.attach();
2018-06-18 01:35:24 +03:00
expect(w.webContents.debugger.isAttached()).to.be.true();
2016-03-25 23:03:49 +03:00
});
});
2016-01-22 11:47:23 +03:00
2017-10-27 03:32:04 +03:00
describe('debugger.detach', () => {
it('fires detach event', async () => {
const detach = emittedOnce(w.webContents.debugger, 'detach');
w.webContents.debugger.attach();
2016-03-25 23:03:49 +03:00
w.webContents.debugger.detach();
const [, reason] = await detach;
expect(reason).to.equal('target closed');
expect(w.webContents.debugger.isAttached()).to.be.false();
2016-03-25 23:03:49 +03:00
});
it('doesn\'t disconnect an active devtools session', async () => {
w.webContents.loadURL('about:blank');
const detach = emittedOnce(w.webContents.debugger, 'detach');
w.webContents.debugger.attach();
w.webContents.openDevTools();
w.webContents.once('devtools-opened', () => {
w.webContents.debugger.detach();
});
await detach;
expect(w.webContents.debugger.isAttached()).to.be.false();
expect((w as any).devToolsWebContents.isDestroyed()).to.be.false();
});
2016-03-25 23:03:49 +03:00
});
2016-01-22 11:47:23 +03:00
2017-10-27 03:32:04 +03:00
describe('debugger.sendCommand', () => {
let server: http.Server;
2017-10-27 03:32:04 +03:00
afterEach(() => {
if (server != null) {
server.close();
server = null as any;
}
});
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();
});
it('returns response when devtools is opened', async () => {
w.webContents.loadURL('about:blank');
w.webContents.debugger.attach();
const opened = emittedOnce(w.webContents, 'devtools-opened');
w.webContents.openDevTools();
await 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();
});
it('fires message event', async () => {
2017-10-27 03:32:04 +03:00
const url = process.platform !== 'win32'
? `file://${path.join(fixtures, 'pages', 'a.html')}`
2018-06-18 01:35:24 +03: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');
2016-03-25 23:03:49 +03: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');
2016-03-25 23:03:49 +03:00
});
2016-01-22 11:47:23 +03: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();
});
it('handles valid unicode characters in message', async () => {
2018-02-02 03:47:52 +03:00
server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end('\u0024');
});
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
2018-02-02 03:47:52 +03:00
w.loadURL(`http://127.0.0.1:${(server.address() as AddressInfo).port}`);
// 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 03:47:52 +03:00
});
it('does not crash for invalid unicode characters in message', (done) => {
2018-02-02 03:47:52 +03:00
try {
w.webContents.debugger.attach();
} catch (err) {
done(`unexpected error : ${err}`);
}
2019-11-01 23:37:02 +03:00
w.webContents.debugger.on('message', (event, method) => {
2018-02-02 03:47:52 +03:00
// loadingFinished indicates that page has been loaded and it did not
// crash because of invalid UTF-8 data
if (method === 'Network.loadingFinished') {
done();
}
});
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() as AddressInfo).port}`);
});
});
it('uses empty sessionId by default', async () => {
w.webContents.loadURL('about:blank');
w.webContents.debugger.attach();
const onMessage = emittedOnce(w.webContents.debugger, 'message');
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 23:03:49 +03:00
});
});