Skip to content

Commit

Permalink
fix: correctly generate test x request id for each test in one browser
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Jan 11, 2024
1 parent 53a7faa commit a1b7b88
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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()}`;
}

Expand Down
4 changes: 2 additions & 2 deletions src/browser/existing-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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,
};
}
Expand Down
12 changes: 10 additions & 2 deletions src/runner/test-runner/regular-test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/worker/hermione.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface WorkerRunTestOpts {
sessionId: string;
sessionCaps: WdioBrowser["capabilities"];
sessionOpts: WdioBrowser["options"];
testXReqId: string;
state: Record<string, unknown>;
}

export interface AssertViewResultsSuccess {
Expand Down
4 changes: 2 additions & 2 deletions src/worker/runner/browser-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/worker/runner/browser-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
4 changes: 2 additions & 2 deletions src/worker/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
};
4 changes: 2 additions & 2 deletions src/worker/runner/test-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down

0 comments on commit a1b7b88

Please sign in to comment.