diff --git a/src/browser/commands/index.ts b/src/browser/commands/index.ts index 3586f6c71..6e947cd8a 100644 --- a/src/browser/commands/index.ts +++ b/src/browser/commands/index.ts @@ -9,4 +9,5 @@ export const customCommandFileNames = [ "switchToRepl", "moveCursorTo", "captureDomSnapshot", + "setWindowSize", ]; diff --git a/src/browser/commands/setWindowSize.ts b/src/browser/commands/setWindowSize.ts new file mode 100644 index 000000000..bb019a5ef --- /dev/null +++ b/src/browser/commands/setWindowSize.ts @@ -0,0 +1,20 @@ +import type { Browser } from "../browser"; + +const setWindowSizeCommand = (browser: Browser): void => { + const { publicAPI: session } = browser; + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + session.overwriteCommand("setWindowSize", async (origSetWindowSize: any, width: number, height: number) => { + browser.applyState({ + currentWindowSize: { + width, + height, + }, + }); + + const result = await origSetWindowSize(width, height); + return result; + }); +}; + +export default setWindowSizeCommand; diff --git a/src/worker/runner/test-runner/index.js b/src/worker/runner/test-runner/index.js index 958fcf03a..51413c4df 100644 --- a/src/worker/runner/test-runner/index.js +++ b/src/worker/runner/test-runner/index.js @@ -118,6 +118,15 @@ module.exports = class TestRunner { this._browser.state.isLastTestFailed = true; } + const currentWindowSize = this._browser.state?.currentWindowSize; + const configSetWindowSize = this._browser.config.windowSize; + + if (currentWindowSize) { + if (!_.isEqual(currentWindowSize, configSetWindowSize)) { + this._browser.publicAPI.setWindowSize(configSetWindowSize); + } + } + this._browserAgent.freeBrowser(this._browser); if (error) { diff --git a/test/src/worker/runner/test-runner/index.js b/test/src/worker/runner/test-runner/index.js index adfbbfc48..59197b4d5 100644 --- a/test/src/worker/runner/test-runner/index.js +++ b/test/src/worker/runner/test-runner/index.js @@ -64,6 +64,7 @@ describe("worker/runner/test-runner", () => { moveToElement: sandbox.stub().named("moveToElement").resolves(), action: sandbox.stub().named("getSize").returns(mkActionAPI_()), isW3C: true, + setWindowSize: sandbox.stub().named("setWindowSize").resolves({ x: 0, y: 0, width: 1024, height: 768 }), }); config = _.defaults(config, { resetCursor: true }); @@ -915,6 +916,51 @@ describe("worker/runner/test-runner", () => { assert.isUndefined(err.history); }); }); + + describe("setWindowSize override", () => { + it("should automatically reset currentSetWindowSize flag when creating browser", async () => { + const browser = mkBrowser_({ config: { windowSize: { width: 10, height: 10 } } }); + browser.state.currentWindowSize = { width: 20, height: 20 }; + + BrowserAgent.prototype.getBrowser.resolves(browser); + + await run_(); + + assert.calledWith(browser.publicAPI.setWindowSize, { width: 10, height: 10 }); + }); + + it("should not reset currentSetWindowSize flag when creating browser with same value as in config", async () => { + const browser = mkBrowser_({ config: { windowSize: { width: 10, height: 10 } } }); + browser.state.currentWindowSize = { width: 10, height: 10 }; + + BrowserAgent.prototype.getBrowser.resolves(browser); + + await run_(); + + assert.notCalled(browser.publicAPI.setWindowSize); + }); + + it("should automatically reset currentSetWindowSize flag when creating browser without config", async () => { + const browser = mkBrowser_(); + browser.state.currentWindowSize = { width: 20, height: 20 }; + + BrowserAgent.prototype.getBrowser.resolves(browser); + + await run_(); + + assert.calledWith(browser.publicAPI.setWindowSize, undefined); + }); + + it("should not set currentSetWindowSize when setWindowSize not called", async () => { + const browser = mkBrowser_({ config: { windowSize: { width: 10, height: 10 } } }); + + BrowserAgent.prototype.getBrowser.resolves(browser); + + await run_(); + + assert.notCalled(browser.publicAPI.setWindowSize); + }); + }); }); });