Skip to content

Commit

Permalink
feat(e2e): support setting browser's UserAgent in CLI (#32)
Browse files Browse the repository at this point in the history
Differential Revision: D39777353

fbshipit-source-id: 49c359142822746fd842e0a4d0d75991e66eb9ba
  • Loading branch information
JacksonGL authored and facebook-github-bot committed Sep 26, 2022
1 parent 23b7102 commit 0a2a7fe
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ async function setupPage(page: Page, options: APIOptions = {}): Promise<void> {
await page.emulate(config.emulateDevice);
}

if (config.defaultUserAgent) {
if (config.defaultUserAgent && config.defaultUserAgent !== 'default') {
await page.setUserAgent(config.defaultUserAgent);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/RunMeasureCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import RunningModeOption from '../options/e2e/RunningModeOption';
import RemoteBrowserDebugOption from '../options/e2e/RemoteBrowserDebugOption';
import ScenarioFileOption from '../options/e2e/ScenarioFileOption';
import SetDeviceOption from '../options/e2e/SetDeviceOption';
import SetUserAgentOption from '../options/e2e/SetUserAgentOption';
import DisableXvfbOption from '../options/e2e/DisableXvfbOption';
import NumberOfRunsOption from '../options/NumberOfRunsOption';
import HeadfulBrowserOption from '../options/e2e/HeadfulBrowserOption';
Expand Down Expand Up @@ -68,6 +69,7 @@ export default class RunMeasureCommand extends BaseCommand {
new RemoteBrowserDebugOption(),
new ScenarioFileOption(),
new SetDeviceOption(),
new SetUserAgentOption(),
new DisableXvfbOption(),
];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/WarmupAppCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import RunningModeOption from '../options/e2e/RunningModeOption';
import RemoteBrowserDebugOption from '../options/e2e/RemoteBrowserDebugOption';
import ScenarioFileOption from '../options/e2e/ScenarioFileOption';
import SetDeviceOption from '../options/e2e/SetDeviceOption';
import SetUserAgentOption from '../options/e2e/SetUserAgentOption';
import DisableXvfbOption from '../options/e2e/DisableXvfbOption';
import SkipWarmupOption from '../options/e2e/SkipWarmupOption';
import CheckXvfbSupportCommand from './snapshot/CheckXvfbSupportCommand';
Expand Down Expand Up @@ -54,6 +55,7 @@ export default class FBWarmupAppCommand extends BaseCommand {
new RemoteBrowserDebugOption(),
new ScenarioFileOption(),
new SetDeviceOption(),
new SetUserAgentOption(),
new DisableXvfbOption(),
new SkipWarmupOption(),
];
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/commands/snapshot/TakeSnapshotCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import DisableXvfbOption from '../../options/e2e/DisableXvfbOption';
import InitDirectoryCommand from '../InitDirectoryCommand';
import CheckXvfbSupportCommand from './CheckXvfbSupportCommand';
import HeadfulBrowserOption from '../../options/e2e/HeadfulBrowserOption';
import SetUserAgentOption from '../../options/e2e/SetUserAgentOption';

export default class TakeSnapshotCommand extends BaseCommand {
getCommandName(): string {
Expand Down Expand Up @@ -71,6 +72,7 @@ export default class TakeSnapshotCommand extends BaseCommand {
new RemoteBrowserDebugOption(),
new ScenarioFileOption(),
new SetDeviceOption(),
new SetUserAgentOption(),
new DisableXvfbOption(),
];
}
Expand Down
37 changes: 37 additions & 0 deletions packages/cli/src/options/e2e/SetUserAgentOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall ws_labs
*/

import type {ParsedArgs} from 'minimist';
import type {MemLabConfig} from '@memlab/core';
import {BaseOption} from '@memlab/core';

export default class SetUserAgentOption extends BaseOption {
getOptionName(): string {
return 'user-agent';
}

getDescription(): string {
return (
'set the UserAgent string in browser (for E2E interaction), ' +
'otherwise it uses the default UserAgent from Chromium'
);
}

getExampleValues(): string[] {
return ['"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2)"'];
}

async parse(config: MemLabConfig, args: ParsedArgs): Promise<void> {
const optionName = this.getOptionName();
if (args[optionName]) {
config.defaultUserAgent = args[optionName];
}
}
}
4 changes: 1 addition & 3 deletions packages/core/src/lib/Constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const constants = {
supportedBrowsers: Object.create(null),
internalDir: 'fb-internal',
monoRepoDir: '',
defaultUserAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2)' +
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.0 Safari/537.36',
defaultUserAgent: 'default',
V8SyntheticRoots: [
'(GC roots)',
'(Internalized strings)',
Expand Down
5 changes: 5 additions & 0 deletions website/docs/cli/CLI-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ memlab run --scenario /tmp/test-scenario.js --work-dir /tmp/test-1/
* **`--local-puppeteer`**: enable remote browser instance debugging via local puppeteer
* **`--scenario`**: set file path loading test scenario
* **`--device`**: set the device type to emulate
* **`--user-agent`**: set the UserAgent string in browser (for E2E interaction), otherwise it uses the default UserAgent from Chromium
* **`--disable-xvfb`**: disable Xvfb (X virtual framebuffer) for simulating headful browser rendering
* **`--leak-filter`**: specify a definition JS file for leak filter
* **`--trace-object-size-above`**: objects with retained size (bytes) bigger than the threshold will be considered as leaks
Expand Down Expand Up @@ -440,6 +441,7 @@ memlab measure --scenario /tmp/test-scenario.js --work-dir /tmp/test-1/
* **`--local-puppeteer`**: enable remote browser instance debugging via local puppeteer
* **`--scenario`**: set file path loading test scenario
* **`--device`**: set the device type to emulate
* **`--user-agent`**: set the UserAgent string in browser (for E2E interaction), otherwise it uses the default UserAgent from Chromium
* **`--disable-xvfb`**: disable Xvfb (X virtual framebuffer) for simulating headful browser rendering
* **`--work-dir`**: set the working directory of the current run
* **`--help`**, **`-h`**: print helper text
Expand Down Expand Up @@ -470,6 +472,7 @@ memlab warmup --scenario /tmp/test-scenario.js
* **`--local-puppeteer`**: enable remote browser instance debugging via local puppeteer
* **`--scenario`**: set file path loading test scenario
* **`--device`**: set the device type to emulate
* **`--user-agent`**: set the UserAgent string in browser (for E2E interaction), otherwise it uses the default UserAgent from Chromium
* **`--disable-xvfb`**: disable Xvfb (X virtual framebuffer) for simulating headful browser rendering
* **`--skip-warmup`**: skip warming up web server
* **`--work-dir`**: set the working directory of the current run
Expand Down Expand Up @@ -538,6 +541,7 @@ memlab snapshot --scenario /tmp/test-scenario.js --work-dir /tmp/test-1/
* **`--local-puppeteer`**: enable remote browser instance debugging via local puppeteer
* **`--scenario`**: set file path loading test scenario
* **`--device`**: set the device type to emulate
* **`--user-agent`**: set the UserAgent string in browser (for E2E interaction), otherwise it uses the default UserAgent from Chromium
* **`--disable-xvfb`**: disable Xvfb (X virtual framebuffer) for simulating headful browser rendering
* **`--work-dir`**: set the working directory of the current run
* **`--help`**, **`-h`**: print helper text
Expand All @@ -563,6 +567,7 @@ memlab warmup-and-snapshot
* **`--local-puppeteer`**: enable remote browser instance debugging via local puppeteer
* **`--scenario`**: set file path loading test scenario
* **`--device`**: set the device type to emulate
* **`--user-agent`**: set the UserAgent string in browser (for E2E interaction), otherwise it uses the default UserAgent from Chromium
* **`--disable-xvfb`**: disable Xvfb (X virtual framebuffer) for simulating headful browser rendering
* **`--skip-warmup`**: skip warming up web server
* **`--full`**: take heap snapshot for every step in E2E interaction
Expand Down

0 comments on commit 0a2a7fe

Please sign in to comment.