| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2019 Google Inc. All rights reserved. | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  | const {spawn, execSync} = require('child_process'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 13:20:13 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @type {TestSuite} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  | module.exports.describe = function({testRunner, expect, product, browserType, playwrightPath, defaultBrowserOptions, WIN, FFOX, CHROMIUM, WEBKIT}) { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   const {describe, xdescribe, fdescribe} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-12-19 15:47:35 -08:00
										 |  |  |   const {it, fit, xit, dit} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |   async function testSignal(action) { | 
					
						
							|  |  |  |     const options = Object.assign({}, defaultBrowserOptions, { | 
					
						
							|  |  |  |       // Disable DUMPIO to cleanly read stdout.
 | 
					
						
							|  |  |  |       dumpio: false, | 
					
						
							|  |  |  |       handleSIGINT: true, | 
					
						
							|  |  |  |       handleSIGTERM: true, | 
					
						
							|  |  |  |       handleSIGHUP: true, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const res = spawn('node', [path.join(__dirname, 'fixtures', 'closeme.js'), playwrightPath, product, JSON.stringify(options)]); | 
					
						
							|  |  |  |     let wsEndPointCallback; | 
					
						
							|  |  |  |     const wsEndPointPromise = new Promise(x => wsEndPointCallback = x); | 
					
						
							|  |  |  |     let output = ''; | 
					
						
							|  |  |  |     let browserExitCode = 'none'; | 
					
						
							|  |  |  |     let browserSignal = 'none'; | 
					
						
							|  |  |  |     let browserPid; | 
					
						
							|  |  |  |     res.stdout.on('data', data => { | 
					
						
							|  |  |  |       output += data; | 
					
						
							|  |  |  |       // Uncomment to debug these tests.
 | 
					
						
							|  |  |  |       // console.log(data.toString());
 | 
					
						
							|  |  |  |       let match = output.match(/browserWS:(.+):browserWS/); | 
					
						
							|  |  |  |       if (match) | 
					
						
							|  |  |  |         wsEndPointCallback(match[1]); | 
					
						
							|  |  |  |       match = output.match(/browserClose:([^:]+):([^:]+):browserClose/); | 
					
						
							|  |  |  |       if (match) { | 
					
						
							|  |  |  |         browserExitCode = match[1]; | 
					
						
							|  |  |  |         browserSignal = match[2]; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       match = output.match(/browserPid:([^:]+):browserPid/); | 
					
						
							|  |  |  |       if (match) | 
					
						
							|  |  |  |         browserPid = +match[1]; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     res.on('error', (...args) => console.log("ERROR", ...args)); | 
					
						
							| 
									
										
										
										
											2020-03-12 17:58:00 -07:00
										 |  |  |     const browser = await browserType.connect({ wsEndpoint: await wsEndPointPromise }); | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |     const promises = [ | 
					
						
							|  |  |  |       new Promise(resolve => browser.once('disconnected', resolve)), | 
					
						
							|  |  |  |       new Promise(resolve => res.on('exit', resolve)), | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  |     action(res, browserPid); | 
					
						
							|  |  |  |     const [, exitCode] = await Promise.all(promises); | 
					
						
							|  |  |  |     return { exitCode, browserSignal, browserExitCode, output }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-20 15:10:13 -07:00
										 |  |  |   describe('Fixtures', function() { | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |     it.slow()('should dump browser process stderr', async({server}) => { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       let dumpioData = ''; | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       const res = spawn('node', [path.join(__dirname, 'fixtures', 'dumpio.js'), playwrightPath, product, 'usewebsocket']); | 
					
						
							| 
									
										
										
										
											2020-03-23 15:08:02 -07:00
										 |  |  |       res.stdout.on('data', data => dumpioData += data.toString('utf8')); | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       await new Promise(resolve => res.on('close', resolve)); | 
					
						
							|  |  |  |       expect(dumpioData).toContain('message from dumpio'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |     it.slow()('should dump browser process stderr', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       let dumpioData = ''; | 
					
						
							|  |  |  |       const res = spawn('node', [path.join(__dirname, 'fixtures', 'dumpio.js'), playwrightPath, product]); | 
					
						
							| 
									
										
										
										
											2020-03-23 15:08:02 -07:00
										 |  |  |       res.stdout.on('data', data => dumpioData += data.toString('utf8')); | 
					
						
							| 
									
										
										
										
											2020-02-05 16:36:36 -08:00
										 |  |  |       await new Promise(resolve => res.on('close', resolve)); | 
					
						
							| 
									
										
										
										
											2019-12-20 15:10:13 -07:00
										 |  |  |       expect(dumpioData).toContain('message from dumpio'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |     it.slow()('should close the browser when the node process closes', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |       const result = await testSignal(child => { | 
					
						
							| 
									
										
										
										
											2020-02-07 14:44:05 -08:00
										 |  |  |         if (WIN) | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |           execSync(`taskkill /pid ${child.pid} /T /F`); | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           process.kill(child.pid); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       expect(result.exitCode).toBe(WIN ? 1 : 0); | 
					
						
							|  |  |  |       // We might not get browser exitCode in time when killing the parent node process,
 | 
					
						
							|  |  |  |       // so we don't check it here.
 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     if (!WIN) { | 
					
						
							|  |  |  |       // Cannot reliably send signals on Windows.
 | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should report browser close signal', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal((child, browserPid) => { | 
					
						
							|  |  |  |           process.kill(browserPid); | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGINT'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(130); | 
					
						
							|  |  |  |         expect(result.browserExitCode).toBe('null'); | 
					
						
							|  |  |  |         expect(result.browserSignal).toBe('SIGTERM'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should report browser close signal 2', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal((child, browserPid) => { | 
					
						
							|  |  |  |           process.kill(browserPid, 'SIGKILL'); | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGINT'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(130); | 
					
						
							|  |  |  |         expect(result.browserExitCode).toBe('null'); | 
					
						
							|  |  |  |         expect(result.browserSignal).toBe('SIGKILL'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should close the browser on SIGINT', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal(child => process.kill(child.pid, 'SIGINT')); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(130); | 
					
						
							|  |  |  |         expect(result.browserExitCode).toBe('0'); | 
					
						
							|  |  |  |         expect(result.browserSignal).toBe('null'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should close the browser on SIGTERM', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal(child => process.kill(child.pid, 'SIGTERM')); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(0); | 
					
						
							|  |  |  |         expect(result.browserExitCode).toBe('0'); | 
					
						
							|  |  |  |         expect(result.browserSignal).toBe('null'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should close the browser on SIGHUP', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal(child => process.kill(child.pid, 'SIGHUP')); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(0); | 
					
						
							|  |  |  |         expect(result.browserExitCode).toBe('0'); | 
					
						
							|  |  |  |         expect(result.browserSignal).toBe('null'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should kill the browser on double SIGINT', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal(child => { | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGINT'); | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGINT'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(130); | 
					
						
							|  |  |  |         // TODO: ideally, we would expect the SIGKILL on the browser from
 | 
					
						
							|  |  |  |         // force kill, but that's racy with sending two signals.
 | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should kill the browser on SIGINT + SIGTERM', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal(child => { | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGINT'); | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGTERM'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(130); | 
					
						
							|  |  |  |         // TODO: ideally, we would expect the SIGKILL on the browser from
 | 
					
						
							|  |  |  |         // force kill, but that's racy with sending two signals.
 | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-03-20 19:49:35 -07:00
										 |  |  |       it.slow()('should kill the browser on SIGTERM + SIGINT', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:07:53 -08:00
										 |  |  |         const result = await testSignal(child => { | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGTERM'); | 
					
						
							|  |  |  |           process.kill(child.pid, 'SIGINT'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         expect(result.exitCode).toBe(130); | 
					
						
							|  |  |  |         // TODO: ideally, we would expect the SIGKILL on the browser from
 | 
					
						
							|  |  |  |         // force kill, but that's racy with sending two signals.
 | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | }; |