From a1b7b886aacf9874cb503f175b61756f9ed1bc5a Mon Sep 17 00:00:00 2001 From: DudaGod Date: Thu, 11 Jan 2024 16:08:53 +0300 Subject: [PATCH] fix: correctly generate test x request id for each test in one browser --- src/browser/browser.js | 4 ++-- src/browser/existing-browser.js | 4 ++-- src/runner/test-runner/regular-test-runner.js | 12 ++++++++++-- src/worker/hermione.ts | 2 +- src/worker/runner/browser-agent.js | 4 ++-- src/worker/runner/browser-pool.js | 4 ++-- src/worker/runner/index.js | 4 ++-- src/worker/runner/test-runner/index.js | 4 ++-- 8 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/browser/browser.js b/src/browser/browser.js index 0f79298fb..f278e60d1 100644 --- a/src/browser/browser.js +++ b/src/browser/browser.js @@ -28,13 +28,13 @@ module.exports = class Browser { constructor(config, opts) { this.id = opts.id; this.version = opts.version; - this.testXReqId = opts.testXReqId; this._config = config.forBrowser(this.id); this._debug = config.system.debug; this._session = null; this._callstackHistory = null; this._state = { + ...opts.state, isBroken: false, }; } @@ -93,7 +93,7 @@ module.exports = class Browser { if (!req.headers["X-Request-ID"]) { req.headers["X-Request-ID"] = `${ - this.testXReqId + this.state.testXReqId }${X_REQUEST_ID_DELIMITER}${crypto.randomUUID()}`; } diff --git a/src/browser/existing-browser.js b/src/browser/existing-browser.js index e33098059..57938c4f4 100644 --- a/src/browser/existing-browser.js +++ b/src/browser/existing-browser.js @@ -69,7 +69,7 @@ module.exports = class ExistingBrowser extends Browser { quit() { this._meta = this._initMeta(); - this._state = { isBroken: false }; + this._state.isBroken = false; } async prepareScreenshot(selectors, opts = {}) { @@ -169,7 +169,7 @@ module.exports = class ExistingBrowser extends Browser { return { pid: process.pid, browserVersion: this.version, - testXReqId: this.testXReqId, + testXReqId: this.state.testXReqId, ...this._config.meta, }; } diff --git a/src/runner/test-runner/regular-test-runner.js b/src/runner/test-runner/regular-test-runner.js index f551498ec..3bdab6e22 100644 --- a/src/runner/test-runner/regular-test-runner.js +++ b/src/runner/test-runner/regular-test-runner.js @@ -65,7 +65,7 @@ module.exports = class RegularTestRunner extends Runner { sessionCaps: this._browser.capabilities, sessionOpts: this._browser.publicAPI.options, file: this._test.file, - testXReqId: this._browser.testXReqId, + state: this._browser.state, }); } @@ -82,7 +82,15 @@ module.exports = class RegularTestRunner extends Runner { async _getBrowser() { try { - this._browser = await this._browserAgent.getBrowser({ testXReqId: crypto.randomUUID() }); + const state = { testXReqId: crypto.randomUUID() }; + + this._browser = await this._browserAgent.getBrowser({ state }); + + // use correct state for cached browsers + if (this._browser.state.testXReqId !== state.testXReqId) { + this._browser.applyState(state); + } + this._test.sessionId = this._browser.sessionId; return this._browser; diff --git a/src/worker/hermione.ts b/src/worker/hermione.ts index cd7ddb111..9daf4c301 100644 --- a/src/worker/hermione.ts +++ b/src/worker/hermione.ts @@ -11,7 +11,7 @@ export interface WorkerRunTestOpts { sessionId: string; sessionCaps: WdioBrowser["capabilities"]; sessionOpts: WdioBrowser["options"]; - testXReqId: string; + state: Record; } export interface AssertViewResultsSuccess { diff --git a/src/worker/runner/browser-agent.js b/src/worker/runner/browser-agent.js index 43bf7d0e7..3b51e76df 100644 --- a/src/worker/runner/browser-agent.js +++ b/src/worker/runner/browser-agent.js @@ -12,14 +12,14 @@ module.exports = class BrowserAgent { this._pool = pool; } - getBrowser({ sessionId, sessionCaps, sessionOpts, testXReqId }) { + getBrowser({ sessionId, sessionCaps, sessionOpts, state }) { return this._pool.getBrowser({ browserId: this.browserId, browserVersion: this.browserVersion, sessionId, sessionCaps, sessionOpts, - testXReqId, + state, }); } diff --git a/src/worker/runner/browser-pool.js b/src/worker/runner/browser-pool.js index c81ab468c..6565ae744 100644 --- a/src/worker/runner/browser-pool.js +++ b/src/worker/runner/browser-pool.js @@ -16,11 +16,11 @@ module.exports = class BrowserPool { this._calibrator = new Calibrator(); } - async getBrowser({ browserId, browserVersion, sessionId, sessionCaps, sessionOpts, testXReqId }) { + async getBrowser({ browserId, browserVersion, sessionId, sessionCaps, sessionOpts, state }) { const browser = Browser.create(this._config, { id: browserId, version: browserVersion, - testXReqId, + state, emitter: this._emitter, }); diff --git a/src/worker/runner/index.js b/src/worker/runner/index.js index e67f06041..39af6525a 100644 --- a/src/worker/runner/index.js +++ b/src/worker/runner/index.js @@ -27,12 +27,12 @@ module.exports = class Runner extends AsyncEmitter { ]); } - async runTest(fullTitle, { browserId, browserVersion, file, sessionId, sessionCaps, sessionOpts, testXReqId }) { + async runTest(fullTitle, { browserId, browserVersion, file, sessionId, sessionCaps, sessionOpts, state }) { const tests = await this._testParser.parse({ file, browserId }); const test = tests.find(t => t.fullTitle() === fullTitle); const browserAgent = BrowserAgent.create({ id: browserId, version: browserVersion, pool: this._browserPool }); const runner = TestRunner.create(test, this._config.forBrowser(browserId), browserAgent); - return runner.run({ sessionId, sessionCaps, sessionOpts, testXReqId }); + return runner.run({ sessionId, sessionCaps, sessionOpts, state }); } }; diff --git a/src/worker/runner/test-runner/index.js b/src/worker/runner/test-runner/index.js index 15e148580..67791e82c 100644 --- a/src/worker/runner/test-runner/index.js +++ b/src/worker/runner/test-runner/index.js @@ -22,14 +22,14 @@ module.exports = class TestRunner { this._browserAgent = browserAgent; } - async run({ sessionId, sessionCaps, sessionOpts, testXReqId }) { + async run({ sessionId, sessionCaps, sessionOpts, state }) { const test = this._test; const hermioneCtx = test.hermioneCtx || {}; let browser; try { - browser = await this._browserAgent.getBrowser({ sessionId, sessionCaps, sessionOpts, testXReqId }); + browser = await this._browserAgent.getBrowser({ sessionId, sessionCaps, sessionOpts, state }); } catch (e) { throw Object.assign(e, { hermioneCtx }); }