Skip to content

Commit

Permalink
feat: add "headless" setting for the main 3 browsers (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
rprovodenko authored Jun 13, 2023
1 parent 34eb381 commit 0400c33
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const CUSTOM_SESSION_OPTS = [
"user",
"key",
"region",
"headless",
];

module.exports = class Browser {
Expand Down
49 changes: 48 additions & 1 deletion src/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ const logger = require("../utils/logger");

const DEFAULT_PORT = 4444;

const headlessBrowserOptions = {
chrome: {
capabilityName: "goog:chromeOptions",
args: ["headless", "disable-gpu"],
},
firefox: {
capabilityName: "moz:firefoxOptions",
args: ["-headless"],
},
msedge: {
capabilityName: "ms:edgeOptions",
args: ["--headless"],
},
edge: {
capabilityName: "ms:edgeOptions",
args: ["--headless"],
},
};

module.exports = class NewBrowser extends Browser {
constructor(config, id, version) {
super(config, id, version);
Expand Down Expand Up @@ -72,7 +91,7 @@ module.exports = class NewBrowser extends Browser {
_getSessionOpts() {
const config = this._config;
const gridUri = new URI(config.gridUrl);
const capabilities = this.version ? this._extendCapabilitiesByVersion() : config.desiredCapabilities;
const capabilities = this._extendCapabilities(config);

const options = {
protocol: gridUri.protocol(),
Expand All @@ -94,6 +113,34 @@ module.exports = class NewBrowser extends Browser {
return options;
}

_extendCapabilities(config) {
const capabilitiesExtendedByVersion = this.version
? this._extendCapabilitiesByVersion()
: config.desiredCapabilities;
const capabilitiesWithAddedHeadless = this._addHeadlessCapability(
config.headless,
capabilitiesExtendedByVersion,
);
return capabilitiesWithAddedHeadless;
}

_addHeadlessCapability(headless, capabilities) {
if (!headless) {
return capabilities;
}
const capabilitySettings = headlessBrowserOptions[capabilities.browserName];
if (!capabilitySettings) {
logger.warn(`WARNING: Headless setting is not supported for ${capabilities.browserName} browserName`);
return capabilities;
}
const browserCapabilities = capabilities[capabilitySettings.capabilityName] ?? {};
capabilities[capabilitySettings.capabilityName] = {
...browserCapabilities,
args: [...(browserCapabilities.args ?? []), ...capabilitySettings.args],
};
return capabilities;
}

_extendCapabilitiesByVersion() {
const { desiredCapabilities, sessionEnvFlags } = this._config;
const versionKeyName =
Expand Down
67 changes: 67 additions & 0 deletions test/src/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,73 @@ describe("NewBrowser", () => {
assert.calledWithMatch(webdriverio.remote, { port: 4444 });
});

describe("headless setting", () => {
it("should generate browser specific settings - chrome", async () => {
await mkBrowser_({
headless: true,
desiredCapabilities: { browserName: "chrome" },
}).init();

assert.calledWithMatch(webdriverio.remote, {
capabilities: {
browserName: "chrome",
"goog:chromeOptions": { args: ["headless", "disable-gpu"] },
},
});
});

it("should generate browser specific settings - firefox", async () => {
await mkBrowser_({
headless: true,
desiredCapabilities: { browserName: "firefox" },
}).init();

assert.calledWithMatch(webdriverio.remote, {
capabilities: {
browserName: "firefox",
"moz:firefoxOptions": { args: ["-headless"] },
},
});
});

it("should generate browser specific settings - edge", async () => {
await mkBrowser_({
headless: true,
desiredCapabilities: { browserName: "msedge" },
}).init();

assert.calledWithMatch(webdriverio.remote, {
capabilities: { browserName: "msedge", "ms:edgeOptions": { args: ["--headless"] } },
});
});

it("not override existing settings", async () => {
await mkBrowser_({
headless: true,
desiredCapabilities: {
browserName: "chrome",
"goog:chromeOptions": { args: ["my", "custom", "flags"] },
},
}).init();

assert.calledWithMatch(webdriverio.remote, {
capabilities: {
browserName: "chrome",
"goog:chromeOptions": { args: ["my", "custom", "flags", "headless", "disable-gpu"] },
},
});
});

it("should issue a warning for an unsupported browser", async () => {
await mkBrowser_({
headless: true,
desiredCapabilities: { browserName: "safari" },
}).init();

assert.calledOnceWith(logger.warn, "WARNING: Headless setting is not supported for safari browserName");
});
});

describe('should create session with extended "browserVersion" in desiredCapabilities if', () => {
it("it is already exists in capabilities", async () => {
await mkBrowser_(
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ declare namespace Hermione {
headers: Record<string, string> | null;

system: SystemConfig;
headless: boolean | null;
}

export interface CompareOptsConfig {
Expand Down

0 comments on commit 0400c33

Please sign in to comment.