| 
									
										
										
										
											2023-05-10 08:53:01 +08: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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-19 02:28:28 +08:00
										 |  |  | import { test, expect, retries } from './ui-mode-fixtures'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test.describe.configure({ mode: 'parallel', retries }); | 
					
						
							| 
									
										
										
										
											2023-05-10 08:53:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  | test('should contain text attachment', async ({ runUITest }) => { | 
					
						
							| 
									
										
										
										
											2023-05-10 08:53:01 +08:00
										 |  |  |   const { page } = await runUITest({ | 
					
						
							|  |  |  |     'a.test.ts': `
 | 
					
						
							|  |  |  |       import { test } from '@playwright/test'; | 
					
						
							|  |  |  |       test('attach test', async () => { | 
					
						
							|  |  |  |         await test.info().attach('note', { path: __filename }); | 
					
						
							| 
									
										
										
										
											2024-03-19 04:26:45 +08:00
										 |  |  |         await test.info().attach('🎭', { body: 'hi tester!', contentType: 'text/plain' }); | 
					
						
							| 
									
										
										
										
											2023-05-10 08:53:01 +08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.getByText('attach test').click(); | 
					
						
							|  |  |  |   await page.getByTitle('Run all').click(); | 
					
						
							|  |  |  |   await expect(page.getByTestId('status-line')).toHaveText('1/1 passed (100%)'); | 
					
						
							|  |  |  |   await page.getByText('Attachments').click(); | 
					
						
							| 
									
										
										
										
											2024-03-19 04:26:45 +08:00
										 |  |  |   for (const { name, content } of [ | 
					
						
							|  |  |  |     { name: 'note', content: 'attach test' }, | 
					
						
							|  |  |  |     { name: '🎭', content: 'hi tester!' } | 
					
						
							|  |  |  |   ]) { | 
					
						
							|  |  |  |     await page.getByText(`attach "${name}"`, { exact: true }).click(); | 
					
						
							|  |  |  |     const downloadPromise = page.waitForEvent('download'); | 
					
						
							|  |  |  |     await page.getByRole('link', { name: name }).click(); | 
					
						
							|  |  |  |     const download = await downloadPromise; | 
					
						
							|  |  |  |     expect(download.suggestedFilename()).toBe(name); | 
					
						
							|  |  |  |     expect((await readAllFromStream(await download.createReadStream())).toString()).toContain(content); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-05-10 08:53:01 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-05-12 07:32:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  | test('should contain binary attachment', async ({ runUITest }) => { | 
					
						
							| 
									
										
										
										
											2023-05-12 07:32:32 +08:00
										 |  |  |   const { page } = await runUITest({ | 
					
						
							|  |  |  |     'a.test.ts': `
 | 
					
						
							|  |  |  |       import { test } from '@playwright/test'; | 
					
						
							|  |  |  |       test('attach test', async () => { | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |         await test.info().attach('data', { body: Buffer.from([1, 2, 3]), contentType: 'application/octet-stream' }); | 
					
						
							| 
									
										
										
										
											2023-05-12 07:32:32 +08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.getByText('attach test').click(); | 
					
						
							|  |  |  |   await page.getByTitle('Run all').click(); | 
					
						
							|  |  |  |   await expect(page.getByTestId('status-line')).toHaveText('1/1 passed (100%)'); | 
					
						
							|  |  |  |   await page.getByText('Attachments').click(); | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |   await page.getByText('attach "data"', { exact: true }).click(); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  |   const downloadPromise = page.waitForEvent('download'); | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |   await page.getByRole('link', { name: 'data' }).click(); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  |   const download = await downloadPromise; | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |   expect(download.suggestedFilename()).toBe('data'); | 
					
						
							|  |  |  |   expect(await readAllFromStream(await download.createReadStream())).toEqual(Buffer.from([1, 2, 3])); | 
					
						
							| 
									
										
										
										
											2023-05-12 07:32:32 +08:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  | test('should contain string attachment', async ({ runUITest }) => { | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  |   const { page } = await runUITest({ | 
					
						
							|  |  |  |     'a.test.ts': `
 | 
					
						
							|  |  |  |       import { test } from '@playwright/test'; | 
					
						
							|  |  |  |       test('attach test', async () => { | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |         await test.info().attach('note', { body: 'text42' }); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  |       }); | 
					
						
							|  |  |  |     `,
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.getByText('attach test').click(); | 
					
						
							|  |  |  |   await page.getByTitle('Run all').click(); | 
					
						
							|  |  |  |   await expect(page.getByTestId('status-line')).toHaveText('1/1 passed (100%)'); | 
					
						
							|  |  |  |   await page.getByText('Attachments').click(); | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |   await page.getByText('attach "note"', { exact: true }).click(); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  |   const downloadPromise = page.waitForEvent('download'); | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |   await page.getByRole('link', { name: 'note' }).click(); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  |   const download = await downloadPromise; | 
					
						
							| 
									
										
										
										
											2023-08-21 05:47:18 +08:00
										 |  |  |   expect(download.suggestedFilename()).toBe('note'); | 
					
						
							|  |  |  |   expect((await readAllFromStream(await download.createReadStream())).toString()).toEqual('text42'); | 
					
						
							| 
									
										
										
										
											2023-08-17 16:57:28 +08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function readAllFromStream(stream: NodeJS.ReadableStream): Promise<Buffer> { | 
					
						
							|  |  |  |   return new Promise(resolve => { | 
					
						
							|  |  |  |     const chunks: Buffer[] = []; | 
					
						
							|  |  |  |     stream.on('data', chunk => chunks.push(chunk)); | 
					
						
							|  |  |  |     stream.on('end', () => resolve(Buffer.concat(chunks))); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |