Skip to content

Commit 52ee992

Browse files
committed
Add option to choose which browser to use (chrome or firefox)
Fixes #1
1 parent a147587 commit 52ee992

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ This will run the benchmark served by `http://localhost:3000/benchmarks/example/
4141

4242
Note: You can also run benchmarks directly in a browser. To do so, visit its URL and invoke `window.startTest().then(console.table);` on the Console.
4343

44+
### Choosing which browser to run the benchmarks in
45+
46+
To select which browser to test things in, use the `--browser` option.
47+
48+
```bash
49+
npm run benchmark example -- --browser=firefox
50+
```
51+
52+
Supported options:
53+
54+
- `chrome` = Use Chrome
55+
- `firefox`= Use Firefox
56+
57+
The default used browser is `chrome`.
58+
59+
A note will be printed on screen to show which version you are using. For example:
60+
61+
```
62+
ℹ️ Running benchmark using browser firefox (firefox/129.0a1)
63+
```
64+
4465
## Creating a benchmark
4566

4667
Benchmarks are HTML pages stored in a subfolder in `./src/benchmarks/`. The page **MUST** expose a `window.startTest` method which returns a promise. When the test logic is done, it **MUST** resolve that promise.

cli/index.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,54 @@
55

66
import puppeteer from 'puppeteer';
77

8-
if (!process.argv[2]) {
9-
console.error('❌ Missing test argument');
8+
const args = process.argv.slice(2);
9+
const flags = args.filter(a => a.startsWith('--'));
10+
const params = args.filter(a => !a.startsWith('--'));
11+
12+
// Determine which page to use
13+
if (!params.length) {
14+
console.error('❌ Missing benchmark argument.');
1015
process.exit(1);
1116
}
12-
1317
const pageUrl = `http://localhost:3000/benchmarks/${process.argv[2]}`.trim();
1418

19+
// Determine which browser to use
20+
const supportedBrowsers = ['chrome', 'firefox'];
21+
let requestedBrowser = flags.filter(f => f.startsWith('--browser=')).reduce((p, c) => `${p}${c}`, '');
22+
if (requestedBrowser) {
23+
requestedBrowser = requestedBrowser.replace('--browser=', '');
24+
25+
if (!supportedBrowsers.includes(requestedBrowser)) {
26+
console.error(`❌ Invalid browser. Only accepted values are ${supportedBrowsers.join(', ')}`);
27+
process.exit(1);
28+
}
29+
} else {
30+
requestedBrowser = supportedBrowsers[0];
31+
}
32+
33+
const puppeteerOptions = {
34+
'chrome': {
35+
headless: 'new',
36+
args: [
37+
"--flag-switches-begin",
38+
"--enable-experimental-web-platform-features",
39+
"--flag-switches-end",
40+
],
41+
},
42+
'firefox': {
43+
44+
},
45+
};
46+
1547
const browser = await puppeteer.launch({
16-
headless: 'new',
17-
product: 'chrome',
48+
product: requestedBrowser,
1849
protocol: 'webDriverBiDi',
19-
args:[
20-
"--flag-switches-begin",
21-
"--enable-experimental-web-platform-features",
22-
"--flag-switches-end",
23-
],
50+
...puppeteerOptions[requestedBrowser],
2451
});
2552

53+
const browserVersion = await browser.version();
54+
console.info(`ℹ️ Running benchmark using browser ${requestedBrowser} (${browserVersion})`);
55+
2656
const page = await browser.newPage();
2757

2858
// Pipe Console output and errors to CLI
@@ -34,13 +64,14 @@ page.on('console', (message) => {
3464

3565
// Catch server not running
3666
page.on('requestfailed', (request) => {
37-
console.error(`❌ ${request.failure().errorText} ${request.url()}`);
3867

3968
switch (request.failure().errorText) {
4069
case 'net::ERR_CONNECTION_REFUSED':
41-
console.info('ℹ️ Please start the webserver first by running `npm run start`');
70+
console.error(`❌ Could not connect to server`);
71+
console.info('ℹ️ Please start the webserver first by running `npm run start` in parallel');
4272
break;
4373
default:
74+
console.error(`❌ ${request.failure().errorText} ${request.url()}`);
4475
break;
4576
}
4677
process.exit(1);

0 commit comments

Comments
 (0)