Skip to content

Commit

Permalink
fix: pass chrome options to chromeOptions.addArguments() (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
patsplat authored Jun 3, 2021
1 parent a3430d9 commit 715f463
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
21 changes: 15 additions & 6 deletions packages/cli/src/lib/webdriver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,24 @@ describe('startDriver', () => {
assert.equal((service as any).executable_, config.chromedriverPath);
});

it('sets the --chrome-options flag with no-sandbox', async () => {
it('passes the --no-sandbox argument to chromeOptions', async () => {
browser = 'chrome-headless';
config.chromeOptions = (['--no-sandbox'] as unknown) as Options[];
config.chromeOptions = ['--no-sandbox'];
await startDriver(config);
const capabilities = config?.builder?.getCapabilities();
const chromeOptions = capabilities?.get('chromeOptions');

assert.isArray(chromeOptions.args);
assert.deepEqual(chromeOptions.args, ['--no-sandbox']);
const options = config?.builder?.getChromeOptions();
assert.isArray(options?.get("goog:chromeOptions").args);
assert.deepEqual(options?.get("goog:chromeOptions").args, ['headless', '--no-sandbox']);
});

it('passes multiple arguments argument to chromeOptions', async () => {
browser = 'chrome-headless';
config.chromeOptions = ['no-sandbox', 'disable-dev-shm-usage'];
await startDriver(config);

const options = config?.builder?.getChromeOptions();
assert.isArray(options?.get("goog:chromeOptions").args);
assert.deepEqual(options?.get("goog:chromeOptions").args, ['headless', 'no-sandbox', "disable-dev-shm-usage"]);
});

it('sets the --timeout flag', async () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/lib/webdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ const startDriver = async (
): Promise<WebDriver> => {
const scriptTimeout = (config.timeout || 20) * 1000.0;
let builder: Builder;
const args: chrome.Options[] = [];
/* istanbul ignore else */
if (config.browser === 'chrome-headless') {
const service = new chrome.ServiceBuilder(
config.chromedriverPath || chromedriver.path
).build();
chrome.setDefaultService(service);

let options = new chrome.Options().headless();
if (config.chromeOptions?.length) {
args.push(...config.chromeOptions);
options = config.chromeOptions.reduce(function(options, arg) {
return options.addArguments(arg);
}, options );
}

const chromeCapabilities = Capabilities.chrome();
chromeCapabilities.set('chromeOptions', { args });
builder = new Builder()
.forBrowser('chrome')
.withCapabilities(chromeCapabilities)
.setChromeOptions(new chrome.Options().headless());
.setChromeOptions(options);
} else {
builder = new Builder().forBrowser(config.browser);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface WebdriverConfigParams {
timeout?: number;
chromedriverPath?: string;
path?: string;
chromeOptions?: Options[];
chromeOptions?: string[];
builder?: Builder;
}

Expand Down

0 comments on commit 715f463

Please sign in to comment.