Skip to content

Commit

Permalink
feat: add ability to use devtools procotol through cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Jan 16, 2024
1 parent 9d8b192 commit 7750da9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Browser = require("./browser");
const signalHandler = require("../signal-handler");
const history = require("./history");
const logger = require("../utils/logger");
const RuntimeConfig = require("../config/runtime-config");

const DEFAULT_PORT = 4444;

Expand Down Expand Up @@ -92,6 +93,7 @@ module.exports = class NewBrowser extends Browser {
const config = this._config;
const gridUri = new URI(config.gridUrl);
const capabilities = this._extendCapabilities(config);
const { devtools } = RuntimeConfig.getInstance();

const options = {
protocol: gridUri.protocol(),
Expand All @@ -100,7 +102,7 @@ module.exports = class NewBrowser extends Browser {
path: gridUri.path(),
queryParams: this._getQueryParams(gridUri.query()),
capabilities,
automationProtocol: config.automationProtocol,
automationProtocol: devtools ? "devtools" : config.automationProtocol,
connectionRetryTimeout: config.sessionRequestTimeout || config.httpTimeout,
connectionRetryCount: 0, // hermione has its own advanced retries
baseUrl: config.baseUrl,
Expand Down
3 changes: 3 additions & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ exports.run = () => {
)
.option("--repl-before-test [type]", "open repl interface before test run", Boolean, false)
.option("--repl-on-fail [type]", "open repl interface on test fail only", Boolean, false)
.option("--devtools", "switches the browser to the devtools mode with using CDP protocol")
.arguments("[paths...]")
.action(async paths => {
try {
Expand All @@ -82,6 +83,7 @@ exports.run = () => {
repl,
replBeforeTest,
replOnFail,
devtools,
} = program;

await handleRequires(requireModules);
Expand All @@ -99,6 +101,7 @@ exports.run = () => {
beforeTest: replBeforeTest,
onFail: replOnFail,
},
devtools: devtools || false,
});

process.exit(isTestsSuccess ? 0 : 1);
Expand Down
4 changes: 3 additions & 1 deletion src/hermione.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface RunOpts {
beforeTest: boolean;
onFail: boolean;
};
devtools: boolean;
}

interface ReadTestsOpts extends Pick<RunOpts, "browsers" | "sets" | "grep" | "replMode"> {
Expand Down Expand Up @@ -69,12 +70,13 @@ export class Hermione extends BaseHermione {
requireModules,
inspectMode,
replMode,
devtools,
reporters = [],
}: Partial<RunOpts> = {},
): Promise<boolean> {
validateUnknownBrowsers(browsers, _.keys(this._config.browsers));

RuntimeConfig.getInstance().extend({ updateRefs, requireModules, inspectMode, replMode });
RuntimeConfig.getInstance().extend({ updateRefs, requireModules, inspectMode, replMode, devtools });

if (replMode?.enabled) {
this._config.system.mochaOpts.timeout = 0;
Expand Down
13 changes: 12 additions & 1 deletion test/src/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const webdriverio = require("webdriverio");
const logger = require("src/utils/logger");
const signalHandler = require("src/signal-handler");
const history = require("src/browser/history");
const { WEBDRIVER_PROTOCOL, SAVE_HISTORY_MODE } = require("src/constants/config");
const { WEBDRIVER_PROTOCOL, DEVTOOLS_PROTOCOL, SAVE_HISTORY_MODE } = require("src/constants/config");
const { X_REQUEST_ID_DELIMITER } = require("src/constants/browser");
const RuntimeConfig = require("src/config/runtime-config");
const { mkNewBrowser_: mkBrowser_, mkSessionStub_ } = require("./utils");

describe("NewBrowser", () => {
Expand All @@ -17,6 +18,8 @@ describe("NewBrowser", () => {
session = mkSessionStub_();
sandbox.stub(logger);
sandbox.stub(webdriverio, "remote").resolves(session);

sandbox.stub(RuntimeConfig, "getInstance").returns({ devtools: undefined });
});

afterEach(() => sandbox.restore());
Expand All @@ -42,6 +45,14 @@ describe("NewBrowser", () => {
});
});

it("should use devtools protocol if hermione runs in devtools mode", async () => {
RuntimeConfig.getInstance.returns({ devtools: true });

await mkBrowser_().init();

assert.calledWithMatch(webdriverio.remote, { automationProtocol: DEVTOOLS_PROTOCOL });
});

it("should pass default port if it is not specified in grid url", async () => {
await mkBrowser_({ gridUrl: "http://some-host/some-path" }).init();

Expand Down
6 changes: 6 additions & 0 deletions test/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,10 @@ describe("cli", () => {
});
});
});

it("should turn on devtools mode from cli", async () => {
await run_("--devtools");

assert.calledWithMatch(Hermione.prototype.run, any, { devtools: true });
});
});
2 changes: 2 additions & 0 deletions test/src/hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ describe("hermione", () => {
replMode: {
enabled: true,
},
devtools: true,
});

assert.calledOnce(RuntimeConfig.getInstance);
Expand All @@ -196,6 +197,7 @@ describe("hermione", () => {
requireModules: ["foo"],
inspectMode: { inspect: true },
replMode: { enabled: true },
devtools: true,
});
assert.callOrder(RuntimeConfig.getInstance, Runner.create);
});
Expand Down

0 comments on commit 7750da9

Please sign in to comment.