Skip to content

Commit cdc421f

Browse files
feat: Switch size retrieval to use the W3C window/rect endpoint (#2538)
BREAKING CHANGE: The following deprecated driver methods were removed: - getWindowSizeWeb - getWindowSizeNative
1 parent cd12b08 commit cdc421f

File tree

9 files changed

+32
-70
lines changed

9 files changed

+32
-70
lines changed

lib/commands/general.js

+20-39
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,12 @@ const commands = {
3939
/**
4040
* Get the window size
4141
* @this {XCUITestDriver}
42-
* @deprecated Use {@linkcode XCUITestDriver.getWindowRect} instead.
42+
* @returns {Promise<import('@appium/types').Size>}
4343
*/
44-
async getWindowSize(windowHandle = 'current') {
45-
if (windowHandle !== 'current') {
46-
throw new errors.NotYetImplementedError(
47-
'Currently only getting current window size is supported.',
48-
);
49-
}
50-
51-
if (!this.isWebContext()) {
52-
return await this.getWindowSizeNative();
53-
} else {
54-
return await this.getWindowSizeWeb();
55-
}
44+
async getWindowSize() {
45+
const {width, height} = await this.getWindowRect();
46+
return {width, height};
5647
},
57-
5848
/**
5949
* Retrieves the actual device time.
6050
*
@@ -116,15 +106,22 @@ const commands = {
116106
/**
117107
* For W3C
118108
* @this {XCUITestDriver}
109+
* @return {Promise<import('@appium/types').Rect>}
119110
*/
120111
async getWindowRect() {
121-
const {width, height} = await this.getWindowSize();
122-
return {
123-
width,
124-
height,
125-
x: 0,
126-
y: 0,
127-
};
112+
if (this.isWebContext()) {
113+
const script = 'return {' +
114+
'x: window.screenX || 0,' +
115+
'y: window.screenY || 0,' +
116+
'width: window.innerWidth,' +
117+
'height: window.innerHeight' +
118+
'}';
119+
return await this.executeAtom('execute_script', [script]);
120+
}
121+
122+
return /** @type {import('@appium/types').Rect} */ (
123+
await this.proxyCommand('/window/rect', 'GET')
124+
);
128125
},
129126
/**
130127
* @this {XCUITestDriver}
@@ -181,7 +178,7 @@ const commands = {
181178
const scale = await this.getDevicePixelRatio();
182179
// status bar height comes in unscaled, so scale it
183180
const statusBarHeight = Math.round((await this.getStatusBarHeight()) * scale);
184-
const windowSize = await this.getWindowSize();
181+
const windowSize = await this.getWindowRect();
185182

186183
// ios returns coordinates/dimensions in logical pixels, not device pixels,
187184
// so scale up to device pixels. status bar height is already scaled.
@@ -260,23 +257,7 @@ const commands = {
260257
},
261258
};
262259

263-
const helpers = {
264-
/**
265-
* @this {XCUITestDriver}
266-
*/
267-
async getWindowSizeWeb() {
268-
const script = 'return {width: window.innerWidth, height: window.innerHeight}';
269-
return await this.executeAtom('execute_script', [script]);
270-
},
271-
/**
272-
* @this {XCUITestDriver}
273-
*/
274-
async getWindowSizeNative() {
275-
return await this.proxyCommand(`/window/size`, 'GET');
276-
},
277-
};
278-
279-
export default {...helpers, ...commands};
260+
export default commands;
280261

281262
/**
282263
* @typedef {Object} PressButtonOptions

lib/commands/proxy-helper.js

-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ const WDA_ROUTES = /** @type {const} */ ({
4343
'/wda/locked': {
4444
GET: 'isLocked',
4545
},
46-
'/window/size': {
47-
GET: 'getWindowSize',
48-
},
4946
});
5047

5148
/**

lib/commands/screenshots.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -97,33 +97,24 @@ export default {
9797
},
9898
/**
9999
* @this {XCUITestDriver}
100+
* @returns {Promise<string>}
100101
*/
101102
async getViewportScreenshot() {
102103
if (this.isWebContext()) {
103104
return await (/** @type {import('appium-remote-debugger').RemoteDebugger} */ (this.remote)).captureScreenshot();
104105
}
105106

106-
let statusBarHeight = await this.getStatusBarHeight();
107107
const screenshot = await this.getScreenshot();
108-
109108
// if we don't have a status bar, there's nothing to crop, so we can avoid
110109
// extra calls and return straightaway
111-
if (statusBarHeight === 0) {
110+
if ((await this.getStatusBarHeight()) === 0) {
112111
return screenshot;
113112
}
114113

115-
const scale = await this.getDevicePixelRatio();
116-
// status bar height comes in unscaled, so scale it
117-
statusBarHeight = Math.round(statusBarHeight * scale);
118-
const windowSize = await this.getWindowSize();
119-
let rect = {
120-
left: 0,
121-
top: statusBarHeight,
122-
width: windowSize.width * scale,
123-
height: windowSize.height * scale - statusBarHeight,
124-
};
125-
let newScreenshot = await imageUtil.cropBase64Image(screenshot, rect);
126-
return newScreenshot;
114+
return await imageUtil.cropBase64Image(
115+
screenshot,
116+
await this.getViewportRect()
117+
);
127118
},
128119
};
129120

lib/commands/web.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,9 @@ const extensions = {
897897

898898
const currentUrl = await this.getUrl();
899899
await this.setUrl(`${this.getWdaLocalhostRoot()}/calibrate`);
900-
const {width, height} = /** @type {import('@appium/types').Size} */(await this.proxyCommand(`/window/size`, 'GET'));
900+
const {width, height} = /** @type {import('@appium/types').Rect} */(
901+
await this.proxyCommand('/window/rect', 'GET')
902+
);
901903
const [centerX, centerY] = [width / 2, height / 2];
902904
const errorPrefix = 'Cannot determine web view coordinates offset. Are you in Safari context?';
903905

lib/driver.js

-2
Original file line numberDiff line numberDiff line change
@@ -2000,8 +2000,6 @@ export class XCUITestDriver extends BaseDriver {
20002000
getDevicePixelRatio = commands.generalExtensions.getDevicePixelRatio;
20012001
mobilePressButton = commands.generalExtensions.mobilePressButton;
20022002
mobileSiriCommand = commands.generalExtensions.mobileSiriCommand;
2003-
getWindowSizeWeb = commands.generalExtensions.getWindowSizeWeb;
2004-
getWindowSizeNative = commands.generalExtensions.getWindowSizeNative;
20052003

20062004
/*-------------+
20072005
| GEOLOCATION |

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"appium-ios-device": "^2.8.0",
8282
"appium-ios-simulator": "^6.2.0",
8383
"appium-remote-debugger": "^12.2.0",
84-
"appium-webdriveragent": "^9.2.0",
84+
"appium-webdriveragent": "^9.3.0",
8585
"appium-xcode": "^5.1.4",
8686
"async-lock": "^1.4.0",
8787
"asyncbox": "^3.0.0",

test/functional/basic/basic-e2e-specs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ describe('XCUITestDriver - basics -', function () {
195195

196196
describe('window size -', function () {
197197
it('should be able to get the current window size', async function () {
198-
let size = await driver.getWindowSize();
198+
let size = await driver.getWindowRect();
199199
size.width.should.be.a('number');
200200
size.height.should.be.a('number');
201201
});

test/functional/web/safari-basic-e2e-specs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ describe('Safari - basics -', function () {
259259
await a.getTagName().should.eventually.equal('a');
260260
});
261261
it('should retrieve a window size', async function () {
262-
const size = await driver.getWindowSize();
262+
const size = await driver.getWindowRect();
263263
size.height.should.be.above(0);
264264
size.width.should.be.above(0);
265265
});

test/unit/commands/general-specs.js

-7
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,6 @@ describe('general commands', function () {
115115
});
116116
});
117117

118-
describe('window size', function () {
119-
it('should be able to get the current window size with Rect', async function () {
120-
mockDriver.expects('proxyCommand').once().withExactArgs('/window/size', 'GET').returns({width: 100, height: 20});
121-
await driver.getWindowRect();
122-
});
123-
});
124-
125118
describe('nativeWebTap as a setting', function () {
126119
// create new driver with no opts
127120
let driver, startStub;

0 commit comments

Comments
 (0)