From 83d9b1899fdd4657fc99978da1069fba2be57181 Mon Sep 17 00:00:00 2001 From: zepumph Date: Mon, 12 Jul 2021 12:48:03 -0400 Subject: [PATCH] use withServer instead of localTestingURL, https://github.com/phetsims/perennial/issues/175 --- js/grunt/PDOMComparison.js | 99 ++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 46 deletions(-) diff --git a/js/grunt/PDOMComparison.js b/js/grunt/PDOMComparison.js index 18942b23..24d28a8d 100644 --- a/js/grunt/PDOMComparison.js +++ b/js/grunt/PDOMComparison.js @@ -18,7 +18,6 @@ */ -const buildLocal = require( '../common/buildLocal' ); const execute = require( '../common/execute' ); const getActiveRepos = require( '../common/getActiveRepos' ); const getDependencies = require( '../common/getDependencies' ); @@ -28,6 +27,7 @@ const logger = require( 'html-differ/lib/logger' ); const _ = require( 'lodash' ); // eslint-disable-line const puppeteer = require( 'puppeteer' ); const winston = require( 'winston' ); +const withServer = require( '../common/withServer' ); // constants const HtmlDiffer = htmlDiffer.HtmlDiffer; @@ -40,6 +40,7 @@ module.exports = async ( repo, sha ) => { // get the current working copy PDOM const workingCopyPDOM = await launchSimAndGetPDOMText( repo ); + assert( workingCopyPDOM, 'should be defined' ); // keep track of the repos that will need to be unstashed. This is necessary to make sure we don't apply a stash that // this task didn't save to begin with, @@ -73,6 +74,7 @@ module.exports = async ( repo, sha ) => { ignoreDuplicateAttributes: false } ); + console.log( 'HTML Diff:' ); const diff = htmlDiffer.diffHtml( workingCopyPDOM, oldShaPDOM ); // TODO https://github.com/phetsims/perennial/issues/138 better interpretation of the diff that is output. Perhaps by looking at "diff" more manually, see https://www.npmjs.com/package/html-differ @@ -145,58 +147,63 @@ const stashAll = async repos => { */ const launchSimAndGetPDOMText = async repo => { - const browser = await puppeteer.launch(); - const page = await browser.newPage(); + return withServer( async port => { + let pdoms = null; + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + + page.on( 'console', msg => { + if ( msg.type() === 'error' ) { + console.error( 'PAGE ERROR:', msg.text() ); + } + else { + console.log( 'PAGE LOG:', msg.text() ); + } + } ); - page.on( 'console', msg => { - if ( msg.type() === 'error' ) { - console.error( 'PAGE ERROR:', msg.text() ); - } - else { - console.log( 'PAGE LOG:', msg.text() ); - } - } ); + page.on( 'error', msg => { throw new Error( msg ); } ); + page.on( 'pageerror', msg => { throw new Error( msg ); } ); - page.on( 'load', async () => { + try { + await page.goto( `http://localhost:${port}/${repo}/${repo}_en.html?brand=phet&postMessageOnLoad` ); - const pdoms = await page.evaluate( async () => { + pdoms = await page.evaluate( async () => { + return new Promise( ( resolve, reject ) => { - // TODO https://github.com/phetsims/perennial/issues/138 - // window.phet.sim.joist.frameEndedEmitter.addListener(); - window.addEventListener( 'message', event => { - if ( event.data ) { - try { - const messageData = JSON.parse( event.data ); - if ( messageData.type === 'load' ) { - console.log( 'sim loaded' ); - return phet.joist.display.pdomRootElement.outerHTML; + // TODO https://github.com/phetsims/perennial/issues/138 + // window.phet.sim.joist.frameEndedEmitter.addListener(); + + window.addEventListener( 'message', event => { + if ( event.data ) { + try { + const messageData = JSON.parse( event.data ); + if ( messageData.type === 'load' ) { + console.log( 'sim loaded' ); + resolve( phet.joist.display.pdomRootElement.outerHTML ); + } + } + catch( e ) { + + // message isn't what we wanted it to be, so ignore it + console.log( 'CAUGHT ERROR:', e.message ); + } } - } - catch( e ) { - - // message isn't what we wanted it to be, so ignore it - console.log( 'CAUGHT ERROR:', e.message ); - } - } - return ''; + resolve( '' ); + } ); + setTimeout( () => { + throw new Error( 'Load timeout' ); + }, 20000 ); + } ); } ); - setTimeout( () => { - throw new Error( 'Load timeout' ); - }, 20000 ); - } ); - browser.close(); + } + catch( e ) { + throw new Error( e ); + } + finally { + browser.close(); + } return pdoms; } ); - - page.on( 'error', msg => { throw new Error( msg ); } ); - page.on( 'pageerror', msg => { throw new Error( msg ); } ); - - try { - await page.goto( `${buildLocal.localTestingURL}${repo}/${repo}_en.html?brand=phet&postMessageOnLoad` ); - } - catch( e ) { - browser.close(); - throw new Error( e ); - } }; +