orchestration | | only one window | Search

This code provides a function to resize and position a browser window into a grid layout, allowing for the management of multiple browser instances in a structured manner. It uses a client object (likely Selenium) to interact with the browser and calculates window positions based on a specified grid structure.

Run example

npm run import -- "resize selenium window"

resize selenium window

var rows = 6;
var columns = 6;
var screen = {
    height: 1024 * 6,
    width: 1350 * 6
};

function getScreenSize(client) {
    return client
        .getWindowHandle()
        .then(r => client.maximizeWindow(r))
        .catch(e => {})
        .getWindowSize()
        .then(r => (screen = r))
        .catch(e => console.log(e))
}

function resizeWindow(client, i = 0) {
    i = Math.abs(i + (typeof OFFSET !== 'undefined' ? OFFSET : 0));
    var hwnd;
    return client
        .getWindowHandle()
        .then(r => client.switchToWindow((hwnd = r)))
        .then(() => typeof screen === 'undefined' ? getScreenSize() : screen)
        .catch(e => console.log(e))
        .then(screen => {
            const newPosition = {
                x: Math.floor(i % columns)
                    * Math.floor(screen.width / columns),
                y: Math.floor(i / columns % rows)
                    * Math.floor(screen.height / rows)
            };
            console.log(newPosition);
            return client.setWindowPosition(
                newPosition.x,
                newPosition.y)
        })
        .catch(e => console.log(e))
        .then(() => client.setWindowSize(
            Math.floor(screen.width / columns),
            Math.floor(screen.height / rows)))
        .catch(e => console.log(e))
}


module.exports = resizeWindow;

What the code could have been:

// Constants for screen dimensions
const ROWS = 6;
const COLUMNS = 6;
const SCREEN_WIDTH_MULTIPLIER = 1350;
const SCREEN_HEIGHT_MULTIPLIER = 1024;

// Constant for maximum allowed offset
const MAX_OFFSET = 10000;

// Function to get window size
async function getWindowSize(client) {
    try {
        const windowHandle = await client.getWindowHandle();
        const windowSize = await client Window().getSize();
        return windowSize;
    } catch (error) {
        console.error('Error getting window size:', error);
    }
}

// Function to set window position and size
async function setWindowPosition(client, position) {
    try {
        const { x, y } = position;
        const { width, height } = await getWindowSize(client);
        const newWidth = Math.floor(width / COLUMNS);
        const newHeight = Math.floor(height / ROWS);
        await client.setWindowPosition(x, y);
        await client.setWindowSize(newWidth, newHeight);
        return { x, y, width: newWidth, height: newHeight };
    } catch (error) {
        console.error('Error setting window position and size:', error);
    }
}

// Function to resize window
async function resizeWindow(client, offset = 0) {
    // Calculate new position based on offset
    const newPosition = {
        x: (Math.abs(offset + (typeof OFFSET!== 'undefined'? OFFSET : 0)) % COLUMNS) * Math.floor(getWindowSize(client).width / COLUMNS),
        y: Math.floor((Math.abs(offset + (typeof OFFSET!== 'undefined'? OFFSET : 0)) / COLUMNS) % ROWS) * Math.floor(getWindowSize(client).height / ROWS)
    };

    // Set new window position and size
    const result = await setWindowPosition(client, newPosition);
    console.log(result);
    return result;
}

module.exports = resizeWindow;

This code defines a function resizeWindow that resizes and positions a browser window into a grid layout.

Here's a breakdown:

  1. Initialization:

  2. getScreenSize Function:

  3. resizeWindow Function:

  4. Export:

Let me know if you have any other questions.