Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to use devtools procotol through cli option #827

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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 { DEVTOOLS_PROTOCOL } = require("../constants/config");

const DEFAULT_PORT = 4444;

Expand Down Expand Up @@ -92,6 +94,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 +103,7 @@ module.exports = class NewBrowser extends Browser {
path: gridUri.path(),
queryParams: this._getQueryParams(gridUri.query()),
capabilities,
automationProtocol: config.automationProtocol,
automationProtocol: devtools ? DEVTOOLS_PROTOCOL : 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")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если опция не указана, то devtools: undefined, а если указана то devtools: true

.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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если 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 });
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Опцию переданную из cli сохраняю в runtime конфиге, чтобы затем заюзать при создании браузера.


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
Loading