Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse browser instances across ct-node-client task #220

Open
zepumph opened this issue Nov 1, 2024 · 3 comments
Open

Reuse browser instances across ct-node-client task #220

zepumph opened this issue Nov 1, 2024 · 3 comments

Comments

@zepumph
Copy link
Member

zepumph commented Nov 1, 2024

Right now each test from CT main gets a new browser. What if we reuse the browser and just recreate new pages, unless there is a problem, then we can start over.

@zepumph zepumph self-assigned this Nov 1, 2024
@zepumph
Copy link
Member Author

zepumph commented Nov 1, 2024

From work in #219.

@zepumph
Copy link
Member Author

zepumph commented Nov 1, 2024

I got totally thwarted trying to convert ct-node-client to typescript. The functions that are exposed into puppeteer are very awkward as it pertains to Typescript and what is transpiled by tsx.

I reverted fbb4d49 because of it.

@zepumph
Copy link
Member Author

zepumph commented Nov 1, 2024

I don't think the error handling is just right yet:

Subject: [PATCH] convert to typescript, https://github.com/phetsims/aqua/issues/220 https://github.com/phetsims/chipper/issues/1465
---
Index: js/node-client/runTest.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/js/node-client/runTest.js b/js/node-client/runTest.js
--- a/js/node-client/runTest.js	(revision 9041cdd89b7a136b1dc9b55f7499ebe5626da266)
+++ b/js/node-client/runTest.js	(date 1730499269599)
@@ -6,16 +6,19 @@
  * @author Jonathan Olson <jonathan.olson@colorado.edu>
  */
 
-const _ = require( 'lodash' );
-const sendTestResult = require( './sendTestResult.js' );
+const path = require( 'path' );
+const assert = require( 'assert' );
+const browserPageLoad = require( '../../../perennial/js/common/browserPageLoad.js' );
+const _ = require( '../../../perennial/js/npm-dependencies/lodash.js' ).default;
+const winston = require( '../../../perennial/js/npm-dependencies/winston.js' ).default;
 const puppeteer = require( '../../../perennial/node_modules/puppeteer' );
-const browserPageLoad = require( '../../../perennial/js/common/browserPageLoad.js' );
-const winston = require( 'winston' );
-const path = require( 'path' );
+const sendTestResult = require( './sendTestResult.js' );
+
 require( 'dotenv' ).config();
 
 /* global window */
 
+
 /**
  * Runs a CT test
  * @public
@@ -24,7 +27,7 @@
  * @param {Object} [options] - see browserPageLoad
  * @returns {Promise}
  */
-module.exports = async function( testInfo, options ) {
+async function runTest( testInfo, options ) {
 
   // Make sure not to resolve if we still have results being sent.
   let currentSendingCount = 0;
@@ -136,6 +139,30 @@
     }
   }, options );
 
+  assert( options.browser, 'please' );
+
+
+  const testInfoQueryParam = `testInfo=${encodeURIComponent( JSON.stringify( {
+    test: testInfo.test,
+    snapshotName: testInfo.snapshotName,
+    timestamp: testInfo.timestamp
+  } ) )}`;
+
+  const url = `${options.fileServerURL}/aqua/html/${testInfo.url}${testInfo.url.includes( '?' ) ? '&' : '?'}${testInfoQueryParam}`;
+
+  try {
+    await browserPageLoad( options.browserCreator, url, options );
+  }
+  catch( e ) {
+    throw new Error( `${e}\n${log}` ); // Post the error with the log from the browser run
+  }
+}
+
+runTest.createBrowser = options => {
+  const browserCreator = options.browserCreator || puppeteer;
+
+  const launchOptions = {};
+
   // Puppeteer-specific Options
   if ( options.browserCreator === puppeteer ) {
 
@@ -157,21 +184,10 @@
         '--no-zygote',
         '--no-sandbox'
       ]
-    }, options.launchOptions );
+    }, launchOptions );
   }
-
-  const testInfoQueryParam = `testInfo=${encodeURIComponent( JSON.stringify( {
-    test: testInfo.test,
-    snapshotName: testInfo.snapshotName,
-    timestamp: testInfo.timestamp
-  } ) )}`;
-
-  const url = `${options.fileServerURL}/aqua/html/${testInfo.url}${testInfo.url.includes( '?' ) ? '&' : '?'}${testInfoQueryParam}`;
+  // @ts-expect-error - TODO: https://github.com/phetsims/perennial/issues/369
+  return browserCreator.launch( options.launchOptions );
+};
 
-  try {
-    await browserPageLoad( options.browserCreator, url, options );
-  }
-  catch( e ) {
-    throw new Error( `${e}\n${log}` ); // Post the error with the log from the browser run
-  }
-};
\ No newline at end of file
+module.exports = runTest;
\ No newline at end of file
Index: js/grunt/tasks/ct-node-client.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/js/grunt/tasks/ct-node-client.ts b/js/grunt/tasks/ct-node-client.ts
--- a/js/grunt/tasks/ct-node-client.ts	(revision 9041cdd89b7a136b1dc9b55f7499ebe5626da266)
+++ b/js/grunt/tasks/ct-node-client.ts	(date 1730499245602)
@@ -15,7 +15,8 @@
 import getOption from '../../../../perennial/js/grunt/tasks/util/getOption';
 import winston from '../../../../perennial/js/npm-dependencies/winston';
 import playwright from '../../../../perennial/node_modules/playwright';
-import runNextTest from '../../node-client/runNextTest';
+import runNextTest from '../../node-client/runNextTest.js';
+import runTest from '../../node-client/runTest.js';
 
 winston.default.transports.console.level = 'info';
 
@@ -47,9 +48,19 @@
 winston.info( `Config: ${JSON.stringify( options )}` );
 
 ( async () => {
+  let browser = null;
   while ( true ) {
+    if ( !browser ) {
+      console.log( options.browserCreator );
+      browser = await runTest.createBrowser( options );
+      options.browser = browser;
+    }
+
     const success = await runNextTest( options );
     if ( !success ) {
+      await browser.close();
+      console.log( 'closed' );
+      browser = null;
       winston.error( 'running test unsuccessful, waiting a few seconds to run next test' );
       await sleep( 5000 );
     }

zepumph added a commit that referenced this issue Nov 1, 2024
zepumph added a commit that referenced this issue Nov 1, 2024
zepumph added a commit that referenced this issue Nov 1, 2024
@zepumph zepumph removed their assignment Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant