| 
									
										
										
										
											2021-03-01 12:20:04 -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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-25 15:05:50 -08:00
										 |  |  | import { contextTest, expect } from '../config/browserTest'; | 
					
						
							|  |  |  | import { InMemorySnapshotter } from '../../packages/playwright-core/lib/server/trace/test/inMemorySnapshotter'; | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 13:42:50 -08:00
										 |  |  | const it = contextTest.extend<{ snapshotter: InMemorySnapshotter }>({ | 
					
						
							| 
									
										
										
										
											2022-07-31 14:31:17 -07:00
										 |  |  |   snapshotter: async ({ toImpl, context }, run) => { | 
					
						
							| 
									
										
										
										
											2021-05-16 19:58:26 -07:00
										 |  |  |     const snapshotter = new InMemorySnapshotter(toImpl(context)); | 
					
						
							|  |  |  |     await snapshotter.initialize(); | 
					
						
							|  |  |  |     await run(snapshotter); | 
					
						
							|  |  |  |     await snapshotter.dispose(); | 
					
						
							| 
									
										
										
										
											2021-08-16 17:40:46 -07:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-04-01 13:18:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  | it.describe('snapshots', () => { | 
					
						
							|  |  |  |   it('should collect snapshot', async ({ page, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |     await page.setContent('<button>Hello</button>'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |     expect(distillSnapshot(snapshot)).toBe('<BUTTON>Hello</BUTTON>'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-04 21:11:35 -07:00
										 |  |  |   it('should preserve BASE and other content on reset', async ({ page, toImpl, snapshotter, server }) => { | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-08-04 21:11:35 -07:00
										 |  |  |     const html1 = snapshot1.render().html; | 
					
						
							|  |  |  |     expect(html1).toContain(`<BASE href="${server.EMPTY_PAGE}"`); | 
					
						
							|  |  |  |     await snapshotter.reset(); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2021-08-04 21:11:35 -07:00
										 |  |  |     const html2 = snapshot2.render().html; | 
					
						
							| 
									
										
										
										
											2023-03-23 12:49:53 -07:00
										 |  |  |     expect(html2.replace(/call@2/g, `call@1`)).toEqual(html1); | 
					
						
							| 
									
										
										
										
											2021-08-04 21:11:35 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  |   it('should capture resources', async ({ page, toImpl, server, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.route('**/style.css', route => { | 
					
						
							|  |  |  |       route.fulfill({ body: 'button { color: red; }', }).catch(() => {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.setContent('<link rel="stylesheet" href="style.css"><button>Hello</button>'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2023-07-10 20:04:48 -07:00
										 |  |  |     const resource = snapshot.resourceByUrl(`http://localhost:${server.PORT}/style.css`, 'GET'); | 
					
						
							| 
									
										
										
										
											2021-08-10 21:23:31 -07:00
										 |  |  |     expect(resource).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  |   it('should collect multiple', async ({ page, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |     await page.setContent('<button>Hello</button>'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							|  |  |  |     await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2023-03-22 09:32:21 -07:00
										 |  |  |     expect(snapshotter.snapshotCount()).toBe(2); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  |   it('should respect inline CSSOM change', async ({ page, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |     await page.setContent('<style>button { color: red; }</style><button>Hello</button>'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2022-04-05 15:10:12 -08:00
										 |  |  |     expect(distillSnapshot(snapshot1)).toBe('<STYLE>button { color: red; }</STYLE><BUTTON>Hello</BUTTON>'); | 
					
						
							| 
									
										
										
										
											2021-04-27 11:07:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     await page.evaluate(() => { (document.styleSheets[0].cssRules[0] as any).style.color = 'blue'; }); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2022-04-05 15:10:12 -08:00
										 |  |  |     expect(distillSnapshot(snapshot2)).toBe('<STYLE>button { color: blue; }</STYLE><BUTTON>Hello</BUTTON>'); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-25 14:34:17 -07:00
										 |  |  |   it('should respect CSSOM change through CSSGroupingRule', async ({ page, toImpl, snapshotter }) => { | 
					
						
							|  |  |  |     await page.setContent('<style>@media { button { color: red; } }</style><button>Hello</button>'); | 
					
						
							|  |  |  |     await page.evaluate(() => { | 
					
						
							|  |  |  |       window['rule'] = document.styleSheets[0].cssRules[0]; | 
					
						
							|  |  |  |       void 0; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							|  |  |  |     expect(distillSnapshot(snapshot1)).toBe('<STYLE>@media  {\n  button { color: red; }\n}</STYLE><BUTTON>Hello</BUTTON>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.evaluate(() => { window['rule'].cssRules[0].style.color = 'blue'; }); | 
					
						
							|  |  |  |     const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							|  |  |  |     expect(distillSnapshot(snapshot2)).toBe('<STYLE>@media  {\n  button { color: blue; }\n}</STYLE><BUTTON>Hello</BUTTON>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.evaluate(() => { window['rule'].insertRule('button { color: green; }', 1); }); | 
					
						
							|  |  |  |     const snapshot3 = await snapshotter.captureSnapshot(toImpl(page), 'call@3', 'snapshot@call@3'); | 
					
						
							|  |  |  |     expect(distillSnapshot(snapshot3)).toBe('<STYLE>@media  {\n  button { color: blue; }\n  button { color: green; }\n}</STYLE><BUTTON>Hello</BUTTON>'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-25 18:52:36 -07:00
										 |  |  |   it('should respect node removal', async ({ page, toImpl, snapshotter }) => { | 
					
						
							|  |  |  |     await page.setContent('<div><button id="button1"></button><button id="button2"></button></div>'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-06-25 18:52:36 -07:00
										 |  |  |     expect(distillSnapshot(snapshot1)).toBe('<DIV><BUTTON id=\"button1\"></BUTTON><BUTTON id=\"button2\"></BUTTON></DIV>'); | 
					
						
							|  |  |  |     await page.evaluate(() => document.getElementById('button2').remove()); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2021-06-25 18:52:36 -07:00
										 |  |  |     expect(distillSnapshot(snapshot2)).toBe('<DIV><BUTTON id=\"button1\"></BUTTON></DIV>'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should respect attr removal', async ({ page, toImpl, snapshotter }) => { | 
					
						
							|  |  |  |     await page.setContent('<div id="div" attr1="1" attr2="2"></div>'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-06-25 18:52:36 -07:00
										 |  |  |     expect(distillSnapshot(snapshot1)).toBe('<DIV id=\"div\" attr1=\"1\" attr2=\"2\"></DIV>'); | 
					
						
							|  |  |  |     await page.evaluate(() => document.getElementById('div').removeAttribute('attr2')); | 
					
						
							| 
									
										
										
										
											2023-03-22 09:32:21 -07:00
										 |  |  |     const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2021-06-25 18:52:36 -07:00
										 |  |  |     expect(distillSnapshot(snapshot2)).toBe('<DIV id=\"div\" attr1=\"1\"></DIV>'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  |   it('should have a custom doctype', async ({ page, server, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-06-17 09:41:29 -07:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.setContent('<!DOCTYPE foo><body>hi</body>'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-06-17 09:41:29 -07:00
										 |  |  |     expect(distillSnapshot(snapshot)).toBe('<!DOCTYPE foo>hi'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 07:54:00 +09:00
										 |  |  |   it('should replace meta charset attr that specifies charset', async ({ page, server, toImpl, snapshotter }) => { | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.setContent('<meta charset="shift-jis" />'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-12-13 07:54:00 +09:00
										 |  |  |     expect(distillSnapshot(snapshot)).toBe('<META charset="utf-8">'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should replace meta content attr that specifies charset', async ({ page, server, toImpl, snapshotter }) => { | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.setContent('<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-12-13 07:54:00 +09:00
										 |  |  |     expect(distillSnapshot(snapshot)).toBe('<META http-equiv="Content-Type" content="text/html; charset=utf-8">'); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  |   it('should respect subresource CSSOM change', async ({ page, server, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |     await page.route('**/style.css', route => { | 
					
						
							|  |  |  |       route.fulfill({ body: 'button { color: red; }', }).catch(() => {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.setContent('<link rel="stylesheet" href="style.css"><button>Hello</button>'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-04-27 11:07:07 -07:00
										 |  |  |     expect(distillSnapshot(snapshot1)).toBe('<LINK rel=\"stylesheet\" href=\"style.css\"><BUTTON>Hello</BUTTON>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.evaluate(() => { (document.styleSheets[0].cssRules[0] as any).style.color = 'blue'; }); | 
					
						
							| 
									
										
										
										
											2023-03-22 09:32:21 -07:00
										 |  |  |     const snapshot2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2023-07-10 20:04:48 -07:00
										 |  |  |     const resource = snapshot2.resourceByUrl(`http://localhost:${server.PORT}/style.css`, 'GET'); | 
					
						
							| 
									
										
										
										
											2021-10-12 13:42:50 -08:00
										 |  |  |     expect((await snapshotter.resourceContentForTest(resource.response.content._sha1)).toString()).toBe('button { color: blue; }'); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-03-08 19:49:57 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-27 22:34:39 +01:00
										 |  |  |   it('should capture frame', async ({ page, server, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-12-18 03:43:19 +09:00
										 |  |  |     await page.route('**/empty.html', route => { | 
					
						
							|  |  |  |       route.fulfill({ | 
					
						
							|  |  |  |         body: '<frameset><frame src="frame.html"></frameset>', | 
					
						
							|  |  |  |         contentType: 'text/html' | 
					
						
							|  |  |  |       }).catch(() => {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.route('**/frame.html', route => { | 
					
						
							|  |  |  |       route.fulfill({ | 
					
						
							|  |  |  |         body: '<html><button>Hello iframe</button></html>', | 
					
						
							|  |  |  |         contentType: 'text/html' | 
					
						
							|  |  |  |       }).catch(() => {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (let counter = 0; ; ++counter) { | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@' + counter, 'snapshot@call@' + counter); | 
					
						
							| 
									
										
										
										
											2021-12-18 03:43:19 +09:00
										 |  |  |       const text = distillSnapshot(snapshot).replace(/frame@[^"]+["]/, '<id>"'); | 
					
						
							|  |  |  |       if (text === '<FRAMESET><FRAME __playwright_src__=\"/snapshot/<id>\"></FRAME></FRAMESET>') | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |       await page.waitForTimeout(250); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-27 22:34:39 +01:00
										 |  |  |   it('should capture iframe', async ({ page, server, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-08 19:49:57 -08:00
										 |  |  |     await page.route('**/empty.html', route => { | 
					
						
							|  |  |  |       route.fulfill({ | 
					
						
							|  |  |  |         body: '<iframe src="iframe.html"></iframe>', | 
					
						
							|  |  |  |         contentType: 'text/html' | 
					
						
							|  |  |  |       }).catch(() => {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.route('**/iframe.html', route => { | 
					
						
							|  |  |  |       route.fulfill({ | 
					
						
							|  |  |  |         body: '<html><button>Hello iframe</button></html>', | 
					
						
							|  |  |  |         contentType: 'text/html' | 
					
						
							|  |  |  |       }).catch(() => {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Marking iframe hierarchy is racy, do not expect snapshot, wait for it.
 | 
					
						
							| 
									
										
										
										
											2021-12-18 03:43:19 +09:00
										 |  |  |     for (let counter = 0; ; ++counter) { | 
					
						
							| 
									
										
										
										
											2023-03-22 09:32:21 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@' + counter, 'snapshot@call@' + counter); | 
					
						
							| 
									
										
										
										
											2021-03-08 19:49:57 -08:00
										 |  |  |       const text = distillSnapshot(snapshot).replace(/frame@[^"]+["]/, '<id>"'); | 
					
						
							| 
									
										
										
										
											2021-11-23 11:36:18 -08:00
										 |  |  |       if (text === '<IFRAME __playwright_src__=\"/snapshot/<id>\"></IFRAME>') | 
					
						
							| 
									
										
										
										
											2023-02-27 22:34:39 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |       await page.waitForTimeout(250); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('should capture iframe with srcdoc', async ({ page, server, toImpl, snapshotter }) => { | 
					
						
							|  |  |  |     await page.route('**/empty.html', route => { | 
					
						
							|  |  |  |       route.fulfill({ | 
					
						
							|  |  |  |         body: '<iframe srcdoc="<html><button>Hello iframe</button></html>"></iframe>', | 
					
						
							|  |  |  |         contentType: 'text/html' | 
					
						
							|  |  |  |       }).catch(() => {}); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Marking iframe hierarchy is racy, do not expect snapshot, wait for it.
 | 
					
						
							|  |  |  |     for (let counter = 0; ; ++counter) { | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@' + counter, 'snapshot@call@' + counter); | 
					
						
							| 
									
										
										
										
											2023-02-27 22:34:39 +01:00
										 |  |  |       const text = distillSnapshot(snapshot).replace(/frame@[^"]+["]/, '<id>"'); | 
					
						
							|  |  |  |       if (text === '<IFRAME __playwright_src__=\"/snapshot/<id>\"></IFRAME>') | 
					
						
							| 
									
										
										
										
											2021-03-08 19:49:57 -08:00
										 |  |  |         break; | 
					
						
							|  |  |  |       await page.waitForTimeout(250); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  |   it('should capture snapshot target', async ({ page, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |     await page.setContent('<button>Hello</button><button>World</button>'); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const handle = await page.$('text=Hello'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1', toImpl(handle)); | 
					
						
							|  |  |  |       expect(distillSnapshot(snapshot, false /* distillTarget */)).toBe('<BUTTON __playwright_target__=\"call@1\">Hello</BUTTON><BUTTON>World</BUTTON>'); | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const handle = await page.$('text=World'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2', toImpl(handle)); | 
					
						
							|  |  |  |       expect(distillSnapshot(snapshot, false /* distillTarget */)).toBe('<BUTTON __playwright_target__=\"call@1\">Hello</BUTTON><BUTTON __playwright_target__=\"call@2\">World</BUTTON>'); | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 17:47:20 -07:00
										 |  |  |   it('should collect on attribute change', async ({ page, toImpl, snapshotter }) => { | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |     await page.setContent('<button>Hello</button>'); | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |       expect(distillSnapshot(snapshot)).toBe('<BUTTON>Hello</BUTTON>'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const handle = await page.$('text=Hello')!; | 
					
						
							|  |  |  |     await handle.evaluate(element => element.setAttribute('data', 'one')); | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |       expect(distillSnapshot(snapshot)).toBe('<BUTTON data="one">Hello</BUTTON>'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     await handle.evaluate(element => element.setAttribute('data', 'two')); | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-03-22 09:32:21 -07:00
										 |  |  |       const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@3', 'snapshot@call@3'); | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |       expect(distillSnapshot(snapshot)).toBe('<BUTTON data="two">Hello</BUTTON>'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-10-23 16:26:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('empty adopted style sheets should not prevent node refs', async ({ page, toImpl, snapshotter, browserName }) => { | 
					
						
							|  |  |  |     it.skip(browserName !== 'chromium', 'Constructed stylesheets are only in Chromium.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await page.setContent('<button>Hello</button>'); | 
					
						
							|  |  |  |     await page.evaluate(() => { | 
					
						
							|  |  |  |       const sheet = new CSSStyleSheet(); | 
					
						
							|  |  |  |       (document as any).adoptedStyleSheets = [sheet]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const sheet2 = new CSSStyleSheet(); | 
					
						
							|  |  |  |       for (const element of [document.createElement('div'), document.createElement('span')]) { | 
					
						
							|  |  |  |         const root = element.attachShadow({ | 
					
						
							|  |  |  |           mode: 'open' | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         root.append('foo'); | 
					
						
							|  |  |  |         (root as any).adoptedStyleSheets = [sheet2]; | 
					
						
							|  |  |  |         document.body.appendChild(element); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 09:32:21 -07:00
										 |  |  |     const renderer1 = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2021-10-23 16:26:46 -07:00
										 |  |  |     // Expect some adopted style sheets.
 | 
					
						
							|  |  |  |     expect(distillSnapshot(renderer1)).toContain('__playwright_style_sheet_'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-22 09:32:21 -07:00
										 |  |  |     const renderer2 = await snapshotter.captureSnapshot(toImpl(page), 'call@2', 'snapshot@call@2'); | 
					
						
							| 
									
										
										
										
											2021-10-23 16:26:46 -07:00
										 |  |  |     const snapshot2 = renderer2.snapshot(); | 
					
						
							|  |  |  |     // Second snapshot should be just a copy of the first one.
 | 
					
						
							|  |  |  |     expect(snapshot2.html).toEqual([[1, 13]]); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-02-17 18:13:45 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it('should not navigate on anchor clicks', async ({ page, toImpl, snapshotter }) => { | 
					
						
							|  |  |  |     await page.setContent('<a href="https://example.com">example.com</a>'); | 
					
						
							| 
									
										
										
										
											2023-03-15 22:33:40 -07:00
										 |  |  |     const snapshot = await snapshotter.captureSnapshot(toImpl(page), 'call@1', 'snapshot@call@1'); | 
					
						
							| 
									
										
										
										
											2023-02-17 18:13:45 -08:00
										 |  |  |     expect(distillSnapshot(snapshot)).toBe('<A href="link://https://example.com">example.com</A>'); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 17:38:01 -07:00
										 |  |  | function distillSnapshot(snapshot, distillTarget = true) { | 
					
						
							|  |  |  |   let { html } = snapshot.render(); | 
					
						
							|  |  |  |   if (distillTarget) | 
					
						
							|  |  |  |     html = html.replace(/\s__playwright_target__="[^"]+"/g, ''); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |   return html | 
					
						
							| 
									
										
										
										
											2021-11-08 18:03:10 -08:00
										 |  |  |       .replace(/<style>\*,\*::before,\*::after { visibility: hidden }<\/style>/, '') | 
					
						
							| 
									
										
										
										
											2021-03-08 19:49:57 -08:00
										 |  |  |       .replace(/<script>[.\s\S]+<\/script>/, '') | 
					
						
							| 
									
										
										
										
											2021-12-18 03:43:19 +09:00
										 |  |  |       .replace(/<style>.*__playwright_target__.*?<\/style>/, '') | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  |       .replace(/<BASE href="about:blank">/, '') | 
					
						
							|  |  |  |       .replace(/<BASE href="http:\/\/localhost:[\d]+\/empty.html">/, '') | 
					
						
							|  |  |  |       .replace(/<HTML>/, '') | 
					
						
							|  |  |  |       .replace(/<\/HTML>/, '') | 
					
						
							|  |  |  |       .replace(/<HEAD>/, '') | 
					
						
							|  |  |  |       .replace(/<\/HEAD>/, '') | 
					
						
							|  |  |  |       .replace(/<BODY>/, '') | 
					
						
							| 
									
										
										
										
											2021-03-10 11:43:26 -08:00
										 |  |  |       .replace(/<\/BODY>/, '').trim(); | 
					
						
							| 
									
										
										
										
											2021-03-01 12:20:04 -08:00
										 |  |  | } |