puppeteer.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * @author munrocket / https://github.com/munrocket
  3. */
  4. const puppeteer = require( 'puppeteer' );
  5. const handler = require( 'serve-handler' );
  6. const http = require( 'http' );
  7. const pixelmatch = require( 'pixelmatch' );
  8. const printImage = require( 'image-output' );
  9. const png = require( 'pngjs' ).PNG;
  10. const fs = require( 'fs' );
  11. const port = 1234;
  12. const pixelThreshold = 0.2;
  13. const maxFailedPixels = 0.05;
  14. const networkTimeout = 600;
  15. const networkTax = 2000; // additional timeout for resources size
  16. const pageSizeMinTax = 1.0; // in mb, when networkTax = 0
  17. const pageSizeMaxTax = 5.0; // in mb, when networkTax = networkTax
  18. const renderTimeout = 1200;
  19. const maxAttemptId = 3; // progresseve attempts
  20. const exceptionList = [
  21. 'index',
  22. 'webgl_loader_texture_pvrtc', // not supported in CI, useless
  23. 'webgl_materials_envmaps_parallax',
  24. 'webgl_test_memory2', // gives fatal error in puppeteer
  25. 'webgl_worker_offscreencanvas', // in a worker, not robust
  26. ].concat( ( process.platform === "win32" ) ? [
  27. 'webgl_effects_ascii' // windows fonts not supported
  28. ] : [] );
  29. console.green = ( msg ) => console.log( `\x1b[32m${ msg }\x1b[37m` );
  30. console.red = ( msg ) => console.log( `\x1b[31m${ msg }\x1b[37m` );
  31. console.null = ( msg ) => {};
  32. /* Launch server */
  33. const server = http.createServer( ( request, response ) => {
  34. return handler( request, response );
  35. } );
  36. server.listen( port, async () => {
  37. try {
  38. await pup;
  39. } catch ( e ) {
  40. console.error( e );
  41. } finally {
  42. server.close();
  43. }
  44. } );
  45. server.on( 'SIGINT', () => process.exit( 1 ) );
  46. /* Launch puppeteer with WebGL support in Linux */
  47. const pup = puppeteer.launch( {
  48. headless: !process.env.VISIBLE,
  49. args: [
  50. '--use-gl=egl',
  51. '--no-sandbox',
  52. '--enable-surface-synchronization'
  53. ]
  54. } ).then( async browser => {
  55. /* Prepare page */
  56. const page = ( await browser.pages() )[ 0 ];
  57. await page.setViewport( { width: 800, height: 600 } );
  58. const injection = fs.readFileSync( 'test/diff/deterministic-injection.js', 'utf8' );
  59. await page.evaluateOnNewDocument( injection );
  60. page.on( 'console', msg => ( msg.text().slice( 0, 8 ) === 'Warning.' ) ? console.null( msg.text() ) : {} );
  61. page.on( 'response', async ( response ) => {
  62. try {
  63. await response.buffer().then( buffer => pageSize += buffer.length );
  64. } catch ( e ) {
  65. console.null( `Warning. Wrong request. \n${ e }` );
  66. }
  67. } );
  68. /* Find files */
  69. const exactList = process.argv.slice(2).map( f => f.replace( '.html', '' ) );
  70. const files = fs.readdirSync( './examples' )
  71. .filter( s => s.slice( - 5 ) === '.html' )
  72. .map( s => s.slice( 0, s.length - 5 ) )
  73. .filter( f => ( process.argv.length > 2 ) ? exactList.includes( f ) : !exceptionList.includes( f ) );
  74. /* Loop for each file, with CI parallelism */
  75. let pageSize, file;
  76. let failedScreenshots = 0;
  77. const isParallel = 'CI' in process.env;
  78. const beginId = isParallel ? Math.floor( parseInt( process.env.CI.slice( 0, 1 ) ) * files.length / 4 ) : 0;
  79. const endId = isParallel ? Math.floor( ( parseInt( process.env.CI.slice( - 1 ) ) + 1 ) * files.length / 4 ) : files.length;
  80. for ( let id = beginId; id < endId; ++ id ) {
  81. /* At least 3 attempts before fail */
  82. let attemptId = process.env.MAKE ? 1 : 0;
  83. while ( attemptId < maxAttemptId ) {
  84. /* Load target page */
  85. file = files[ id ];
  86. pageSize = 0;
  87. global.gc();
  88. global.gc();
  89. try {
  90. await page.goto( `http://localhost:${ port }/examples/${ file }.html`, {
  91. waitUntil: 'networkidle2',
  92. timeout: networkTimeout * ( 1 + attemptId )
  93. } );
  94. } catch {
  95. console.null( 'Warning. Network timeout exceeded...' );
  96. }
  97. try {
  98. await page.evaluate( async ( pageSize, pageSizeMinTax, pageSizeMaxTax, networkTax, renderTimeout, attemptId ) => {
  99. /* Prepare page */
  100. let button = document.getElementById( 'startButton' );
  101. if ( button ) {
  102. button.click();
  103. }
  104. let style = document.createElement( 'style' );
  105. style.type = 'text/css';
  106. style.innerHTML = `body { font size: 0 !important; }
  107. #info, button, input, body > div.dg.ac, body > div.lbl { display: none !important; }`;
  108. document.getElementsByTagName( 'head' )[ 0 ].appendChild( style );
  109. let canvas = document.getElementsByTagName( 'canvas' );
  110. for ( let i = 0; i < canvas.length; ++ i ) {
  111. if ( canvas[i].height === 48 ) {
  112. canvas[i].style.display = 'none';
  113. }
  114. }
  115. let resourcesSize = Math.min( 1, ( pageSize / 1024 / 1024 - pageSizeMinTax ) / pageSizeMaxTax );
  116. await new Promise( resolve => setTimeout( resolve, networkTax * resourcesSize * ( 1 + attemptId ) ) );
  117. /* Resolve render promise */
  118. window.chromeRenderStarted = true;
  119. await new Promise( function( resolve ) {
  120. let renderStart = performance.wow();
  121. let waitingLoop = setInterval( function() {
  122. let renderEcceded = ( performance.wow() - renderStart > renderTimeout * window.chromeMaxFrameId * ( 1 + attemptId ) );
  123. if ( window.chromeRenderFinished || renderEcceded ) {
  124. if ( renderEcceded ) {
  125. console.log( 'Warning. Render timeout exceeded...' );
  126. }
  127. clearInterval( waitingLoop );
  128. resolve();
  129. }
  130. }, 0 );
  131. } );
  132. }, pageSize, pageSizeMinTax, pageSizeMaxTax, networkTax, renderTimeout, attemptId );
  133. } catch ( e ) {
  134. if ( ++ attemptId === maxAttemptId ) {
  135. console.red( `WTF? 'Network timeout' is small for your machine. file: ${ file } \n${ e }` );
  136. ++ failedScreenshots;
  137. continue;
  138. } else {
  139. console.log( 'Another attempt..' );
  140. await new Promise( resolve => setTimeout( resolve, networkTimeout * ( 1 + attemptId ) ) );
  141. }
  142. }
  143. /* Make or diff? */
  144. if ( process.env.MAKE ) {
  145. /* Make screenshots */
  146. attemptId = maxAttemptId;
  147. await page.screenshot( { path: `./examples/screenshots/${ file }.png` } );
  148. console.green( `file: ${ file } generated` );
  149. } else if ( fs.existsSync( `./examples/screenshots/${ file }.png` ) ) {
  150. /* Diff screenshots */
  151. let actual = png.sync.read( await page.screenshot() );
  152. let expected = png.sync.read( fs.readFileSync( `./examples/screenshots/${ file }.png` ) );
  153. let diff = new png( { width: actual.width, height: actual.height } );
  154. let numFailedPixels;
  155. try {
  156. numFailedPixels = pixelmatch( expected.data, actual.data, diff.data, actual.width, actual.height, {
  157. threshold: pixelThreshold,
  158. alpha: 0.2,
  159. diffMask: process.env.FORCE_COLOR === '0',
  160. diffColor: process.env.FORCE_COLOR === '0' ? [255, 255, 255] : [255, 0, 0]
  161. } );
  162. } catch {
  163. attemptId = maxAttemptId;
  164. console.red( `ERROR! Image sizes does not match in file: ${ file }` );
  165. ++ failedScreenshots;
  166. continue;
  167. }
  168. numFailedPixels /= actual.width * actual.height;
  169. /* Print results */
  170. if ( numFailedPixels < maxFailedPixels ) {
  171. attemptId = maxAttemptId;
  172. console.green( `diff: ${ numFailedPixels.toFixed( 3 ) }, file: ${ file }` );
  173. } else {
  174. if ( ++ attemptId === maxAttemptId ) {
  175. printImage(diff, console);
  176. console.red( `ERROR! Diff wrong in ${ numFailedPixels.toFixed( 3 ) } of pixels in file: ${ file }` );
  177. ++ failedScreenshots;
  178. continue;
  179. } else {
  180. console.log( 'Another attempt...' );
  181. }
  182. }
  183. } else {
  184. attemptId = maxAttemptId;
  185. console.red( `ERROR! Screenshot not exists: ${ file }` );
  186. ++ failedScreenshots;
  187. continue;
  188. }
  189. }
  190. }
  191. /* Finish */
  192. if ( failedScreenshots ) {
  193. console.red( `TEST FAILED! ${ failedScreenshots } from ${ endId - beginId } screenshots not pass.` );
  194. process.exit( 1 );
  195. } else if ( !process.env.MAKE ) {
  196. console.green( `TEST PASSED! ${ endId - beginId } screenshots correctly rendered.` );
  197. }
  198. await browser.close();
  199. } );