webdriver | selenium select | Cell 8 | Search

The getAllQuery function is defined as an asynchronous function that executes a script in a browser context using the driver.executeScript method, which involves importing and utilizing external functions for querying and evaluating the DOM. The function takes three parameters - driver, select, and an optional ctx object - and returns the result of executing the queryDom function.

Run example

npm run import -- "selenium query"

selenium query


const {queryDom, evaluateDom} = importer.import("select tree")
const {walkTree} = importer.import("walk tree")

async function getAllQuery(driver, select, ctx) {
  return await driver.executeScript((
    function main(evaluateDomString, walkTreeString, queryDomString, select, ctx) {
      if(!window.evaluateDom)
        window.evaluateDom = eval('(' + evaluateDomString + ')')
      if(!window.walkTree)
        window.walkTree = eval('(' + walkTreeString + ')')
      if(!window.queryDom)
        window.queryDom = eval('(' + queryDomString + ')')
      let result = queryDom(select, ctx || document)
      return result;
    }), evaluateDom, walkTree, queryDom, select, ctx)
}

module.exports = getAllQuery

What the code could have been:

// Import required functions from separate modules
const { queryDom, evaluateDom } = require('./select-tree');
const { walkTree } = require('./walk-tree');

/**
 * Execute a query on the DOM using the provided driver and context.
 * 
 * @param {object} driver - The driver to execute the script on.
 * @param {string|object} select - The query or object to query the DOM with.
 * @param {object} ctx - The context to query the DOM with.
 * @returns {Promise} - The result of the query.
 */
async function getAllQuery(driver, select, ctx) {
  // Check if the driver has a executeScript method
  if (!driver.executeScript) {
    throw new Error('Driver does not support executeScript method');
  }

  try {
    // Execute the script on the driver
    const result = await driver.executeScript(`
      // Check if the functions are already defined
      if (typeof window.evaluateDom!== 'function' && typeof window.evaluateDom ==='string') {
        // If not, define them using eval
        window.evaluateDom = eval('(' + ${evaluateDom} + ')');
      }
      if (typeof window.walkTree!== 'function' && typeof window.walkTree ==='string') {
        window.walkTree = eval('(' + ${walkTree} + ')');
      }
      if (typeof window.queryDom!== 'function' && typeof window.queryDom ==='string') {
        window.queryDom = eval('(' + ${queryDom} + ')');
      }

      // Query the DOM and return the result
      return window.queryDom(${select}, ${ctx || 'document'});
    `);

    return result;
  } catch (error) {
    // Log the error and rethrow it
    console.error('Error executing script:', error);
    throw error;
  }
}

module.exports = getAllQuery;

Code Breakdown

Importing Dependencies

The code starts by importing two functions from external modules:

  • queryDom and evaluateDom from the 'select tree' module
  • walkTree from the 'walk tree' module
const {queryDom, evaluateDom} = importer.import('select tree')
const {walkTree} = importer.import('walk tree')

Defining the getAllQuery Function

The getAllQuery function is defined as an asynchronous function that takes three parameters:

  • driver: an object with an executeScript method
  • select: an object or string to be used in the queryDom function
  • ctx: an optional context object
async function getAllQuery(driver, select, ctx) {
 ...
}

Executing Script in Browser Context

The function executes a script in a browser context using the driver.executeScript method. The script defines a main function that:

  • Checks if window.evaluateDom, window.walkTree, and window.queryDom are defined, and if not, sets them to the imported functions using eval.
  • Calls the queryDom function with the provided select and ctx (or document if ctx is not provided), and returns the result.
driver.executeScript((
  function main(evaluateDomString, walkTreeString, queryDomString, select, ctx) {
   ...
    let result = queryDom(select, ctx || document)
    return result;
  }), evaluateDom, walkTree, queryDom, select, ctx)

Exporting the Function

The getAllQuery function is exported as a module.

module.exports = getAllQuery