Skip to content

Commit eaa702c

Browse files
authored
chore(e2e): refactor resize into its own method; use compass default instead of os max screen size (#7545)
* chore(e2e): refactor resize into its own method; use compass default instead of os max screen size * fix(e2e): allow slight deviations when resizing window * fix(e2e): skip expected height check on macos * fix(e2e): account for old compass in smokes not returning from maximize handler * fix(e2e): more smoke workarounds * debugging smokes is not fun
1 parent 3b96c54 commit eaa702c

File tree

4 files changed

+89
-28
lines changed

4 files changed

+89
-28
lines changed

packages/compass-e2e-tests/helpers/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ export * from './read-first-document-content';
6666
export * from './read-stage-operators';
6767
export * from './click-confirmation-action';
6868
export * from './get-input-by-label';
69+
export * from './resize-window';
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { inspect } from 'util';
2+
import type { CompassBrowser } from '../compass-browser';
3+
import { isTestingDesktop } from '../test-runner-context';
4+
5+
type WindowSize = Awaited<ReturnType<CompassBrowser['getWindowSize']>>;
6+
7+
function isEqualWithMargin(a: number, b: number, margin = 30) {
8+
return Math.abs(a - b) <= margin;
9+
}
10+
11+
export async function resizeWindow(
12+
browser: CompassBrowser,
13+
width: number,
14+
height: number,
15+
dangerouslySkipWaitFor?: boolean
16+
): Promise<WindowSize> {
17+
let newSize: WindowSize | undefined | void;
18+
// On macOS you can only change height as much as the system allows, so when
19+
// on macOS, skip checking for the height matching what we requested. That's
20+
// not great that we can't be sure that we got what we requested, but there's
21+
// little we can do and generally speaking we usually mostly care about the
22+
// width being big enough when resizing
23+
const skipHeightCheck = process.platform === 'darwin';
24+
try {
25+
await browser.waitUntil(async () => {
26+
// Electron doesn't support setWindowSize / getWindowSize, so we use a
27+
// custom ipc handler
28+
if (isTestingDesktop()) {
29+
newSize = await browser.execute(
30+
async (_width: number, _height: number) => {
31+
// eslint-disable-next-line @typescript-eslint/no-require-imports
32+
return await require('electron').ipcRenderer.invoke(
33+
'compass:maximize',
34+
_width,
35+
_height
36+
);
37+
},
38+
width,
39+
height
40+
);
41+
newSize ??= { width: 0, height: 0 }; // in older compass versions 'compass:maximize' doesn't return anything
42+
} else {
43+
await browser.setWindowSize(width, height);
44+
newSize = await browser.getWindowSize();
45+
}
46+
return (
47+
dangerouslySkipWaitFor ||
48+
(newSize &&
49+
isEqualWithMargin(newSize.width, width) &&
50+
(skipHeightCheck || isEqualWithMargin(newSize.height, height)))
51+
);
52+
});
53+
} catch (err) {
54+
throw new Error(
55+
`Failed to update window size: expected ${inspect({
56+
width,
57+
height,
58+
})}, but got ${inspect(newSize)}. Original error:\n\n${
59+
(err as Error).message
60+
}`
61+
);
62+
}
63+
return newSize!;
64+
}

packages/compass-e2e-tests/helpers/compass.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,30 +1147,17 @@ export async function init(
11471147
opts.dangerouslySkipSharedConfigWaitFor
11481148
);
11491149

1150-
if (TEST_COMPASS_WEB) {
1151-
// larger window for more consistent results
1152-
const [width, height] = await browser.execute(() => {
1153-
// in case setWindowSize() below doesn't work
1154-
// eslint-disable-next-line no-restricted-globals
1155-
window.resizeTo(window.screen.availWidth, window.screen.availHeight);
1156-
// eslint-disable-next-line no-restricted-globals
1157-
return [window.screen.availWidth, window.screen.availHeight];
1158-
});
1159-
// getting available width=1512, height=944 in electron on mac which is arbitrary
1160-
debug(`available width=${width}, height=${height}`);
1161-
try {
1162-
// window.resizeTo() doesn't work on firefox
1163-
await browser.setWindowSize(width, height);
1164-
} catch (err) {
1165-
console.error(err instanceof Error ? err.stack : err);
1166-
}
1167-
} else {
1168-
await browser.execute(() => {
1169-
// eslint-disable-next-line @typescript-eslint/no-require-imports
1170-
const { ipcRenderer } = require('electron');
1171-
void ipcRenderer.invoke('compass:maximize');
1172-
});
1173-
}
1150+
// Matches Compass desktop defaults
1151+
const defaultWindowWidth = 1432;
1152+
const defaultWindowHeight = 840;
1153+
1154+
const { width: newWidth, height: newHeight } = await browser.resizeWindow(
1155+
defaultWindowWidth,
1156+
defaultWindowHeight,
1157+
opts.dangerouslySkipSharedConfigWaitFor
1158+
);
1159+
1160+
debug(`resized window to ${newWidth}x${newHeight}`);
11741161

11751162
if (compass.needsCloseWelcomeModal) {
11761163
try {

packages/compass/src/main/window-manager.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,19 @@ class CompassWindowManager {
394394
// To resize an electron window you have to do it from the main process.
395395
// This is here so that the e2e tests can resize the window from the
396396
// renderer process.
397-
ipcMain?.handle('compass:maximize', () => {
398-
const first = BrowserWindow.getAllWindows()[0];
399-
first.maximize();
400-
});
397+
ipcMain?.handle(
398+
'compass:maximize',
399+
(evt, width: number, height: number) => {
400+
const window = BrowserWindow.fromWebContents(evt.sender);
401+
if (width && height) {
402+
window?.setSize(width, height);
403+
} else {
404+
window?.maximize();
405+
}
406+
const [newWidth, newHeight] = window?.getSize() ?? [];
407+
return window ? { width: newWidth, height: newHeight } : null;
408+
}
409+
);
401410

402411
await electronApp.whenReady();
403412
await onAppReady();

0 commit comments

Comments
 (0)