| 
									
										
										
										
											2022-11-09 15:29:07 -08: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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import path from 'path'; | 
					
						
							|  |  |  | import fs from 'fs'; | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  | import { test, expect } from './playwright-test-fixtures'; | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | async function getSnapshotPaths(runInlineTest, testInfo, playwrightConfig, pathArgs) { | 
					
						
							|  |  |  |   const SEPARATOR = '==== 8< ---- '; | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.js': `
 | 
					
						
							|  |  |  |       module.exports = ${JSON.stringify(playwrightConfig, null, 2)} | 
					
						
							|  |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |     'a/b/c/d.spec.ts': `
 | 
					
						
							|  |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							|  |  |  |       test.describe('suite', () => { | 
					
						
							|  |  |  |         test('test should work', async ({ page }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2022-11-10 17:23:57 -08:00
										 |  |  |           console.log([ | 
					
						
							|  |  |  |             ${JSON.stringify(SEPARATOR)}, | 
					
						
							|  |  |  |             testInfo.project.name, | 
					
						
							|  |  |  |             ${JSON.stringify(SEPARATOR)}, | 
					
						
							|  |  |  |             testInfo.snapshotPath(...${JSON.stringify(pathArgs)}), | 
					
						
							|  |  |  |             ${JSON.stringify(SEPARATOR)}, | 
					
						
							|  |  |  |           ].join('')); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     `
 | 
					
						
							|  |  |  |   }, { workers: 1 }); | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(0); | 
					
						
							| 
									
										
										
										
											2023-02-07 15:11:44 -08:00
										 |  |  |   const allSegments = result.output.split(SEPARATOR); | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  |   const projToSnapshot = {}; | 
					
						
							|  |  |  |   for (let i = 1; i < allSegments.length; i += 3) | 
					
						
							|  |  |  |     projToSnapshot[allSegments[i]] = path.relative(testInfo.outputDir, allSegments[i + 1]); | 
					
						
							|  |  |  |   return projToSnapshot; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('tokens should expand property', async ({ runInlineTest }, testInfo) => { | 
					
						
							| 
									
										
										
										
											2022-11-10 15:50:13 -08:00
										 |  |  |   test.slow(); | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  |   const snapshotPath = await getSnapshotPaths(runInlineTest, testInfo, { | 
					
						
							|  |  |  |     projects: [{ | 
					
						
							|  |  |  |       name: 'proj1', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{projectName}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'proj 2', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{-projectName}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'proj3', | 
					
						
							|  |  |  |       snapshotPathTemplate: 'foo{/projectName}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       snapshotPathTemplate: '{/projectName}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'platform', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{platform}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'extension', | 
					
						
							|  |  |  |       snapshotPathTemplate: 'mysnapshot{ext}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'arg', | 
					
						
							|  |  |  |       snapshotPathTemplate: 'bar/{arg}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'testFileDir', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{testFileDir}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'testFilePath', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{testFilePath}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'testFileName', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{testFileName}', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'snapshotDir', | 
					
						
							|  |  |  |       snapshotDir: './a-snapshot-dir', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{snapshotDir}.png', | 
					
						
							|  |  |  |     }, { | 
					
						
							|  |  |  |       name: 'snapshotSuffix', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{-snapshotSuffix}', | 
					
						
							| 
									
										
										
										
											2022-11-10 17:23:57 -08:00
										 |  |  |     }, { | 
					
						
							|  |  |  |       name: 'testName', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{testName}', | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  |     }], | 
					
						
							|  |  |  |   }, ['foo.png']); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['proj1']).toBe('proj1'); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['proj 2']).toBe('-proj-2'); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['proj3']).toBe(path.join('foo', 'proj3')); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['']).toBe(''); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['platform']).toBe(process.platform); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['extension']).toBe('mysnapshot.png'); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['arg']).toBe(path.join('bar', 'foo')); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['testFileDir']).toBe(path.join('a', 'b', 'c')); | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |   expect.soft(snapshotPath['testFilePath']).toBe(path.join('a', 'b', 'c', 'd.spec.ts')); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['testFileName']).toBe('d.spec.ts'); | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  |   expect.soft(snapshotPath['snapshotDir']).toBe('a-snapshot-dir.png'); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['snapshotSuffix']).toBe('-' + process.platform); | 
					
						
							| 
									
										
										
										
											2022-11-10 17:23:57 -08:00
										 |  |  |   expect.soft(snapshotPath['testName']).toBe('suite-test-should-work'); | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('args array should work', async ({ runInlineTest }, testInfo) => { | 
					
						
							|  |  |  |   const snapshotPath = await getSnapshotPaths(runInlineTest, testInfo, { | 
					
						
							|  |  |  |     projects: [{ | 
					
						
							|  |  |  |       name: 'proj', | 
					
						
							|  |  |  |       snapshotPathTemplate: '{ext}{arg}', | 
					
						
							|  |  |  |     }], | 
					
						
							|  |  |  |   }, ['foo', 'bar', 'baz.jpeg']); | 
					
						
							|  |  |  |   expect.soft(snapshotPath['proj']).toBe(path.join('.jpegfoo', 'bar', 'baz')); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test('arg should receive default arg', async ({ runInlineTest }, testInfo) => { | 
					
						
							|  |  |  |   const result = await runInlineTest({ | 
					
						
							|  |  |  |     'playwright.config.js': `
 | 
					
						
							|  |  |  |       module.exports = { | 
					
						
							|  |  |  |         snapshotPathTemplate: '__screenshots__/{arg}{ext}', | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     `,
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:20:56 -08:00
										 |  |  |     'a.spec.ts': `
 | 
					
						
							|  |  |  |       import { test, expect } from '@playwright/test'; | 
					
						
							|  |  |  |       test('is a test', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  |         await expect(page).toHaveScreenshot(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     `
 | 
					
						
							|  |  |  |   }, { 'update-snapshots': true }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   expect(result.exitCode).toBe(0); | 
					
						
							|  |  |  |   expect(result.passed).toBe(1); | 
					
						
							|  |  |  |   const snapshotOutputPath = testInfo.outputPath('__screenshots__/is-a-test-1.png'); | 
					
						
							| 
									
										
										
										
											2022-11-30 02:21:15 +05:30
										 |  |  |   expect(result.output).toContain(`A snapshot doesn't exist at ${snapshotOutputPath}, writing actual`); | 
					
						
							| 
									
										
										
										
											2022-11-09 15:29:07 -08:00
										 |  |  |   expect(fs.existsSync(snapshotOutputPath)).toBe(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 |