| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright Microsoft Corporation. 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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  | import { test, expect } from './playwright-test-fixtures'; | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalSetup and globalTeardown should work', async ({ runInlineTest }) => { | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'dir/playwright.config.ts': `
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |         testDir: '..', | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |         globalSetup: './globalSetup', | 
					
						
							|  |  |  |         globalTeardown: path.join(__dirname, 'globalTeardown.ts'), | 
					
						
							| 
									
										
										
										
											2023-01-31 15:59:13 -08:00
										 |  |  |         projects: [{ name: 'p1' }] | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |     'dir/globalSetup.ts': `
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       module.exports = async () => { | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |         console.log('\\n%%from-global-setup'); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |     'dir/globalTeardown.ts': `
 | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         console.log('\\n%%from-global-teardown'); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('should work', async ({}, testInfo) => { | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |         console.log('\\n%%from-test'); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2023-01-31 15:59:13 -08:00
										 |  |  |   }, { 'config': 'dir' }); | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |   expect(result.passed).toBe(1); | 
					
						
							|  |  |  |   expect(result.failed).toBe(0); | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  |   expect(result.outputLines).toEqual([ | 
					
						
							|  |  |  |     'from-global-setup', | 
					
						
							|  |  |  |     'from-test', | 
					
						
							|  |  |  |     'from-global-teardown', | 
					
						
							| 
									
										
										
										
											2022-08-01 09:01:23 -07:00
										 |  |  |   ]); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 12:41:35 -07:00
										 |  |  | test('standalone globalTeardown should work', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const { results, output } = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalTeardown: './globalTeardown.ts', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalTeardown.ts': `
 | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         console.log('got my teardown'); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-07-20 12:41:35 -07:00
										 |  |  |       test('should work', async ({}, testInfo) => { | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(results[0].status).toBe('passed'); | 
					
						
							|  |  |  |   expect(output).toContain('got my teardown'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalTeardown runs after failures', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const { results, output } = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: 'globalSetup.ts', | 
					
						
							|  |  |  |         globalTeardown: './globalTeardown.ts', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalSetup.ts': `
 | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         await new Promise(f => setTimeout(f, 100)); | 
					
						
							|  |  |  |         global.value = 42; | 
					
						
							|  |  |  |         process.env.FOO = String(global.value); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalTeardown.ts': `
 | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         console.log('teardown=' + global.value); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('should work', async ({}, testInfo) => { | 
					
						
							|  |  |  |         expect(process.env.FOO).toBe('43'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(results[0].status).toBe('failed'); | 
					
						
							|  |  |  |   expect(output).toContain('teardown=42'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-05 10:36:29 +01:00
										 |  |  | test('globalTeardown still runs when globalSetup times out', async ({ runInlineTest }) => { | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './globalSetup.ts', | 
					
						
							|  |  |  |         globalTeardown: 'globalTeardown.ts', | 
					
						
							|  |  |  |         globalTimeout: 1000, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalSetup.ts': `
 | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         await new Promise(f => setTimeout(f, 10000)); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalTeardown.ts': `
 | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         console.log('teardown='); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('should not run', async ({}, testInfo) => { | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-01-23 17:44:23 -08:00
										 |  |  |   expect(result.output).toContain('Timed out waiting 1s for the global setup to run'); | 
					
						
							| 
									
										
										
										
											2024-11-05 10:36:29 +01:00
										 |  |  |   expect(result.output).toContain('teardown='); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalSetup should work with sync function', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const { passed } = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './globalSetup.ts', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalSetup.ts': `
 | 
					
						
							|  |  |  |       module.exports = () => { | 
					
						
							|  |  |  |         process.env.FOO = JSON.stringify({ foo: 'bar' }); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('should work', async ({}) => { | 
					
						
							|  |  |  |         const value = JSON.parse(process.env.FOO); | 
					
						
							|  |  |  |         expect(value).toEqual({ foo: 'bar' }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(passed).toBe(1); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-06-06 17:09:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalSetup error should prevent tests from executing', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const { passed, output } = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './globalSetup.ts', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalSetup.ts': `
 | 
					
						
							|  |  |  |       module.exports = () => { | 
					
						
							|  |  |  |         throw new Error('failure in global setup!'); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('a', async ({}) => { | 
					
						
							|  |  |  |         console.log('this test ran'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2022-04-01 12:35:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('b', async ({}) => { | 
					
						
							|  |  |  |         console.log('this test ran'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }, { reporter: 'line' }); | 
					
						
							| 
									
										
										
										
											2022-04-01 12:35:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  |   expect(output).not.toContain('this test ran'); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |   expect(passed).toBe(0); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-04-01 12:35:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalSetup should throw when passed non-function', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const { output } = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './globalSetup.ts', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalSetup.ts': `
 | 
					
						
							|  |  |  |       module.exports = 42; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('should work', async ({}) => { | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-05-05 09:14:00 -08:00
										 |  |  |   expect(output).toContain(`globalSetup.ts: file must export a single function.`); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-07-20 15:13:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalSetup should work with default export and run the returned fn', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const { output, exitCode, passed } = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './globalSetup.ts', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalSetup.ts': `
 | 
					
						
							|  |  |  |       function setup() { | 
					
						
							|  |  |  |         let x = 42; | 
					
						
							|  |  |  |         console.log('\\n%%setup: ' + x); | 
					
						
							|  |  |  |         return async () => { | 
					
						
							|  |  |  |           await x; | 
					
						
							|  |  |  |           console.log('\\n%%teardown: ' + x); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       export default setup; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('should work', async ({}) => { | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(passed).toBe(1); | 
					
						
							|  |  |  |   expect(exitCode).toBe(0); | 
					
						
							|  |  |  |   expect(output).toContain(`%%setup: 42`); | 
					
						
							|  |  |  |   expect(output).toContain(`%%teardown: 42`); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-09-01 15:35:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalSetup should allow requiring a package from node_modules', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const { results } = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       import * as path from 'path'; | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: 'my-global-setup' | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'node_modules/my-global-setup/index.js': `
 | 
					
						
							|  |  |  |       module.exports = async () => { | 
					
						
							|  |  |  |         await new Promise(f => setTimeout(f, 100)); | 
					
						
							|  |  |  |         global.value = 42; | 
					
						
							|  |  |  |         process.env.FOO = String(global.value); | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       test('should work', async ({}, testInfo) => { | 
					
						
							|  |  |  |         expect(process.env.FOO).toBe('42'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(results[0].status).toBe('passed'); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-09-13 17:50:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | const authFiles = { | 
					
						
							|  |  |  |   'playwright.config.ts': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |     import { PlaywrightTestConfig } from '@playwright/test'; | 
					
						
							|  |  |  |     const config: PlaywrightTestConfig = { | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       globalSetup: require.resolve('./auth'), | 
					
						
							|  |  |  |       use: { | 
					
						
							|  |  |  |         baseURL: 'https://www.example.com', | 
					
						
							|  |  |  |         storageState: 'state.json', | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2022-02-07 10:41:56 -08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |     export default config; | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   'auth.ts': `
 | 
					
						
							| 
									
										
										
										
											2024-04-24 14:14:05 -07:00
										 |  |  |     import { chromium, FullConfig } from '@playwright/test'; | 
					
						
							|  |  |  |     async function globalSetup(config: FullConfig) { | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       const { baseURL, storageState } = config.projects[0].use; | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |       const browser = await chromium.launch(); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |       const page = await browser.newPage(); | 
					
						
							|  |  |  |       await page.route('**/*', route => { | 
					
						
							|  |  |  |         route.fulfill({ body: '<html></html>' }).catch(() => {}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       await page.goto(baseURL!); | 
					
						
							|  |  |  |       await page.evaluate(() => { | 
					
						
							|  |  |  |         localStorage['name'] = 'value'; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       await page.context().storageState({ path: storageState as string }); | 
					
						
							|  |  |  |       await browser.close(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     export default globalSetup; | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  |   'a.test.ts': `
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |     import { test, expect } from '@playwright/test'; | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |     test('should have storage state', async ({ page }) => { | 
					
						
							|  |  |  |       await page.route('**/*', route => { | 
					
						
							|  |  |  |         route.fulfill({ body: '<html></html>' }).catch(() => {}); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       await page.goto('/'); | 
					
						
							|  |  |  |       const value = await page.evaluate(() => localStorage['name']); | 
					
						
							|  |  |  |       expect(value).toBe('value'); | 
					
						
							| 
									
										
										
										
											2022-02-07 10:41:56 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  |   `,
 | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-09-13 17:50:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:01:37 -08:00
										 |  |  | test('globalSetup should work for auth', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest(authFiles); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(0); | 
					
						
							|  |  |  |   expect(result.passed).toBe(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('globalSetup auth should compile', async ({ runTSC }) => { | 
					
						
							|  |  |  |   const result = await runTSC(authFiles); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(0); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('teardown order', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							| 
									
										
										
										
											2024-02-13 09:34:03 -08:00
										 |  |  |       const plugins = []; | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |       for (let i = 1; i < 4; ++i) { | 
					
						
							| 
									
										
										
										
											2024-02-13 09:34:03 -08:00
										 |  |  |         plugins.push(() => ({ | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |           setup: () => console.log('\\n%%setup ' + i), | 
					
						
							|  |  |  |           teardown: () => console.log('\\n%%teardown ' + i), | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-02-13 09:34:03 -08:00
										 |  |  |       export default { '@playwright/test': { plugins } }; | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |     'a.test.ts': `
 | 
					
						
							|  |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							|  |  |  |       test('test', () => {}); | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(0); | 
					
						
							|  |  |  |   expect(result.passed).toBe(1); | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  |   expect(result.outputLines).toEqual([ | 
					
						
							|  |  |  |     'setup 1', | 
					
						
							|  |  |  |     'setup 2', | 
					
						
							|  |  |  |     'setup 3', | 
					
						
							|  |  |  |     'teardown 3', | 
					
						
							|  |  |  |     'teardown 2', | 
					
						
							|  |  |  |     'teardown 1', | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |   ]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('teardown after error', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							| 
									
										
										
										
											2024-02-13 09:34:03 -08:00
										 |  |  |       const plugins = []; | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |       for (let i = 1; i < 4; ++i) { | 
					
						
							| 
									
										
										
										
											2024-02-13 09:34:03 -08:00
										 |  |  |         plugins.push(() => ({ | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |           setup: () => console.log('\\n%%setup ' + i), | 
					
						
							|  |  |  |           teardown: () => { | 
					
						
							|  |  |  |             console.log('\\n%%teardown ' + i); | 
					
						
							|  |  |  |             throw new Error('failed teardown ' + i) | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         })); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2024-02-13 09:34:03 -08:00
										 |  |  |       export default { '@playwright/test': { plugins } }; | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |     'a.test.ts': `
 | 
					
						
							|  |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							|  |  |  |       test('test', () => {}); | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-02-08 12:44:51 -08:00
										 |  |  |   expect(result.exitCode).toBe(1); | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |   expect(result.passed).toBe(1); | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  |   const output = result.output; | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |   expect(output).toContain('Error: failed teardown 1'); | 
					
						
							|  |  |  |   expect(output).toContain('Error: failed teardown 2'); | 
					
						
							|  |  |  |   expect(output).toContain('Error: failed teardown 3'); | 
					
						
							|  |  |  |   expect(output).toContain('throw new Error(\'failed teardown'); | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  |   expect(result.outputLines).toEqual([ | 
					
						
							|  |  |  |     'setup 1', | 
					
						
							|  |  |  |     'setup 2', | 
					
						
							|  |  |  |     'setup 3', | 
					
						
							|  |  |  |     'teardown 3', | 
					
						
							|  |  |  |     'teardown 2', | 
					
						
							|  |  |  |     'teardown 1', | 
					
						
							| 
									
										
										
										
											2023-01-25 15:38:23 -08:00
										 |  |  |   ]); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2024-10-18 11:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | test('globalSetup should support multiple', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: ['./globalSetup1.ts','./globalSetup2.ts','./globalSetup3.ts','./globalSetup4.ts'], | 
					
						
							|  |  |  |         globalTeardown: ['./globalTeardown1.ts', './globalTeardown2.ts'], | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2024-11-05 10:36:29 +01:00
										 |  |  |     'globalSetup1.ts': `module.exports = () => { console.log('%%globalSetup1'); return () => { console.log('%%callback1'); throw new Error('kaboom'); } };`, | 
					
						
							| 
									
										
										
										
											2024-10-18 11:03:00 +02:00
										 |  |  |     'globalSetup2.ts': `module.exports = () => console.log('%%globalSetup2');`, | 
					
						
							| 
									
										
										
										
											2024-11-05 10:36:29 +01:00
										 |  |  |     'globalSetup3.ts': `module.exports = () => { console.log('%%globalSetup3'); return () => console.log('%%callback3'); }`, | 
					
						
							| 
									
										
										
										
											2024-10-18 11:03:00 +02:00
										 |  |  |     'globalSetup4.ts': `module.exports = () => console.log('%%globalSetup4');`, | 
					
						
							|  |  |  |     'globalTeardown1.ts': `module.exports = () => console.log('%%globalTeardown1')`, | 
					
						
							|  |  |  |     'globalTeardown2.ts': `module.exports = () => { console.log('%%globalTeardown2'); throw new Error('kaboom'); }`, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							|  |  |  |       import { test } from '@playwright/test'; | 
					
						
							|  |  |  |       test('a', () => console.log('%%test a')); | 
					
						
							|  |  |  |       test('b', () => console.log('%%test b')); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }, { reporter: 'line' }); | 
					
						
							|  |  |  |   expect(result.passed).toBe(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-05 10:36:29 +01:00
										 |  |  |   // first setups, then setup callbacks in reverse order.
 | 
					
						
							|  |  |  |   // then teardowns in declared order.
 | 
					
						
							| 
									
										
										
										
											2024-10-18 11:03:00 +02:00
										 |  |  |   expect(result.outputLines).toEqual([ | 
					
						
							|  |  |  |     'globalSetup1', | 
					
						
							|  |  |  |     'globalSetup2', | 
					
						
							|  |  |  |     'globalSetup3', | 
					
						
							|  |  |  |     'globalSetup4', | 
					
						
							|  |  |  |     'test a', | 
					
						
							|  |  |  |     'test b', | 
					
						
							| 
									
										
										
										
											2024-11-05 10:36:29 +01:00
										 |  |  |     'callback3', | 
					
						
							|  |  |  |     'callback1', | 
					
						
							|  |  |  |     'globalTeardown1', | 
					
						
							| 
									
										
										
										
											2024-10-18 11:03:00 +02:00
										 |  |  |     'globalTeardown2', | 
					
						
							| 
									
										
										
										
											2024-11-05 10:36:29 +01:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(result.output).toContain('Error: kaboom'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('globalTeardown runs even if callback failed', async ({ runInlineTest }) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.ts': `
 | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         globalSetup: './globalSetup.ts', | 
					
						
							|  |  |  |         globalTeardown: './globalTeardown.ts', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |     'globalSetup.ts': `module.exports = () => { console.log('%%globalSetup'); return () => { throw new Error('kaboom'); } };`, | 
					
						
							|  |  |  |     'globalTeardown.ts': `module.exports = () => console.log('%%globalTeardown')`, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     'a.test.js': `
 | 
					
						
							|  |  |  |       import { test } from '@playwright/test'; | 
					
						
							|  |  |  |       test('a', () => console.log('%%test')); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }, { reporter: 'line' }); | 
					
						
							|  |  |  |   expect(result.passed).toBe(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // first setups, then setup callbacks in reverse order.
 | 
					
						
							|  |  |  |   // then teardowns in declared order.
 | 
					
						
							|  |  |  |   expect(result.outputLines).toEqual([ | 
					
						
							|  |  |  |     'globalSetup', | 
					
						
							|  |  |  |     'test', | 
					
						
							|  |  |  |     'globalTeardown', | 
					
						
							| 
									
										
										
										
											2024-10-18 11:03:00 +02:00
										 |  |  |   ]); | 
					
						
							|  |  |  |   expect(result.output).toContain('Error: kaboom'); | 
					
						
							|  |  |  | }); |