webdriver | selenium executor | selenium select | Search

The code imports necessary modules for interacting with web browsers and defines constants and functions for managing Selenium sessions, including verifying individual sessions and retrieving active sessions. The getSessions function is exported as a module export, which can be used to retrieve and verify available Selenium sessions.

Run example

npm run import -- "list sessions"

list sessions

const {readSessions} = importer.import("selenium session")
const { WebDriver, Capabilities, Session } = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome');
const createExecutor = importer.import("selenium executor")
const getClient = importer.import("webdriver client")

const LOCAL_URL = 'http://localhost:4444/wd/hub';

async function verifySession(driver, sessionId) {
  let driver2 = new chrome.Driver(
    new Session(sessionId[1], Capabilities.chrome()), createExecutor(Promise.resolve(LOCAL_URL)))

  try {
    let windows = await driver2.getAllWindowHandles()
    console.log('windows ', windows)
    //await driver.switchTo().window(window)
    //let status = await driver.getSession()
    return sessionId[1]
  } catch (e) {
    console.log(e)
  }
}

async function getSessions(driver, inactive = false) {
  const sessions = readSessions()
  if(!driver) {
    driver = await getClient()
  }
  //const session = await driver.getSession()
  //const original = session.getId()
  let active = [].concat(sessions)
      .filter(session => typeof session[1] !== 'undefined'
              && session[1] !== null && session[1].length > 0);
  //if(inactive) {
  //    active = active.filter(session => (new Date()).getTime() - session[0] > TIMEOUT);
  //}
  let cancelled = false;
  let available = []
  for(let i = 0; i < active.length; i++) {
    let r = await verifySession(driver, active[i])
    if(typeof r !== 'undefined') {
      available[available.length] = active[i]
    }
    if(inactive) {
      cancelled = true;
    } else {
    }
  }
  //session.id_ = original;

  return available
    .filter(sess => typeof sess !== 'undefined' && sess !== null)
    .filter((elem, pos, arr) => arr.indexOf(elem) === pos)
}


module.exports = getSessions

What the code could have been:

const { readSessions } = importer.import('selenium session');
const { WebDriver, Capabilities, Session } = require('selenium-webdriver');
const { Builder } = require('selenium-webdriver/chrome');
const createExecutor = importer.import('selenium executor');
const getClient = importer.import('webdriver client');

const LOCAL_URL = 'http://localhost:4444/wd/hub';

/**
 * Verifies a session by getting all window handles.
 * @param {Session} driver - The session to verify.
 * @param {string} sessionId - The ID of the session.
 * @returns {string} The ID of the verified session.
 */
async function verifySession(session, sessionId) {
  try {
    const driver = new Builder().forBrowser('chrome').setChromeOptions(Capabilities.chrome()).build();
    const windows = await driver.getAllWindowHandles();
    console.log('Windows: ', windows);
    driver.quit(); // Quit the driver after use to free up resources.
    return sessionId;
  } catch (error) {
    console.error(error);
  }
}

/**
 * Gets active or inactive sessions.
 * @param {Session} driver - The driver to use.
 * @param {boolean} inactive - Whether to get inactive sessions.
 * @returns {Array<string>} The IDs of the available sessions.
 */
async function getSessions(driver, inactive = false) {
  const sessions = readSessions();
  if (!driver) {
    driver = await getClient();
  }

  const activeSessions = sessions
   .filter((session) => Object.prototype.hasOwnProperty.call(session, 'id') && session.id!== null && session.id.length > 0);

  let availableSessions = activeSessions.map((session) => verifySession(null, session));

  if (inactive) {
    await Promise.all(availableSessions);
    availableSessions = availableSessions.filter((result) => result!== undefined);
  } else {
    await Promise.all(availableSessions);
    availableSessions = availableSessions.filter((result) => result!== undefined).map((result) => result);
  }

  return availableSessions.filter((session) => session!== undefined && session!== null).filter((value, index, array) => array.indexOf(value) === index);
}

module.exports = getSessions;

Code Breakdown

Imported Modules

The code imports the following modules:

Constants

Functions

verifySession

getSessions

Exports

The getSessions function is exported as a module export.