diff --git a/lib/handlers/browserHandler.js b/lib/handlers/browserHandler.js index 249425f48..71c59c619 100644 --- a/lib/handlers/browserHandler.js +++ b/lib/handlers/browserHandler.js @@ -26,8 +26,16 @@ const overridePermissions = async (origin, permissions) => { } }; +const setWindowBounds = async (targetId, height, width) => { + const { windowId } = await _browser.getWindowForTarget({ targetId }); + await _browser.setWindowBounds({ + bounds: { height, width }, + windowId, + }); +}; + const clearPermissionOverrides = async () => { await _browser.resetPermissions(); }; -module.exports = { clearPermissionOverrides, overridePermissions }; +module.exports = { clearPermissionOverrides, overridePermissions, setWindowBounds }; diff --git a/lib/taiko.js b/lib/taiko.js index dc33edbeb..d415acf4e 100644 --- a/lib/taiko.js +++ b/lib/taiko.js @@ -638,6 +638,23 @@ module.exports.deleteCookies = async (cookieName, options = {}) => { } }; +/** + * Resize the browser window + * + * @example + * await resizeWindow({width:600, height:800}) + * + * @returns {Promise} + */ +module.exports.resizeWindow = async (options = {}) => { + validate(); + if (options.height === undefined || options.width === undefined) { + throw new Error('Please specify the window height and width'); + } + const [{ targetId }] = await targetHandler.getFirstAvailablePageTarget(); + await browserHandler.setWindowBounds(targetId, options.height, options.width); + descEvent.emit('success', `Window resized to height ${ options.height } and width ${ options.width }`); +}; /** * Get browser cookies * @@ -2616,6 +2633,7 @@ module.exports.metadata = { 'emulateNetwork', 'emulateDevice', 'setViewPort', + 'resizeWindow', 'openTab', 'closeTab', 'openIncognitoWindow', diff --git a/package.json b/package.json index 047afb16d..e7ef2b1e7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "http://json.schemastore.org/package", "name": "taiko", - "version": "1.1.2", + "version": "1.1.3", "description": "Taiko is a Node.js library for automating Chromium based browsers", "main": "bin/taiko.js", "bin": { diff --git a/types/taiko/index.d.ts b/types/taiko/index.d.ts index 9fc9cc3c3..a85c3518a 100644 --- a/types/taiko/index.d.ts +++ b/types/taiko/index.d.ts @@ -114,6 +114,11 @@ export interface CookieOptions { path?: string; } +export interface ResizeWindowOptions { + height?: string; + width?: string; +} + export interface CookieDetailOptions extends CookieOptions { secure?: boolean; httpOnly?: boolean; @@ -389,6 +394,7 @@ export function emulateNetwork( export function emulateDevice(deviceModel: string): Promise; // https://docs.taiko.dev/api/setviewport export function setViewPort(options: ViewPortOptions): Promise; +export function resizeWindow(options: ResizeWindowOptions): Promise; // https://docs.taiko.dev/api/emulateTimezone export function emulateTimezone(timezoneId: string): Promise; // https://docs.taiko.dev/api/opentab