Skip to content

Commit 724dbd5

Browse files
authored
Add browser.profile setting and fix Firefox addArguments bug (#226)
This PR adds a new browser.profile JSON config setting which can be used like this: { "benchmarks": [ { "url": "mybench.html", "browser": { "name": "firefox", "profile": "/Users/<username>/Library/Application Support/Firefox/Profiles/<profile-name>" } } ] } It also fixes a bug where addArguments was not being applied to Firefox, even though it was documented that it was supported. For Chrome, it was previously supported and documented to use "addArguments": ["user-data-dir=<path>"] to achieve this same effect, but I found that the equivalent for Firefox ("-profile=<path>") caused Selenium to timeout trying to connect to the process. I wasn't able to figure out exactly why this was happening, but I did notice a dedicated setProfile method just for Firefox, which does work. So by adding the browser.profile setting, we now have a way to call this special API, plus the user doesn't need to remember the user-data-dir flag in Chrome. Fixes #222 Also threw in a fix to tests relating to chromedriver having updated to Chrome 94 while GitHub Actions is still on Chrome 93.
1 parent 19fc355 commit 724dbd5

File tree

6 files changed

+102
-14
lines changed

6 files changed

+102
-14
lines changed

.github/workflows/tests.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ jobs:
2828
cache: npm
2929

3030
- run: npm ci
31+
# TODO(aomarks) The latest chromedriver has updated to 94, but GitHub
32+
# Actions hasn't updated yet. Pin to the earlier verison of chromedriver
33+
# until GitHub Actions updated.
34+
- run: npm install chromedriver@^93.0.0
3135
- run: npm run build
3236

3337
- run: npm test

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ project adheres to [Semantic Versioning](http://semver.org/).
1919
- Fix bug where log files would be created with '\' backslash names instead of
2020
nested directories.
2121

22+
- Fix bug where `browser.addArguments` JSON config setting did not work for
23+
Firefox.
24+
25+
- Add `browser.profile` JSON config setting that sets the browser profile
26+
directory. Currently supported in Chrome and Firefox.
27+
2228
- Upgrade dependencies.
2329

2430
## [0.5.9] 2021-04-22

README.md

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -538,26 +538,65 @@ However, in some cases it may be useful to use an existing browser profile, for
538538
example if the webpage you are benchmarking requires being signed into an
539539
account.
540540

541-
In Chrome, you can use the `user-data-dir` flag to launch the browser using an
542-
existing profile directory. You may also need to remove the `use-mock-keychain`
543-
default argument if you encounter authentication problems. You can find out the
544-
current binary path, profile location, and arguments of a running Chrome session
545-
by visiting the `chrome://version` URL.
541+
In Chrome and Firefox, use the `profile` JSON config option to specify an
542+
existing profile to use. Other browsers do not yet support this option.
546543

547-
NOTE: If there is an existing Chrome process using the profile, you must
548-
first terminate it. You also need to close all open tabs, or disable the
549-
"Continue where you left off" startup setting, because tachometer does not
550-
expect to find any existing tabs.
544+
#### Chrome
545+
546+
To find your current profile location in Chrome, visit `chrome://version` and
547+
look for "Profile Path".
548+
549+
If there is an existing Chrome process using this profile, you must first
550+
terminate it. You also need to close all open tabs, or disable the "Continue
551+
where you left off" startup setting, because tachometer does not expect to find
552+
any existing tabs.
553+
554+
You may also need to remove the `use-mock-keychain` default argument if you
555+
encounter authentication problems.
551556

552557
For example, using the standard location of the default user profile on macOS:
553558

554559
```json
555560
{
556-
"name": "chrome",
557-
"addArguments": [
558-
"user-data-dir=/Users/<username>/Library/Application Support/Google/Chrome"
559-
],
560-
"removeArguments": ["use-mock-keychain"]
561+
"benchmarks": [
562+
{
563+
"url": "mybench.html",
564+
"browser": {
565+
"name": "chrome",
566+
"profile": "/Users/<username>/Library/Application Support/Google/Chrome",
567+
"removeArguments": ["use-mock-keychain"]
568+
}
569+
}
570+
]
571+
}
572+
```
573+
574+
#### Firefox
575+
576+
To find your current profile location in Firefox, visit `about:support` and look
577+
for "Profile Folder" or "Profile Directory".
578+
579+
Note when using the `profile` option in Firefox, the profile directory is copied
580+
to a temporary location.
581+
582+
<!-- TODO(aomarks) Send a PR to selenium-webdriver to fix this stat error. -->
583+
584+
You may encounter a `no such file or directory, stat '.../lock'` error, due to a
585+
bug in `selenium-webdriver`. Deleting this `lock` file should resolve the error.
586+
587+
For example, using the standard location of user profiles on macOS:
588+
589+
```json
590+
{
591+
"benchmarks": [
592+
{
593+
"url": "mybench.html",
594+
"browser": {
595+
"name": "firefox",
596+
"profile": "/Users/<username>/Library/Application Support/Firefox/Profiles/<profile-name>"
597+
}
598+
}
599+
]
561600
}
562601
```
563602

config.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
],
5151
"type": "string"
5252
},
53+
"profile": {
54+
"description": "Path to a profile directory to use instead of the default temporary fresh\none.",
55+
"type": "string"
56+
},
5357
"remoteUrl": {
5458
"description": "A remote WebDriver server HTTP address to launch the browser from.",
5559
"type": "string"
@@ -291,6 +295,10 @@
291295
"description": "Advanced preferences that are usually set from the about:config page\nin Firefox (see\nhttps://support.mozilla.org/en-US/kb/about-config-editor-firefox).",
292296
"type": "object"
293297
},
298+
"profile": {
299+
"description": "Path to a profile directory to use instead of the default temporary fresh\none.",
300+
"type": "string"
301+
},
294302
"remoteUrl": {
295303
"description": "A remote WebDriver server HTTP address to launch the browser from.",
296304
"type": "string"

src/browser.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export interface BrowserConfig {
6868
preferences?: {[name: string]: string | number | boolean};
6969
/** Trace browser performance logs configuration */
7070
trace?: TraceConfig;
71+
/** Path to profile directory to use instead of the default fresh one. */
72+
profile?: string;
7173
}
7274

7375
/**
@@ -105,6 +107,7 @@ export function browserSignature(config: BrowserConfig): string {
105107
config.removeArguments ?? [],
106108
config.cpuThrottlingRate ?? 1,
107109
config.preferences ?? {},
110+
config.profile ?? '',
108111
]);
109112
}
110113

@@ -243,6 +246,9 @@ function chromeOpts(config: BrowserConfig): chrome.Options {
243246
}
244247
const {width, height} = config.windowSize;
245248
opts.addArguments(`--window-size=${width},${height}`);
249+
if (config.profile) {
250+
opts.addArguments(`user-data-dir=${config.profile}`);
251+
}
246252
return opts;
247253
}
248254

@@ -262,6 +268,16 @@ function firefoxOpts(config: BrowserConfig): firefox.Options {
262268
const {width, height} = config.windowSize;
263269
opts.addArguments(`-width=${width}`);
264270
opts.addArguments(`-height=${height}`);
271+
if (config.addArguments) {
272+
opts.addArguments(...config.addArguments);
273+
}
274+
if (config.profile) {
275+
// Note there is also a `-profile` flag for Firefox that could be set with
276+
// `addArguments`, but using that causes Selenium to timeout trying to
277+
// connect to the browser process. This `setProfile` method creates a
278+
// temporary copy of the profile.
279+
opts.setProfile(config.profile);
280+
}
265281
return opts;
266282
}
267283

src/configfile.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ interface ChromeConfig extends BrowserConfigBase {
241241
* Optional config to turn on performance tracing.
242242
*/
243243
trace?: TraceConfig | true;
244+
245+
/**
246+
* Path to a profile directory to use instead of the default temporary fresh
247+
* one.
248+
*/
249+
profile?: string;
244250
}
245251

246252
/**
@@ -285,6 +291,12 @@ interface FirefoxConfig extends BrowserConfigBase {
285291
* https://support.mozilla.org/en-US/kb/about-config-editor-firefox).
286292
*/
287293
preferences?: {[name: string]: string | number | boolean};
294+
295+
/**
296+
* Path to a profile directory to use instead of the default temporary fresh
297+
* one.
298+
*/
299+
profile?: string;
288300
}
289301

290302
interface SafariConfig extends BrowserConfigBase {
@@ -526,6 +538,9 @@ function parseBrowserObject(config: BrowserConfigs): BrowserConfig {
526538
};
527539
}
528540
}
541+
if ('profile' in config && config.profile !== undefined) {
542+
parsed.profile = config.profile;
543+
}
529544
return parsed;
530545
}
531546

0 commit comments

Comments
 (0)