From 0defecf1975b6bc60da8d53ba8f5bca55b113c66 Mon Sep 17 00:00:00 2001 From: JoelEinbinder Date: Tue, 6 Mar 2018 22:01:43 -0800 Subject: [PATCH] fix(test): add missing test for making sure chrome is closed (#2109) --- test/fixtures/closeme.js | 5 +++++ test/test.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/fixtures/closeme.js diff --git a/test/fixtures/closeme.js b/test/fixtures/closeme.js new file mode 100644 index 00000000..66a1620f --- /dev/null +++ b/test/fixtures/closeme.js @@ -0,0 +1,5 @@ +(async() => { + const [, , puppeteerRoot, options] = process.argv; + const browser = await require(puppeteerRoot).launch(JSON.parse(options)); + console.log(browser.wsEndpoint()); +})(); diff --git a/test/test.js b/test/test.js index 10abfb46..7d2c54b4 100644 --- a/test/test.js +++ b/test/test.js @@ -283,6 +283,27 @@ describe('Puppeteer', function() { expect(dumpioData).toContain(dumpioTextToLog); }); + it('should close the browser when the node process closes', async({ server }) => { + const {spawn, execSync} = require('child_process'); + const res = spawn('node', [path.join(__dirname, 'fixtures', 'closeme.js'), PROJECT_ROOT, JSON.stringify(defaultBrowserOptions)]); + let wsEndPointCallback; + const wsEndPointPromise = new Promise(x => wsEndPointCallback = x); + let output = ''; + res.stdout.on('data', data => { + output += data; + if (output.indexOf('\n')) + wsEndPointCallback(output.substring(0, output.indexOf('\n'))); + }); + const browser = await puppeteer.connect({ browserWSEndpoint: await wsEndPointPromise }); + const promises = [ + new Promise(resolve => browser.once('disconnected', resolve)), + new Promise(resolve => res.on('close', resolve))]; + if (process.platform === 'win32') + execSync(`taskkill /pid ${res.pid} /T /F`); + else + process.kill(res.pid); + await Promise.all(promises); + }); }); describe('Puppeteer.connect', function() { it('should be able to connect multiple times to the same browser', async({server}) => {