Browse Source

Puppeteer E2E test: Log console output (#25382)

* Puppeteer: Log console output

* Don't error that camera is not available
Levi Pesin 2 years ago
parent
commit
892dbbae3d
1 changed files with 65 additions and 8 deletions
  1. 65 8
      test/e2e/puppeteer.js

+ 65 - 8
test/e2e/puppeteer.js

@@ -145,8 +145,10 @@ async function main() {
 
 	/* Prepare page */
 
+	const errorMessagesCache = [];
+
 	const page = ( await browser.pages() )[ 0 ];
-	await preparePage( page, injection, build );
+	await preparePage( page, injection, build, errorMessagesCache );
 
 	/* Loop for each file */
 
@@ -184,13 +186,64 @@ async function main() {
 
 }
 
-async function preparePage( page, injection, build ) {
+async function preparePage( page, injection, build, errorMessages ) {
 
-	/* let page.pageSize */
+	/* let page.file, page.pageSize, page.error */
 
 	await page.evaluateOnNewDocument( injection );
 	await page.setRequestInterception( true );
 
+	page.on( 'console', async msg => {
+
+		const type = msg.type();
+
+		if ( type !== 'warning' && type !== 'error' ) {
+
+			return;
+
+		}
+
+		const file = page.file;
+
+		if ( file === undefined ) {
+
+			return;
+
+		}
+
+		let text = ( await Promise.all( msg.args().map( arg => arg.executionContext().evaluate( arg => arg instanceof Error ? arg.message : arg, arg ) ) ) ).join( ' ' ); // https://github.com/puppeteer/puppeteer/issues/3397#issuecomment-434970058
+
+		text = text.trim();
+		if ( text === '' ) return;
+
+		text = file + ': ' + text.replace( /\[\.WebGL-(.+?)\] /g, '' );
+
+		if ( errorMessages.includes( text ) ) {
+
+			return;
+
+		}
+
+		if ( text.includes( 'Unable to access the camera/webcam' ) ) {
+
+			return;
+
+		}
+
+		errorMessages.push( text );
+
+		if ( type === 'warning' ) {
+
+			console.yellow( text );
+
+		} else {
+
+			page.error = text;
+
+		}
+
+	} );
+
 	page.on( 'response', async ( response ) => {
 
 		try {
@@ -231,7 +284,9 @@ async function makeAttempt( page, failedScreenshots, cleanPage, isMakeScreenshot
 
 	try {
 
+		page.file = file;
 		page.pageSize = 0;
+		page.error = undefined;
 
 		/* Load target page */
 
@@ -295,20 +350,22 @@ async function makeAttempt( page, failedScreenshots, cleanPage, isMakeScreenshot
 
 		} catch ( e ) {
 
-			if ( e.message.includes( 'Render timeout exceeded' ) ) { // This can mean that the example doesn't use requestAnimationFrame loop
+			if ( ! e.message.includes( 'Render timeout exceeded' ) ) {
 
-				console.yellow( `Render timeout exceeded in file ${ file }` );
+				throw new Error( `Error happened while rendering file ${ file }: ${ e }` );
 
-			} else {
+			} /* else { // This can mean that the example doesn't use requestAnimationFrame loop
 
-				throw new Error( `Error happened while rendering file ${ file }: ${ e }` );
+				console.yellow( `Render timeout exceeded in file ${ file }` );
 
-			}
+			} */
 
 		}
 
 		const screenshot = ( await jimp.read( await page.screenshot() ) ).scale( 1 / viewScale ).quality( jpgQuality );
 
+		if ( page.error !== undefined ) throw new Error( page.error );
+
 		if ( isMakeScreenshot ) {
 
 			/* Make screenshots */