orchestration | resize selenium window | tile selenium chrome windows | Search

This code provides functions to control browser windows and tabs, with the primary purpose of ensuring that only a single window remains open after a series of actions. It achieves this by closing all other windows and tabs, leaving a designated window active.

Run example

npm run import -- "only one window"

only one window

var importer = require('../Core');

function closeAllTabs(client, keep) {
    return client
        .getWindowHandles()
        .then(h => {
            var promises = [];
            for(var i = 0; i < h.length; i++) {
                if(h[i] === keep) {
                    continue;
                }
                promises.push((i => resolve => client
                              .switchToWindow(h[i])
                              .then(() => client.close())
                              .then(() => resolve())
                              .catch(e => resolve()))
                              .apply(this, [i]));
            }
            return importer.runAllPromises(promises);
        })
        .catch(e => console.log(e))
}

function closeAllWindows(client, keep) {
    return client
        .getWindowHandles()
        .then(h => {
            var promises = [];
            for(var i = 0; i < h.length; i++) {
                if(h[i] === keep) {
                    continue;
                }
                promises.push((i => resolve => client
                              .switchToWindow(h[i])
                              .then(() => client.close())
                              .then(() => resolve())
                              .catch(e => resolve()))
                              .apply(this, [i]));
            }
            return importer.runAllPromises(promises);
        })
        .then(() => client.switchToWindow(keep))
        .catch(e => console.log(e))
}

function onlyOneWindow(client) {
    return client
        .newWindow('https://google.com')
        .then(() => client.getWindowHandle())
        .then(r => closeAllWindows(client, r))
        .then(() => client.getWindowHandle())
        .then(r => closeAllTabs(client, r))
        .catch(e => console.log(e))
    // TODO: close all tabs
}

module.exports = onlyOneWindow;

What the code could have been:

const { runAllPromises } = require('../Core');

/**
 * Closes all tabs in the current window except the one specified by `keep`.
 *
 * @param {object} client - The client object with methods for interacting with the browser.
 * @param {string} keep - The handle of the window to keep open.
 * @returns {Promise} A promise that resolves when all tabs have been closed.
 */
async function closeAllTabs(client, keep) {
    const handles = await client.getWindowHandles();
    const promises = handles.filter(handle => handle!== keep).map(i => () => client.closeWindow());
    await runAllPromises(promises);
}

/**
 * Closes all windows in the browser except the one specified by `keep`.
 *
 * @param {object} client - The client object with methods for interacting with the browser.
 * @param {string} keep - The handle of the window to keep open.
 * @returns {Promise} A promise that resolves when all windows have been closed.
 */
async function closeAllWindows(client, keep) {
    await closeAllTabs(client, keep);
    return client.switchToWindow(keep);
}

/**
 * Ensures there is only one window open in the browser.
 *
 * @param {object} client - The client object with methods for interacting with the browser.
 * @returns {Promise} A promise that resolves when the operation is complete.
 */
async function onlyOneWindow(client) {
    try {
        const newHandle = await client.newWindow('https://google.com');
        await closeAllWindows(client, newHandle);
    } catch (error) {
        console.error(error);
    }
}

module.exports = onlyOneWindow;

This code defines functions to manage browser windows and tabs, specifically focusing on ensuring only one window remains open.

Here's a breakdown:

  1. closeAllTabs(client, keep):

  2. closeAllWindows(client, keep):

  3. onlyOneWindow(client):

  4. Export:

Let me know if you have any other questions.