| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { playwrightTest as test, expect } from '../config/browserTest'; | 
					
						
							| 
									
										
										
										
											2021-10-14 05:55:08 -04:00
										 |  |  | import type { TestInfo } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  | import path from 'path'; | 
					
						
							|  |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { start } from '../../packages/playwright-core/lib/outofprocess'; | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-18 15:32:09 -08:00
										 |  |  | const chromeDriver = process.env.PWTEST_CHROMEDRIVER; | 
					
						
							| 
									
										
										
										
											2022-03-26 08:36:25 -07:00
										 |  |  | const brokenDriver = path.join(__dirname, '..', 'assets', 'selenium-grid', 'broken-selenium-driver.js'); | 
					
						
							|  |  |  | const standalone_3_141_59 = path.join(__dirname, '..', 'assets', 'selenium-grid', 'selenium-server-standalone-3.141.59.jar'); | 
					
						
							|  |  |  | const selenium_4_0_0_rc1 = path.join(__dirname, '..', 'assets', 'selenium-grid', 'selenium-server-4.0.0-rc-1.jar'); | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | function writeSeleniumConfig(testInfo: TestInfo, port: number) { | 
					
						
							| 
									
										
										
										
											2022-03-26 08:36:25 -07:00
										 |  |  |   const template = path.join(__dirname, '..', 'assets', 'selenium-grid', `selenium-config-standalone.json`); | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  |   const content = fs.readFileSync(template, 'utf8').replace(/4444/g, String(port)); | 
					
						
							|  |  |  |   const file = testInfo.outputPath(`selenium-config-standalone.json`); | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  |   fs.writeFileSync(file, content, 'utf8'); | 
					
						
							|  |  |  |   return file; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test.skip(({ mode }) => mode !== 'default', 'Using test hooks'); | 
					
						
							| 
									
										
										
										
											2021-11-18 15:32:09 -08:00
										 |  |  | test.skip(!chromeDriver); | 
					
						
							| 
									
										
										
										
											2021-09-29 14:54:24 -07:00
										 |  |  | test.slow(); | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | test('selenium grid 3.141.59 standalone chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const port = testInfo.workerIndex + 15123; | 
					
						
							|  |  |  |   const grid = childProcess({ | 
					
						
							|  |  |  |     command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', standalone_3_141_59, '-config', writeSeleniumConfig(testInfo, port)], | 
					
						
							|  |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await waitForPort(port); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |   const __testHookSeleniumRemoteURL = `http://127.0.0.1:${port}/wd/hub`; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any); | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  |   const page = await browser.newPage(); | 
					
						
							|  |  |  |   await page.setContent('<title>Hello world</title><div>Get Started</div>'); | 
					
						
							|  |  |  |   await page.click('text=Get Started'); | 
					
						
							|  |  |  |   await expect(page).toHaveTitle('Hello world'); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(grid.output).toContain('Starting ChromeDriver'); | 
					
						
							|  |  |  |   expect(grid.output).toContain('Started new session'); | 
					
						
							|  |  |  |   await grid.waitForOutput('Removing session'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  | test('selenium grid 3.141.59 hub + node chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => { | 
					
						
							|  |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const port = testInfo.workerIndex + 15123; | 
					
						
							|  |  |  |   const hub = childProcess({ | 
					
						
							|  |  |  |     command: ['java', '-jar', standalone_3_141_59, '-role', 'hub', '-port', String(port)], | 
					
						
							|  |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await waitForPort(port); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const node = childProcess({ | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |     command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', standalone_3_141_59, '-role', 'node', '-host', '127.0.0.1', '-hub', `http://127.0.0.1:${port}/grid/register`], | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     node.waitForOutput('The node is registered to the hub and ready to use'), | 
					
						
							|  |  |  |     hub.waitForOutput('Registered a node'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |   const __testHookSeleniumRemoteURL = `http://127.0.0.1:${port}/wd/hub`; | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  |   const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any); | 
					
						
							|  |  |  |   const page = await browser.newPage(); | 
					
						
							|  |  |  |   await page.setContent('<title>Hello world</title><div>Get Started</div>'); | 
					
						
							|  |  |  |   await page.click('text=Get Started'); | 
					
						
							|  |  |  |   await expect(page).toHaveTitle('Hello world'); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(hub.output).toContain('Got a request to create a new session'); | 
					
						
							|  |  |  |   expect(node.output).toContain('Starting ChromeDriver'); | 
					
						
							|  |  |  |   expect(node.output).toContain('Started new session'); | 
					
						
							|  |  |  |   await node.waitForOutput('Removing session'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | test('selenium grid 4.0.0-rc-1 standalone chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const port = testInfo.workerIndex + 15123; | 
					
						
							|  |  |  |   const grid = childProcess({ | 
					
						
							|  |  |  |     command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', selenium_4_0_0_rc1, 'standalone', '--config', writeSeleniumConfig(testInfo, port)], | 
					
						
							|  |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await waitForPort(port); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |   const __testHookSeleniumRemoteURL = `http://127.0.0.1:${port}/wd/hub`; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any); | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  |   const page = await browser.newPage(); | 
					
						
							|  |  |  |   await page.setContent('<title>Hello world</title><div>Get Started</div>'); | 
					
						
							|  |  |  |   await page.click('text=Get Started'); | 
					
						
							|  |  |  |   await expect(page).toHaveTitle('Hello world'); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(grid.output).toContain('Starting ChromeDriver'); | 
					
						
							|  |  |  |   expect(grid.output).toContain('Session created'); | 
					
						
							|  |  |  |   await grid.waitForOutput('Deleted session'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  | test('selenium grid 4.0.0-rc-1 hub + node chromium', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => { | 
					
						
							|  |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const port = testInfo.workerIndex + 15123; | 
					
						
							|  |  |  |   const hub = childProcess({ | 
					
						
							|  |  |  |     command: ['java', '-jar', selenium_4_0_0_rc1, 'hub', '--port', String(port)], | 
					
						
							|  |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await waitForPort(port); | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |   const __testHookSeleniumRemoteURL = `http://127.0.0.1:${port}/wd/hub`; | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const node = childProcess({ | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |     command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', selenium_4_0_0_rc1, 'node', '--grid-url', `http://127.0.0.1:${port}`, '--port', String(port + 1)], | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await Promise.all([ | 
					
						
							|  |  |  |     node.waitForOutput('Node has been added'), | 
					
						
							|  |  |  |     hub.waitForOutput('from DOWN to UP'), | 
					
						
							|  |  |  |   ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const browser = await browserType.launch({ __testHookSeleniumRemoteURL } as any); | 
					
						
							|  |  |  |   const page = await browser.newPage(); | 
					
						
							|  |  |  |   await page.setContent('<title>Hello world</title><div>Get Started</div>'); | 
					
						
							|  |  |  |   await page.click('text=Get Started'); | 
					
						
							|  |  |  |   await expect(page).toHaveTitle('Hello world'); | 
					
						
							|  |  |  |   await browser.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(hub.output).toContain('Session request received by the distributor'); | 
					
						
							|  |  |  |   expect(node.output).toContain('Starting ChromeDriver'); | 
					
						
							|  |  |  |   await hub.waitForOutput('Deleted session'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  | test('selenium grid 4.0.0-rc-1 standalone chromium broken driver', async ({ browserName, childProcess, waitForPort, browserType }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const port = testInfo.workerIndex + 15123; | 
					
						
							|  |  |  |   const grid = childProcess({ | 
					
						
							|  |  |  |     command: ['java', `-Dwebdriver.chrome.driver=${brokenDriver}`, '-jar', selenium_4_0_0_rc1, 'standalone', '--config', writeSeleniumConfig(testInfo, port)], | 
					
						
							|  |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await waitForPort(port); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |   const __testHookSeleniumRemoteURL = `http://127.0.0.1:${port}/wd/hub`; | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const error = await browserType.launch({ __testHookSeleniumRemoteURL } as any).catch(e => e); | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |   expect(error.message).toContain(`Error connecting to Selenium at http://127.0.0.1:${port}/wd/hub/session: Could not start a new session`); | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   expect(grid.output).not.toContain('Starting ChromeDriver'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('selenium grid 3.141.59 standalone non-chromium', async ({ browserName, browserType }, testInfo) => { | 
					
						
							|  |  |  |   test.skip(browserName === 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |   const __testHookSeleniumRemoteURL = `http://127.0.0.1:4444/wd/hub`; | 
					
						
							| 
									
										
										
										
											2021-09-22 21:13:32 -07:00
										 |  |  |   const error = await browserType.launch({ __testHookSeleniumRemoteURL } as any).catch(e => e); | 
					
						
							|  |  |  |   expect(error.message).toContain('Connecting to SELENIUM_REMOTE_URL is only supported by Chromium'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-09-29 14:54:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-09 14:41:13 -08:00
										 |  |  | test('selenium grid 3.141.59 standalone chromium through run-driver', async ({ browserName, childProcess, waitForPort }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2021-09-29 14:54:24 -07:00
										 |  |  |   test.skip(browserName !== 'chromium'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const port = testInfo.workerIndex + 15123; | 
					
						
							|  |  |  |   const grid = childProcess({ | 
					
						
							|  |  |  |     command: ['java', `-Dwebdriver.chrome.driver=${chromeDriver}`, '-jar', standalone_3_141_59, '-config', writeSeleniumConfig(testInfo, port)], | 
					
						
							|  |  |  |     cwd: __dirname, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await waitForPort(port); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 07:28:53 -08:00
										 |  |  |   const { playwright: pw, stop } = await start({ | 
					
						
							| 
									
										
										
										
											2022-04-22 13:42:52 +02:00
										 |  |  |     SELENIUM_REMOTE_URL: `http://127.0.0.1:${port}/wd/hub`, | 
					
						
							| 
									
										
										
										
											2021-09-29 14:54:24 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-10-27 18:00:06 -08:00
										 |  |  |   const browser = await pw.chromium.launch(); | 
					
						
							| 
									
										
										
										
											2021-09-29 14:54:24 -07:00
										 |  |  |   const page = await browser.newPage(); | 
					
						
							|  |  |  |   await page.setContent('<title>Hello world</title><div>Get Started</div>'); | 
					
						
							|  |  |  |   await page.click('text=Get Started'); | 
					
						
							|  |  |  |   await expect(page).toHaveTitle('Hello world'); | 
					
						
							|  |  |  |   // Note: it is important to stop the driver without explicitly closing the browser.
 | 
					
						
							|  |  |  |   // It should terminate selenium session in this case.
 | 
					
						
							| 
									
										
										
										
											2021-10-27 07:28:53 -08:00
										 |  |  |   await stop(); | 
					
						
							| 
									
										
										
										
											2021-09-29 14:54:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   expect(grid.output).toContain('Starting ChromeDriver'); | 
					
						
							|  |  |  |   expect(grid.output).toContain('Started new session'); | 
					
						
							|  |  |  |   // It is important that selenium session is terminated.
 | 
					
						
							|  |  |  |   await grid.waitForOutput('Removing session'); | 
					
						
							|  |  |  | }); |