Skip to content

Commit

Permalink
Add test:playwright:open-browser script
Browse files Browse the repository at this point in the history
Lets you open a headed browser which is configured to use the
interception proxy. Useful for local debugging of browser tests.
  • Loading branch information
lawrence-forooghian committed Apr 19, 2024
1 parent ff17a2c commit 737f32d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/common/modules/interception_proxy_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions test/support/openPlaywrightBrowser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { openPlaywrightBrowser } = require('./playwrightHelpers');

(async function run() {
await openPlaywrightBrowser(false /* headless */);
})();
28 changes: 28 additions & 0 deletions test/support/playwrightHelpers.js
Original file line number Diff line number Diff line change
@@ -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 };
26 changes: 5 additions & 21 deletions test/support/runPlaywrightTests.js
Original file line number Diff line number Diff line change
@@ -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()}`);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 737f32d

Please sign in to comment.