orchestration | only one window | get all session and window urls | Search

This code provides a way to open multiple URLs in separate browser windows and arrange them in a tiled layout using Selenium WebDriver. It leverages imported functions for window resizing and URL management to achieve this functionality.

Run example

npm run import -- "tile selenium chrome windows"

tile selenium chrome windows

var importer = require('../Core');
var {
    resizeWindow,
    getAllSessionUrls
} = importer.import("resize selenium window",
"get all session urls")

function openUrl(client, url) {
    var handles;
    return client
        .getWindowHandles()
        .then(h => handles = h)
        .execute((url, width, height) => {
            window.open(url, '', 'width=1350,height=1024');
        }, url)
        .getWindowHandles()
        .then(h => h.filter(hwnd => handles.indexOf(hwnd) === -1)[0])
        .catch(e => console.log(e))
}

function createNewWindows(client, urls) {
    var hwnd;
    return client
        .getWindowHandle()
        .then(r => hwnd = r)
        .getWindowHandles()
        .then(r => {
            // -1 first window is already used by sliced url
            const count = urls.length - (r.length - 1);
            const promises = [];
            for(var i = 0; i < count; i++) {
                promises.push((i => resolve => {
                    console.log(urls[i]);
                    return openUrl(urls[i])
                        .switchToWindow(hwnd)
                        .then(h => resolve(h))
                        .catch(e => { console.log(e); resolve() })
                }).apply(this, [i]));
            }
            return importer.runAllPromises(promises);
        })
        .catch(e => console.log(e))
}

function tileWindows(client, urls) {
    return client
        .resizeWindow(client, 0)
        .url(typeof urls === 'string' ? urls : urls[0])
        .then(h => typeof urls === 'string' ? [] : createNewWindows(urls.slice(1)))
        .then(() => getAllSessionUrls())
        .then(() => client.getWindowHandles())
        .catch(e => console.log(e))
}
module.exports = tileWindows;

What the code could have been:

const Core = require('../Core');
const {
  resizeWindow,
  getAllSessionUrls
} = Core.import(['resize selenium window', 'get all session urls']);

/**
 * Opens a URL in a new window and returns the handle of the new window.
 * 
 * @param {object} client - The Selenium client.
 * @param {string} url - The URL to open.
 * @returns {Promise<object>} A promise that resolves with the handle of the new window.
 */
function openUrl(client, url) {
  return client
   .getWindowHandles()
   .then(handles => {
      return client
       .execute((url, width, height) => {
          window.open(url, '', 'width=1350,height=1024');
        }, url)
       .then(() => client.getWindowHandles())
       .then(newHandles => newHandles.filter(hwnd =>!handles.includes(hwnd))[0]);
    });
}

/**
 * Creates new windows for the given URLs and returns the handles of the new windows.
 * 
 * @param {object} client - The Selenium client.
 * @param {array} urls - An array of URLs to open in new windows.
 * @returns {Promise<object>} A promise that resolves with an array of handles of the new windows.
 */
function createNewWindows(client, urls) {
  return client
   .getWindowHandle()
   .then(handle => {
      const promises = urls.map((url, index) => {
        return openUrl(client, url).then(h => {
          return client
           .switchToWindow(h)
           .then(() => h);
        });
      });
      return Promise.all(promises);
    });
}

/**
 * Resizes the window, opens the given URL, and creates new windows for the remaining URLs.
 * 
 * @param {object} client - The Selenium client.
 * @param {string|array} urls - The URL or an array of URLs to open.
 * @returns {Promise<object>} A promise that resolves with an array of handles of the new windows.
 */
function tileWindows(client, urls) {
  return resizeWindow(client, 0)
   .then(() => {
      const url = typeof urls ==='string'? urls : urls[0];
      return client
       .url(url)
       .then(() => typeof urls ==='string'? [] : createNewWindows(client, urls.slice(1)));
    })
   .then(() => {
      return getAllSessionUrls().then(() => client.getWindowHandles());
    })
   .then(handles => handles.filter(h =>!handles[0].includes(h)));
}

module.exports = tileWindows;

This code manages multiple browser windows, allowing you to open URLs in separate windows and arrange them in a tiled layout.

Here's a breakdown:

  1. Imports:

  2. openUrl(client, url):

  3. createNewWindows(client, urls):

  4. tileWindows(client, urls):

  5. Export:

Let me know if you have any other questions.