From 6720df535baf9abf598812df9f51b4703071e4bd Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Fri, 9 Aug 2024 09:36:41 +0200 Subject: [PATCH] fix: Add workaround for setting the window position (#271) --- lib/commands/general.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/commands/general.js b/lib/commands/general.js index 8849ea6..bbdab77 100644 --- a/lib/commands/general.js +++ b/lib/commands/general.js @@ -47,12 +47,33 @@ commands.getWindowSize = async function getWindowSize () { // a workaround for https://github.com/appium/appium/issues/15923 commands.getWindowRect = async function getWindowRect () { const {width, height} = await this.getWindowSize(); - return {x: 0, y: 0, width, height}; + let [x, y] = [0, 0]; + try { + const handle = await this.winAppDriver.sendCommand('/window_handle', 'GET'); + ({x, y} = await this.winAppDriver.sendCommand(`/window/${handle}/position`, 'GET')); + } catch (e) { + this.log.warn( + `Cannot fetch the window position. Defaulting to zeroes. Original error: ${e.message}` + ); + } + return {x, y, width, height}; }; // a workaround for https://github.com/appium/appium/issues/15923 commands.setWindowRect = async function setWindowRect (x, y, width, height) { - await this.winAppDriver.sendCommand('/window/size', 'POST', {width, height}); + let didProcess = false; + if (!_.isNil(width) && !_.isNil(height)) { + await this.winAppDriver.sendCommand('/window/size', 'POST', {width, height}); + didProcess = true; + } + if (!_.isNil(x) && !_.isNil(y)) { + const handle = await this.winAppDriver.sendCommand('/window_handle', 'GET'); + await this.winAppDriver.sendCommand(`/window/${handle}/position`, 'POST', {x, y}); + didProcess = true; + } + if (!didProcess) { + this.log.info('Either x and y or width and height must be defined. Doing nothing'); + } }; commands.getScreenshot = async function getScreenshot () {