diff --git a/package.json b/package.json index f0316d67f..193edaa79 100644 --- a/package.json +++ b/package.json @@ -132,6 +132,7 @@ "test:node:skip-build": "mocha", "test:webserver": "grunt test:webserver", "test:playwright": "node test/support/runPlaywrightTests.js", + "test:playwright:open-browser": "node test/support/openPlaywrightBrowser.js", "test:react": "vitest run", "test:package": "grunt test:package", "test:proxy": "npm run build && esr test/interception-proxy/server.ts", diff --git a/test/common/modules/interception_proxy_client.js b/test/common/modules/interception_proxy_client.js index 3de949f15..e7224dcce 100644 --- a/test/common/modules/interception_proxy_client.js +++ b/test/common/modules/interception_proxy_client.js @@ -188,7 +188,7 @@ define(['ably', 'shared_helper'], function (Ably, helper) { } else { // assume it's the response to our startInterception call; TODO sort this out if ('error' in message) { - this.onFailedToStartInterception(message.error); + this.onFailedToStartInterception(new Error(message.error.message)); } else { this.onStartedInterception(); } diff --git a/test/support/openPlaywrightBrowser.js b/test/support/openPlaywrightBrowser.js new file mode 100644 index 000000000..2f828bd29 --- /dev/null +++ b/test/support/openPlaywrightBrowser.js @@ -0,0 +1,5 @@ +const { openPlaywrightBrowser } = require('./playwrightHelpers'); + +(async function run() { + await openPlaywrightBrowser(false /* headless */); +})(); diff --git a/test/support/playwrightHelpers.js b/test/support/playwrightHelpers.js new file mode 100644 index 000000000..30a33fff5 --- /dev/null +++ b/test/support/playwrightHelpers.js @@ -0,0 +1,28 @@ +const playwright = require('playwright'); +const playwrightBrowsers = ['chromium', 'firefox', 'webkit']; + +async function openPlaywrightBrowser(headless) { + const browserEnv = process.env.PLAYWRIGHT_BROWSER; + + if (!playwrightBrowsers.includes(browserEnv)) { + throw new Error( + `PLAYWRIGHT_BROWSER environment variable must be one of: ${playwrightBrowsers.join( + ', ', + )}. Currently: ${browserEnv}`, + ); + } + + const browserType = playwright[browserEnv]; + + // TODO undo this, trust the certs (it’s risky stuff though especially on e.g. a developer’s Mac where this potentially has global effect, although I guess it’s less risky given that we’re running as a different user) + const browser = await browserType.launch({ headless }); + // bypass localhost so that the proxy doesn’t need to be running in order for us to contact the control API to tell it to be started; TODO there is quite possibly a less convoluted way of starting the proxy in this case, also think in a more holistic manner about the various ways in which we make sure that only certain traffic is intercepted (there are notes dotted around about this) + const page = await browser.newPage({ + proxy: { server: 'localhost:8080', bypass: 'localhost' }, + ignoreHTTPSErrors: true, + }); + + return { browserType, browser, page }; +} + +module.exports = { openPlaywrightBrowser }; diff --git a/test/support/runPlaywrightTests.js b/test/support/runPlaywrightTests.js index 294576f82..dd7452148 100644 --- a/test/support/runPlaywrightTests.js +++ b/test/support/runPlaywrightTests.js @@ -1,23 +1,17 @@ -const playwright = require('playwright'); const path = require('path'); const MochaServer = require('../web_server'); const fs = require('fs'); const jUnitDirectoryPath = require('./junit_directory_path'); +const { openPlaywrightBrowser } = require('./playwrightHelpers'); const port = process.env.PORT || 3000; const host = 'localhost'; -const playwrightBrowsers = ['chromium', 'firefox', 'webkit']; const mochaServer = new MochaServer(/* playwrightTest: */ true); -const runTests = async (browserType) => { +const runTests = async () => { + const { browserType, browser, page } = await openPlaywrightBrowser(true /* headless */); + await mochaServer.listen(); - // TODO undo this, trust the certs (it’s risky stuff though especially on e.g. a developer’s Mac where this potentially has global effect, although I guess it’s less risky given that we’re running as a different user) - const browser = await browserType.launch(); - // bypass localhost so that the proxy doesn’t need to be running in order for us to contact the control API to tell it to be started; TODO there is quite possibly a less convoluted way of starting the proxy in this case, also think in a more holistic manner about the various ways in which we make sure that only certain traffic is intercepted (there are notes dotted around about this) - const page = await browser.newPage({ - proxy: { server: 'localhost:8080', bypass: 'localhost' }, - ignoreHTTPSErrors: true, - }); await page.goto(`http://${host}:${port}`); console.log(`\nrunning tests in ${browserType.name()}`); @@ -72,17 +66,7 @@ const runTests = async (browserType) => { let caughtError; try { - const browserEnv = process.env.PLAYWRIGHT_BROWSER; - - if (!playwrightBrowsers.includes(browserEnv)) { - throw new Error( - `PLAYWRIGHT_BROWSER environment variable must be one of: ${playwrightBrowsers.join( - ', ', - )}. Currently: ${browserEnv}`, - ); - } - - await runTests(playwright[browserEnv]); + await runTests(); } catch (error) { // save error for now, we must ensure we end mocha web server first. // if we end current process too early, mocha web server will be left running,