| 
									
										
										
										
											2022-04-13 15:13:31 -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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { test, expect } from './playwright-test-fixtures'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 13:25:56 -08:00
										 |  |  | test('should work with connectOptions', async ({ runInlineTest }) => { | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.js': `
 | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './global-setup', | 
					
						
							| 
									
										
										
										
											2023-07-12 16:40:55 +02:00
										 |  |  |         // outputDir is relative to the config file. Customers can have special characters in the path:
 | 
					
						
							|  |  |  |         // See: https://github.com/microsoft/playwright/issues/24157
 | 
					
						
							|  |  |  |         outputDir: 'Привет', | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |         use: { | 
					
						
							|  |  |  |           connectOptions: { | 
					
						
							|  |  |  |             wsEndpoint: process.env.CONNECT_WS_ENDPOINT, | 
					
						
							|  |  |  |           }, | 
					
						
							| 
									
										
										
										
											2023-07-12 16:40:55 +02:00
										 |  |  |           launchOptions: { | 
					
						
							|  |  |  |             env: { | 
					
						
							|  |  |  |               // Customers can have special characters: https://github.com/microsoft/playwright/issues/24157
 | 
					
						
							|  |  |  |               RANDOM_TEST_SPECIAL: 'Привет', | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'global-setup.ts': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { chromium } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |       module.exports = async () => { | 
					
						
							| 
									
										
										
										
											2023-03-31 08:57:07 -07:00
										 |  |  |         process.env.DEBUG = 'pw:browser'; | 
					
						
							|  |  |  |         process.env.PWTEST_SERVER_WS_HEADERS = | 
					
						
							|  |  |  |           'x-playwright-debug-log: a-debug-log-string\\r\\n' + | 
					
						
							|  |  |  |           'x-playwright-attachment: attachment-a=value-a\\r\\n' + | 
					
						
							|  |  |  |           'x-playwright-debug-log: b-debug-log-string\\r\\n' + | 
					
						
							|  |  |  |           'x-playwright-attachment: attachment-b=value-b'; | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |         const server = await chromium.launchServer(); | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |         process.env.CONNECT_WS_ENDPOINT = server.wsEndpoint(); | 
					
						
							|  |  |  |         return () => server.close(); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.ts': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |       test.use({ locale: 'fr-CH' }); | 
					
						
							|  |  |  |       test('pass', async ({ page }) => { | 
					
						
							|  |  |  |         await page.setContent('<div>PASS</div>'); | 
					
						
							|  |  |  |         await expect(page.locator('div')).toHaveText('PASS'); | 
					
						
							|  |  |  |         expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(0); | 
					
						
							|  |  |  |   expect(result.passed).toBe(1); | 
					
						
							| 
									
										
										
										
											2023-03-31 08:57:07 -07:00
										 |  |  |   expect(result.output).toContain('a-debug-log-string'); | 
					
						
							|  |  |  |   expect(result.output).toContain('b-debug-log-string'); | 
					
						
							|  |  |  |   expect(result.results[0].attachments).toEqual([ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       name: 'attachment-a', | 
					
						
							|  |  |  |       contentType: 'text/plain', | 
					
						
							|  |  |  |       body: 'dmFsdWUtYQ==' | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       name: 'attachment-b', | 
					
						
							|  |  |  |       contentType: 'text/plain', | 
					
						
							|  |  |  |       body: 'dmFsdWUtYg==' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('should throw with bad connectOptions', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.js': `
 | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         use: { | 
					
						
							|  |  |  |           connectOptions: { | 
					
						
							|  |  |  |             wsEndpoint: 'http://does-not-exist-bad-domain.oh-no-should-not-work', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.ts': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |       test('pass', async ({ page }) => { | 
					
						
							|  |  |  |         await page.setContent('<div>PASS</div>'); | 
					
						
							|  |  |  |         await expect(page.locator('div')).toHaveText('PASS'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(1); | 
					
						
							|  |  |  |   expect(result.passed).toBe(0); | 
					
						
							| 
									
										
										
										
											2023-08-31 15:29:04 -07:00
										 |  |  |   expect(result.output).toContain('browserType.connect:'); | 
					
						
							| 
									
										
										
										
											2022-09-07 15:57:20 -07:00
										 |  |  |   expect(result.output).toContain('does-not-exist-bad-domain'); | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('should respect connectOptions.timeout', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.js': `
 | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         use: { | 
					
						
							|  |  |  |           connectOptions: { | 
					
						
							| 
									
										
										
										
											2023-10-04 22:56:42 -04:00
										 |  |  |             wsEndpoint: 'wss://localhost:5678', | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |             timeout: 1, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.ts': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  |       test('pass', async ({ page }) => { | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(1); | 
					
						
							|  |  |  |   expect(result.passed).toBe(0); | 
					
						
							| 
									
										
										
										
											2023-08-31 15:29:04 -07:00
										 |  |  |   expect(result.output).toContain('browserType.connect: Timeout 1ms exceeded.'); | 
					
						
							| 
									
										
										
										
											2022-04-13 15:13:31 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-05-16 16:46:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('should print debug log when failed to connect', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.js': `
 | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './global-setup', | 
					
						
							|  |  |  |         use: { | 
					
						
							|  |  |  |           connectOptions: { | 
					
						
							|  |  |  |             wsEndpoint: process.env.CONNECT_WS_ENDPOINT, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'global-setup.ts': `
 | 
					
						
							|  |  |  |       import { chromium } from '@playwright/test'; | 
					
						
							|  |  |  |       import ws from 'ws'; | 
					
						
							|  |  |  |       import http from 'http'; | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         const server = http.createServer(() => {}); | 
					
						
							|  |  |  |         server.on('upgrade', async (request, socket, head) => { | 
					
						
							|  |  |  |           socket.write('HTTP/1.1 401 Unauthorized\\r\\nx-playwright-debug-log: b-debug-log-string\\r\\n\\r\\nUnauthorized body'); | 
					
						
							|  |  |  |           socket.destroy(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         server.listen(0); | 
					
						
							|  |  |  |         await new Promise(f => server.once('listening', f)); | 
					
						
							|  |  |  |         process.env.CONNECT_WS_ENDPOINT = 'ws://localhost:' + server.address().port; | 
					
						
							|  |  |  |         return () => new Promise(f => server.close(f)); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.ts': `
 | 
					
						
							|  |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							|  |  |  |       test('fail', async ({ page }) => { | 
					
						
							|  |  |  |         await page.setContent('<div>FAIL</div>'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(1); | 
					
						
							|  |  |  |   expect(result.failed).toBe(1); | 
					
						
							|  |  |  |   expect(result.output).toContain('b-debug-log-string'); | 
					
						
							|  |  |  |   expect(result.results[0].attachments).toEqual([]); | 
					
						
							|  |  |  | }); |