Skip to content
10 changes: 9 additions & 1 deletion src/browser/provider/built-in/dedicated/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,20 @@ export default {
await writePng(path, pngImage);
},

async maximizeWindow (browserId) {
async maximizeWindowHeadless (browserId) {
const maximumSize = getMaximizedHeadlessWindowSize();

await this.resizeWindow(browserId, maximumSize.width, maximumSize.height, maximumSize.width, maximumSize.height);
},

async maximizeWindowNativeAutomation (browserId) {
await this.maximizeWindow(browserId);
},

async resizeWindowNativeAutomation (browserId, width, height, currentWidth, currentHeight) {
await this.resizeBounds(browserId, width, height, currentWidth, currentHeight);
},

async closeBrowserChildWindow (browserId, windowId) {
const runtimeInfo = this.openedBrowsers[browserId];
const browserClient = this._getBrowserProtocolClient(runtimeInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class BrowserClient {
this._runtimeInfo.emulatedDevicePixelRatio = this._config.scaleFactor || this._runtimeInfo.originalDevicePixelRatio;
}

public async resizeWindow (newDimensions: Size): Promise<void> {
public async resizeWindow (newDimensions: { width: number, height: number}): Promise<void> {
const { browserId, config, viewportSize, providerMethods, emulatedDevicePixelRatio } = this._runtimeInfo;

const currentWidth = viewportSize.width;
Expand Down Expand Up @@ -232,6 +232,50 @@ export class BrowserClient {
}
}

public async maximizeWindow (): Promise<void> {
const target = await getFirstTab(this._port);
const client = await this.getActiveClient();

if (client) {
const windowParams = await client.Browser.getWindowForTarget({ targetId: target.id });

await client.Browser.setWindowBounds({ windowId: windowParams.windowId, bounds: { windowState: 'maximized' } });
}
}

public async resizeBounds (newDimensions: Size): Promise<void> {
const { viewportSize, config } = this._runtimeInfo;

const horizontal = viewportSize.outerWidth - viewportSize.width;
const vertical = viewportSize.outerHeight - viewportSize.height;

const target = await getFirstTab(this._port);
const client = await this.getActiveClient();

if (client) {
await this._setDeviceMetricsOverride(client, newDimensions.width, newDimensions.height, 1, config.mobile);

const windowParams = await client.Browser.getWindowForTarget({ targetId: target.id });

if (windowParams.bounds.windowState !== 'normal') {
await client.Browser.setWindowBounds({
windowId: windowParams.windowId,
bounds: {
windowState: 'normal',
},
});
}

await client.Browser.setWindowBounds({
windowId: windowParams.windowId,
bounds: {
width: newDimensions.width + horizontal,
height: newDimensions.height + vertical,
},
});
}
}

public isHeadlessTab (): boolean {
return !!this._parentTarget && this._config.headless;
}
Expand Down
12 changes: 12 additions & 0 deletions src/browser/provider/built-in/dedicated/chrome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ export default {
await runtimeInfo.browserClient.resizeWindow({ width, height });
},

async maximizeWindow (browserId) {
const { browserClient } = this.openedBrowsers[browserId];

await browserClient.maximizeWindow();
},

async resizeBounds (browserId, width, height, currentWidth, currentHeight) {
const { browserClient } = this.openedBrowsers[browserId];

await browserClient.resizeBounds({ width, height, currentWidth, currentHeight });
},

async startCapturingVideo (browserId) {
const { browserClient } = this.openedBrowsers[browserId];

Expand Down
2 changes: 2 additions & 0 deletions src/browser/provider/built-in/dedicated/chrome/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { BrowserClient } from './cdp-client';
export interface Size {
width: number;
height: number;
outerWidth: number;
outerHeight: number;
}

export interface Config {
Expand Down
15 changes: 12 additions & 3 deletions src/browser/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const DEBUG_LOGGER = debug('testcafe:browser:provider');

const BROWSER_OPENING_DELAY = 2000;

const BROWSER_MINIMAL_WIDTH = 500;

const RESIZE_DIFF_SIZE = {
width: 100,
height: 100,
Expand Down Expand Up @@ -355,11 +357,15 @@ export default class BrowserProvider {
return await this.plugin.isValidBrowserName(browserName);
}

public async resizeWindow (browserId: string, width: number, height: number, currentWidth: number, currentHeight: number): Promise<void> {
public async resizeWindow (browserId: string, width: number, height: number, currentWidth: number, currentHeight: number, isNativeAutomation: boolean): Promise<void> {
const canUseDefaultWindowActions = await this.canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomResizeWindow = customActionsInfo.hasResizeWindow;

if (canUseDefaultWindowActions && !hasCustomResizeWindow && isNativeAutomation && width >= BROWSER_MINIMAL_WIDTH) {
await this.plugin.resizeWindowNativeAutomation(browserId, width, height, currentWidth, currentHeight);
return;
}

if (canUseDefaultWindowActions && !hasCustomResizeWindow) {
await this._resizeLocalBrowserWindow(browserId, width, height, currentWidth, currentHeight);
Expand All @@ -381,15 +387,18 @@ export default class BrowserProvider {
return await this.plugin.canResizeWindowToDimensions(browserId, width, height);
}

public async maximizeWindow (browserId: string): Promise<void> {
public async maximizeWindow (browserId: string, isNativeAutomation: boolean): Promise<void> {
const canUseDefaultWindowActions = await this.canUseDefaultWindowActions(browserId);
const customActionsInfo = await this.hasCustomActionForBrowser(browserId);
const hasCustomMaximizeWindow = customActionsInfo.hasMaximizeWindow;

if (canUseDefaultWindowActions && !hasCustomMaximizeWindow && isNativeAutomation)
return await this.plugin.maximizeWindowNativeAutomation(browserId);

if (canUseDefaultWindowActions && !hasCustomMaximizeWindow)
return await this._maximizeLocalBrowserWindow(browserId);

return await this.plugin.maximizeWindow(browserId);
return await this.plugin.maximizeWindowHeadless(browserId);
}

public async takeScreenshot (browserId: string, screenshotPath: string, pageWidth: number, pageHeight: number, fullPage: boolean): Promise<void> {
Expand Down
7 changes: 4 additions & 3 deletions src/test-run/browser-manipulation-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import WARNING_MESSAGE from '../notifications/warning-message';
import { WindowDimensionsOverflowError } from '../errors/test-run/';

export default class BrowserManipulationQueue {
constructor (browserConnection, screenshotCapturer, warningLog) {
constructor (browserConnection, screenshotCapturer, warningLog, isNativeAutomation) {
this.commands = [];
this.browserId = browserConnection.id;
this.browserProvider = browserConnection.provider;
this.screenshotCapturer = screenshotCapturer;
this.warningLog = warningLog;
this.isNativeAutomation = isNativeAutomation;
}

async _resizeWindow (width, height, currentWidth, currentHeight) {
Expand All @@ -20,7 +21,7 @@ export default class BrowserManipulationQueue {
throw new WindowDimensionsOverflowError();

try {
return await this.browserProvider.resizeWindow(this.browserId, width, height, currentWidth, currentHeight);
return await this.browserProvider.resizeWindow(this.browserId, width, height, currentWidth, currentHeight, this.isNativeAutomation);
}
catch (err) {
this.warningLog.addWarning(WARNING_MESSAGE.resizeError, err.message);
Expand All @@ -40,7 +41,7 @@ export default class BrowserManipulationQueue {

async _maximizeWindow () {
try {
return await this.browserProvider.maximizeWindow(this.browserId);
return await this.browserProvider.maximizeWindow(this.browserId, this.isNativeAutomation);
}
catch (err) {
this.warningLog.addWarning(WARNING_MESSAGE.maximizeError, err.message);
Expand Down
2 changes: 1 addition & 1 deletion src/test-run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export default class TestRun extends AsyncEventEmitter {
this.disableDebugBreakpoints = false;
this.debugReporterPluginHost = new ReporterPluginHost({ noColors: false });

this.browserManipulationQueue = new BrowserManipulationQueue(browserConnection, screenshotCapturer, this.warningLog);
this.browserManipulationQueue = new BrowserManipulationQueue(browserConnection, screenshotCapturer, this.warningLog, nativeAutomation);

this.debugLog = new TestRunDebugLog(this.browserConnection.userAgent);

Expand Down
4 changes: 4 additions & 0 deletions test/functional/fixtures/api/es-next/resize-window/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe('[API] Resize window actions', function () {
errorInEachBrowserContains(errs, '> 76 | await t.resizeWindow(500, 500);', 0);
});
});

it('Should resize the window after maximizeWindow', function () {
return runTests('./testcafe-fixtures/resize-window-test.js', 'Resize the window after maximizeWindow', { only: 'chrome' });
});
});

describe('t.resizeWindowToFitDevice', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,12 @@ test('Too big size', async t => {

await t.resizeWindow(hugeWidth, hugeHeight);
});

test('Resize the window after maximizeWindow', async t => {
await t.maximizeWindow();

await t.resizeWindow(640, 480);

expect(await getWindowWidth()).equals(640);
expect(await getWindowHeight()).equals(480);
});