| 
									
										
										
										
											2020-09-26 14:30:46 +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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 22:52:17 +08:00
										 |  |  | import type { Frame, Page } from 'playwright-core'; | 
					
						
							| 
									
										
										
										
											2022-06-18 07:11:22 +08:00
										 |  |  | import { ZipFile } from '../../packages/playwright-core/lib/utils/zipFile'; | 
					
						
							| 
									
										
										
										
											2020-09-26 14:30:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export async function attachFrame(page: Page, frameId: string, url: string): Promise<Frame> { | 
					
						
							|  |  |  |   const handle = await page.evaluateHandle(async ({ frameId, url }) => { | 
					
						
							|  |  |  |     const frame = document.createElement('iframe'); | 
					
						
							|  |  |  |     frame.src = url; | 
					
						
							|  |  |  |     frame.id = frameId; | 
					
						
							|  |  |  |     document.body.appendChild(frame); | 
					
						
							|  |  |  |     await new Promise(x => frame.onload = x); | 
					
						
							|  |  |  |     return frame; | 
					
						
							|  |  |  |   }, { frameId, url }); | 
					
						
							|  |  |  |   return handle.asElement().contentFrame(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function detachFrame(page: Page, frameId: string) { | 
					
						
							|  |  |  |   await page.evaluate(frameId => { | 
					
						
							|  |  |  |     document.getElementById(frameId).remove(); | 
					
						
							|  |  |  |   }, frameId); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function verifyViewport(page: Page, width: number, height: number) { | 
					
						
							| 
									
										
										
										
											2023-01-04 09:30:36 +08:00
										 |  |  |   // `expect` may clash in test runner tests if imported eagerly.
 | 
					
						
							|  |  |  |   const { expect } = require('@playwright/test'); | 
					
						
							| 
									
										
										
										
											2020-09-26 14:30:46 +08:00
										 |  |  |   expect(page.viewportSize().width).toBe(width); | 
					
						
							|  |  |  |   expect(page.viewportSize().height).toBe(height); | 
					
						
							|  |  |  |   expect(await page.evaluate('window.innerWidth')).toBe(width); | 
					
						
							|  |  |  |   expect(await page.evaluate('window.innerHeight')).toBe(height); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function expectedSSLError(browserName: string): string { | 
					
						
							|  |  |  |   let expectedSSLError: string; | 
					
						
							|  |  |  |   if (browserName === 'chromium') { | 
					
						
							|  |  |  |     expectedSSLError = 'net::ERR_CERT_AUTHORITY_INVALID'; | 
					
						
							|  |  |  |   } else if (browserName === 'webkit') { | 
					
						
							|  |  |  |     if (process.platform === 'darwin') | 
					
						
							|  |  |  |       expectedSSLError = 'The certificate for this server is invalid'; | 
					
						
							|  |  |  |     else if (process.platform === 'win32') | 
					
						
							|  |  |  |       expectedSSLError = 'SSL peer certificate or SSH remote key was not OK'; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       expectedSSLError = 'Unacceptable TLS certificate'; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     expectedSSLError = 'SSL_ERROR_UNKNOWN'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return expectedSSLError; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-05-05 02:59:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function chromiumVersionLessThan(a: string, b: string) { | 
					
						
							|  |  |  |   const left: number[] = a.split('.').map(e => Number(e)); | 
					
						
							|  |  |  |   const right: number[] = b.split('.').map(e => Number(e)); | 
					
						
							|  |  |  |   for (let i = 0; i < 4; i++) { | 
					
						
							|  |  |  |     if (left[i] > right[i]) | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     if (left[i] < right[i]) | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2021-09-09 11:32:52 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let didSuppressUnverifiedCertificateWarning = false; | 
					
						
							|  |  |  | let originalEmitWarning: (warning: string | Error, ...args: any[]) => void; | 
					
						
							|  |  |  | export function suppressCertificateWarning() { | 
					
						
							|  |  |  |   if (didSuppressUnverifiedCertificateWarning) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   didSuppressUnverifiedCertificateWarning = true; | 
					
						
							|  |  |  |   // Supress one-time warning:
 | 
					
						
							|  |  |  |   // https://github.com/nodejs/node/blob/1bbe66f432591aea83555d27dd76c55fea040a0d/lib/internal/options.js#L37-L49
 | 
					
						
							|  |  |  |   originalEmitWarning = process.emitWarning; | 
					
						
							|  |  |  |   process.emitWarning = (warning, ...args) => { | 
					
						
							|  |  |  |     if (typeof warning === 'string' && warning.includes('NODE_TLS_REJECT_UNAUTHORIZED')) { | 
					
						
							|  |  |  |       process.emitWarning = originalEmitWarning; | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return originalEmitWarning.call(process, warning, ...args); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2021-12-10 09:21:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-04 09:30:36 +08:00
										 |  |  | export async function parseTrace(file: string): Promise<{ events: any[], resources: Map<string, Buffer>, actions: string[] }> { | 
					
						
							| 
									
										
										
										
											2022-06-18 07:11:22 +08:00
										 |  |  |   const zipFS = new ZipFile(file); | 
					
						
							| 
									
										
										
										
											2021-12-10 09:21:17 +08:00
										 |  |  |   const resources = new Map<string, Buffer>(); | 
					
						
							|  |  |  |   for (const entry of await zipFS.entries()) | 
					
						
							|  |  |  |     resources.set(entry, await zipFS.read(entry)); | 
					
						
							|  |  |  |   zipFS.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const events = []; | 
					
						
							|  |  |  |   for (const line of resources.get('trace.trace').toString().split('\n')) { | 
					
						
							|  |  |  |     if (line) | 
					
						
							|  |  |  |       events.push(JSON.parse(line)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   for (const line of resources.get('trace.network').toString().split('\n')) { | 
					
						
							|  |  |  |     if (line) | 
					
						
							|  |  |  |       events.push(JSON.parse(line)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     events, | 
					
						
							|  |  |  |     resources, | 
					
						
							| 
									
										
										
										
											2023-01-04 09:30:36 +08:00
										 |  |  |     actions: eventsToActions(events) | 
					
						
							| 
									
										
										
										
											2021-12-10 09:21:17 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-06-17 07:33:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-04 09:30:36 +08:00
										 |  |  | function eventsToActions(events: any[]): string[] { | 
					
						
							|  |  |  |   // Trace viewer only shows non-internal non-tracing actions.
 | 
					
						
							|  |  |  |   return events.filter(e => e.type === 'action' && !e.metadata.internal && !e.metadata.method.startsWith('tracing')) | 
					
						
							|  |  |  |       .sort((a, b) => a.metadata.startTime - b.metadata.startTime) | 
					
						
							|  |  |  |       .map(e => e.metadata.apiName); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 07:33:32 +08:00
										 |  |  | export async function parseHar(file: string): Promise<Map<string, Buffer>> { | 
					
						
							| 
									
										
										
										
											2022-06-18 07:11:22 +08:00
										 |  |  |   const zipFS = new ZipFile(file); | 
					
						
							| 
									
										
										
										
											2022-06-17 07:33:32 +08:00
										 |  |  |   const resources = new Map<string, Buffer>(); | 
					
						
							|  |  |  |   for (const entry of await zipFS.entries()) | 
					
						
							|  |  |  |     resources.set(entry, await zipFS.read(entry)); | 
					
						
							|  |  |  |   zipFS.close(); | 
					
						
							|  |  |  |   return resources; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-02 04:57:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function waitForTestLog<T>(page: Page, prefix: string): Promise<T> { | 
					
						
							|  |  |  |   return new Promise<T>(resolve => { | 
					
						
							|  |  |  |     page.on('console', message => { | 
					
						
							|  |  |  |       const text = message.text(); | 
					
						
							|  |  |  |       if (text.startsWith(prefix)) { | 
					
						
							|  |  |  |         const json = text.substring(prefix.length); | 
					
						
							|  |  |  |         resolve(JSON.parse(json)); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-11 06:10:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const ansiRegex = new RegExp('[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g'); | 
					
						
							|  |  |  | export function stripAnsi(str: string): string { | 
					
						
							|  |  |  |   return str.replace(ansiRegex, ''); | 
					
						
							|  |  |  | } |