diff --git a/packages/api/src/API.ts b/packages/api/src/API.ts index e8ce30117..d6812d302 100644 --- a/packages/api/src/API.ts +++ b/packages/api/src/API.ts @@ -322,7 +322,7 @@ async function setupPage(page: Page, options: APIOptions = {}): Promise { await page.emulate(config.emulateDevice); } - if (config.defaultUserAgent) { + if (config.defaultUserAgent && config.defaultUserAgent !== 'default') { await page.setUserAgent(config.defaultUserAgent); } diff --git a/packages/cli/src/commands/RunMeasureCommand.ts b/packages/cli/src/commands/RunMeasureCommand.ts index 1835b1e9c..eba41989e 100644 --- a/packages/cli/src/commands/RunMeasureCommand.ts +++ b/packages/cli/src/commands/RunMeasureCommand.ts @@ -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'; @@ -68,6 +69,7 @@ export default class RunMeasureCommand extends BaseCommand { new RemoteBrowserDebugOption(), new ScenarioFileOption(), new SetDeviceOption(), + new SetUserAgentOption(), new DisableXvfbOption(), ]; } diff --git a/packages/cli/src/commands/WarmupAppCommand.ts b/packages/cli/src/commands/WarmupAppCommand.ts index fa767da21..e4b14d38e 100644 --- a/packages/cli/src/commands/WarmupAppCommand.ts +++ b/packages/cli/src/commands/WarmupAppCommand.ts @@ -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'; @@ -54,6 +55,7 @@ export default class FBWarmupAppCommand extends BaseCommand { new RemoteBrowserDebugOption(), new ScenarioFileOption(), new SetDeviceOption(), + new SetUserAgentOption(), new DisableXvfbOption(), new SkipWarmupOption(), ]; diff --git a/packages/cli/src/commands/snapshot/TakeSnapshotCommand.ts b/packages/cli/src/commands/snapshot/TakeSnapshotCommand.ts index 35301ce80..1de313835 100644 --- a/packages/cli/src/commands/snapshot/TakeSnapshotCommand.ts +++ b/packages/cli/src/commands/snapshot/TakeSnapshotCommand.ts @@ -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 { @@ -71,6 +72,7 @@ export default class TakeSnapshotCommand extends BaseCommand { new RemoteBrowserDebugOption(), new ScenarioFileOption(), new SetDeviceOption(), + new SetUserAgentOption(), new DisableXvfbOption(), ]; } diff --git a/packages/cli/src/options/e2e/SetUserAgentOption.ts b/packages/cli/src/options/e2e/SetUserAgentOption.ts new file mode 100644 index 000000000..a26f25b4b --- /dev/null +++ b/packages/cli/src/options/e2e/SetUserAgentOption.ts @@ -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 { + const optionName = this.getOptionName(); + if (args[optionName]) { + config.defaultUserAgent = args[optionName]; + } + } +} diff --git a/packages/core/src/lib/Constant.ts b/packages/core/src/lib/Constant.ts index a6eddb18f..ec1a94023 100644 --- a/packages/core/src/lib/Constant.ts +++ b/packages/core/src/lib/Constant.ts @@ -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)', diff --git a/website/docs/cli/CLI-commands.md b/website/docs/cli/CLI-commands.md index 2494ae41e..06111cfa1 100644 --- a/website/docs/cli/CLI-commands.md +++ b/website/docs/cli/CLI-commands.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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