Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function runQunitPuppeteer(qunitPuppeteerArgs) {
  2.   const timeout = qunitPuppeteerArgs.timeout || DEFAULT_TIMEOUT;
  3.  
  4.   const puppeteerArgs = qunitPuppeteerArgs.puppeteerArgs || ['--allow-file-access-from-files'];
  5.   const args = { args: puppeteerArgs };
  6.   const browser = await puppeteer.launch(args);
  7.  
  8.   try {
  9.     // Opens the target page
  10.     const page = await browser.newPage();
  11.  
  12.     // Redirect the page console if needed
  13.     if (qunitPuppeteerArgs.redirectConsole) {
  14.       const Console = console;
  15.       page.on('console', (consoleArgs) => { Console.log('[%s] %s', consoleArgs.type(), consoleArgs.text()); });
  16.     }
  17.  
  18.     // Prepare the callbacks that will be called by the page
  19.     const deferred = await exposeCallbacks(page);
  20.  
  21.     // Run the timeout timer just in case
  22.     const timeoutId = setTimeout(() => { deferred.reject(new Error(`Test run could not finish in ${timeout}ms`)); }, timeout);
  23.  
  24.     // Configuration for the in-page script (will be passed via evaluate to the page script)
  25.     const evaluateArgs = {
  26.       testTimeout: timeout,
  27.       callbacks: {
  28.         begin: BEGIN_CB,
  29.         done: DONE_CB,
  30.         moduleStart: MODULE_START_CB,
  31.         moduleDone: MODULE_DONE_CB,
  32.         testStart: TEST_START_CB,
  33.         testDone: TEST_DONE_CB,
  34.         log: LOG_CB,
  35.       },
  36.     };
  37.  
  38.     await page.evaluateOnNewDocument((evaluateArgs) => {
  39.       /* global window */
  40.       // IMPORTANT: This script is executed in the context of the page
  41.       // YOU CANNOT ACCESS ANY VARIABLE OUT OF THIS BLOCK SCOPE
  42.  
  43.       // Save these globals immediately in order to avoid
  44.       // messing with in-page scripts that can redefine them
  45.       const jsonParse = JSON.parse;
  46.       const jsonStringify = JSON.stringify;
  47.       const objectKeys = Object.keys;
  48.  
  49.       /**
  50.        * Clones QUnit context object in a safe manner:
  51.        * https://github.com/ameshkov/node-qunit-puppeteer/issues/16
  52.        *
  53.        * @param {*} object - object to clone in a safe manner
  54.        */
  55.       function safeCloneQUnitContext(object) {
  56.         const clone = {};
  57.  
  58.         objectKeys(object).forEach((prop) => {
  59.           const propValue = object[prop];
  60.           if (propValue === null || typeof propValue === 'undefined') {
  61.             clone[prop] = propValue;
  62.             return;
  63.           }
  64.  
  65.           try {
  66.             clone[prop] = jsonParse(jsonStringify(propValue));
  67.           } catch (ex) {
  68.             // Most likely this is a circular structure
  69.             // In this case we just call toString on this value
  70.             clone[prop] = propValue.toString();
  71.           }
  72.         });
  73.  
  74.         return clone;
  75.       }
  76.  
  77.       /**
  78.        * Changes QUnit so that their callbacks were passed to the main program.
  79.        * We call previously exposed functions for every QUnit callback.
  80.        *
  81.        * @param {*} QUnit - qunit global object
  82.        */
  83.       function extendQUnit(QUnit) {
  84.         try {
  85.           // eslint-disable-next-line
  86.           QUnit.config.testTimeout = evaluateArgs.testTimeout;
  87.  
  88.           // Pass our callback methods to QUnit
  89.           const callbacks = Object.keys(evaluateArgs.callbacks);
  90.           for (let i = 0; i < callbacks.length; i += 1) {
  91.             const qunitName = callbacks[i];
  92.             const callbackName = evaluateArgs.callbacks[qunitName];
  93.             QUnit[qunitName]((context) => { window[callbackName](safeCloneQUnitContext(context)); });
  94.           }
  95.         } catch (ex) {
  96.           const Console = console;
  97.           Console.error(`Error while executing the in-page script: ${ex}`);
  98.         }
  99.       }
  100.  
  101.       let qUnit;
  102.       Object.defineProperty(window, 'QUnit', {
  103.         get: () => qUnit,
  104.         set: (value) => {
  105.           qUnit = value;
  106.           extendQUnit(qUnit);
  107.         },
  108.         configurable: true,
  109.       });
  110.     }, evaluateArgs);
  111.  
  112.     // Open the target page
  113.     await page.goto(qunitPuppeteerArgs.targetUrl);
  114.  
  115.     // Wait for the test result
  116.     const qunitTestResult = await deferred.promise;
  117.  
  118.     // All good, clear the timeout
  119.     clearTimeout(timeoutId);
  120.     return qunitTestResult;
  121.   } catch (ex) {
  122.     throw ex;
  123.   } finally {
  124.     if (browser) {
  125.       browser.close();
  126.     }
  127.   }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement