| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2019 Google Inc. All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-05 14:12:22 -08:00
										 |  |  | const util = require('util'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | const os = require('os'); | 
					
						
							| 
									
										
										
										
											2019-12-11 07:38:20 -08:00
										 |  |  | const readFileAsync = util.promisify(fs.readFile); | 
					
						
							| 
									
										
										
										
											2019-12-05 14:12:22 -08:00
										 |  |  | const rmAsync = util.promisify(require('rimraf')); | 
					
						
							|  |  |  | const mkdtempAsync = util.promisify(fs.mkdtemp); | 
					
						
							| 
									
										
										
										
											2019-12-11 07:38:20 -08:00
										 |  |  | const statAsync = util.promisify(fs.stat); | 
					
						
							| 
									
										
										
										
											2019-12-05 14:12:22 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 16:15:41 -08:00
										 |  |  | const TMP_FOLDER = path.join(os.tmpdir(), 'pw_tmp_folder-'); | 
					
						
							| 
									
										
										
										
											2019-12-04 16:12:43 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 17:11:45 -08:00
										 |  |  | module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, WIN}) { | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-10 14:02:48 -08:00
										 |  |  |   describe('CrPlaywright', function() { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |     describe('Playwright.launch |browserURL| option', function() { | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |       function getBrowserUrl(wsEndpoint) { | 
					
						
							|  |  |  |         const port = wsEndpoint.match(/ws:\/\/([0-9A-Za-z\.]*):(\d+)\//)[2]; | 
					
						
							|  |  |  |         return `http://127.0.0.1:${port}`; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       it('should be able to connect using browserUrl, with and without trailing slash', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         const browserApp = await playwright.launchBrowserApp({...defaultBrowserOptions, webSocket: true}); | 
					
						
							|  |  |  |         const browserURL = getBrowserUrl(browserApp.wsEndpoint()); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const browser1 = await playwright.connect({browserURL}); | 
					
						
							| 
									
										
										
										
											2019-12-18 18:07:11 -08:00
										 |  |  |         const page1 = await browser1.defaultContext().newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(await page1.evaluate(() => 7 * 8)).toBe(56); | 
					
						
							|  |  |  |         browser1.disconnect(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const browser2 = await playwright.connect({browserURL: browserURL + '/'}); | 
					
						
							| 
									
										
										
										
											2019-12-18 18:07:11 -08:00
										 |  |  |         const page2 = await browser2.defaultContext().newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(await page2.evaluate(() => 8 * 7)).toBe(56); | 
					
						
							|  |  |  |         browser2.disconnect(); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         await browserApp.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       it('should throw when using both browserWSEndpoint and browserURL', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         const browserApp = await playwright.launchBrowserApp({...defaultBrowserOptions, webSocket: true}); | 
					
						
							|  |  |  |         const browserURL = getBrowserUrl(browserApp.wsEndpoint()); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let error = null; | 
					
						
							| 
									
										
										
										
											2020-01-23 14:40:37 -08:00
										 |  |  |         await playwright.connect({browserURL, browserWSEndpoint: browserApp.wsEndpoint()}).catch(e => error = e); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(error.message).toContain('Exactly one of browserWSEndpoint, browserURL or transport'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         await browserApp.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |       it('should throw when trying to connect to non-existing browser', async({server}) => { | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         const browserApp = await playwright.launchBrowserApp({...defaultBrowserOptions, webSocket: true}); | 
					
						
							|  |  |  |         const browserURL = getBrowserUrl(browserApp.wsEndpoint()); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let error = null; | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         await playwright.connect({browserURL: browserURL + 'foo'}).catch(e => error = e); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(error.message).toContain('Failed to fetch browser webSocket url from'); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         await browserApp.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |     describe('Playwright.launch webSocket option', function() { | 
					
						
							|  |  |  |       it('should support the remote-debugging-port argument', async() => { | 
					
						
							|  |  |  |         const options = Object.assign({}, defaultBrowserOptions); | 
					
						
							|  |  |  |         options.args = ['--remote-debugging-port=0'].concat(options.args || []); | 
					
						
							| 
									
										
										
										
											2020-01-23 14:40:37 -08:00
										 |  |  |         const browserApp = await playwright.launchBrowserApp(options); | 
					
						
							|  |  |  |         const browser = await playwright.connect(browserApp.connectOptions()); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |         expect(browserApp.wsEndpoint()).not.toBe(null); | 
					
						
							| 
									
										
										
										
											2019-12-18 18:07:11 -08:00
										 |  |  |         const page = await browser.defaultContext().newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(await page.evaluate('11 * 11')).toBe(121); | 
					
						
							|  |  |  |         await page.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 14:40:37 -08:00
										 |  |  |         await browserApp.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |       it('should support the remote-debugging-pipe argument', async() => { | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         const options = Object.assign({}, defaultBrowserOptions); | 
					
						
							|  |  |  |         options.args = ['--remote-debugging-pipe'].concat(options.args || []); | 
					
						
							| 
									
										
										
										
											2020-01-23 14:40:37 -08:00
										 |  |  |         const browserApp = await playwright.launchBrowserApp(options); | 
					
						
							|  |  |  |         const browser = await playwright.connect(browserApp.connectOptions()); | 
					
						
							|  |  |  |         expect(browserApp.wsEndpoint()).toBe(null); | 
					
						
							| 
									
										
										
										
											2019-12-18 18:07:11 -08:00
										 |  |  |         const page = await browser.defaultContext().newPage(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |         expect(await page.evaluate('11 * 11')).toBe(121); | 
					
						
							|  |  |  |         await page.close(); | 
					
						
							| 
									
										
										
										
											2020-01-23 14:40:37 -08:00
										 |  |  |         await browserApp.close(); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-01-23 17:45:31 -08:00
										 |  |  |       it('should throw with remote-debugging-pipe argument and webSocket', async() => { | 
					
						
							|  |  |  |         const options = Object.assign({webSocket: true}, defaultBrowserOptions); | 
					
						
							|  |  |  |         options.args = ['--remote-debugging-pipe'].concat(options.args || []); | 
					
						
							|  |  |  |         const error = await playwright.launchBrowserApp(options).catch(e => e); | 
					
						
							|  |  |  |         expect(error.message).toBe('Argument "--remote-debugging-pipe" is not compatible with "webSocket" launch option.'); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-12-04 16:12:43 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('Browser target events', function() { | 
					
						
							|  |  |  |     it('should work', async({server}) => { | 
					
						
							|  |  |  |       const browser = await playwright.launch(defaultBrowserOptions); | 
					
						
							|  |  |  |       const events = []; | 
					
						
							| 
									
										
										
										
											2019-12-19 14:51:49 -08:00
										 |  |  |       browser.on('targetcreated', () => events.push('CREATED')); | 
					
						
							|  |  |  |       browser.on('targetchanged', () => events.push('CHANGED')); | 
					
						
							|  |  |  |       browser.on('targetdestroyed', () => events.push('DESTROYED')); | 
					
						
							| 
									
										
										
										
											2019-12-18 18:07:11 -08:00
										 |  |  |       const page = await browser.defaultContext().newPage(); | 
					
						
							| 
									
										
										
										
											2019-12-04 16:12:43 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await page.close(); | 
					
						
							|  |  |  |       expect(events).toEqual(['CREATED', 'CHANGED', 'DESTROYED']); | 
					
						
							|  |  |  |       await browser.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |   describe('BrowserFetcher', function() { | 
					
						
							| 
									
										
										
										
											2019-12-11 07:38:20 -08:00
										 |  |  |     it('should download and extract linux binary', async({server}) => { | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |       const downloadsFolder = await mkdtempAsync(TMP_FOLDER); | 
					
						
							| 
									
										
										
										
											2020-01-14 10:07:26 -08:00
										 |  |  |       const browserFetcher = playwright._createBrowserFetcher({ | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |         platform: 'linux', | 
					
						
							|  |  |  |         path: downloadsFolder, | 
					
						
							|  |  |  |         host: server.PREFIX | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       let revisionInfo = browserFetcher.revisionInfo('123456'); | 
					
						
							|  |  |  |       server.setRoute(revisionInfo.url.substring(server.PREFIX.length), (req, res) => { | 
					
						
							|  |  |  |         server.serveFile(req, res, '/chromium-linux.zip'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(revisionInfo.local).toBe(false); | 
					
						
							| 
									
										
										
										
											2019-12-11 07:38:20 -08:00
										 |  |  |       expect(browserFetcher._platform).toBe('linux'); | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |       expect(await browserFetcher.canDownload('100000')).toBe(false); | 
					
						
							|  |  |  |       expect(await browserFetcher.canDownload('123456')).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       revisionInfo = await browserFetcher.download('123456'); | 
					
						
							|  |  |  |       expect(revisionInfo.local).toBe(true); | 
					
						
							|  |  |  |       expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe('LINUX BINARY\n'); | 
					
						
							|  |  |  |       const expectedPermissions = WIN ? 0666 : 0755; | 
					
						
							|  |  |  |       expect((await statAsync(revisionInfo.executablePath)).mode & 0777).toBe(expectedPermissions); | 
					
						
							|  |  |  |       expect(await browserFetcher.localRevisions()).toEqual(['123456']); | 
					
						
							|  |  |  |       await browserFetcher.remove('123456'); | 
					
						
							|  |  |  |       expect(await browserFetcher.localRevisions()).toEqual([]); | 
					
						
							|  |  |  |       await rmAsync(downloadsFolder); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-11-18 18:18:28 -08:00
										 |  |  | }; |