webdriver | list sessions | selenium query | Search

The code imports three functions (selectDom, evaluateDom, and walkTree) from separate modules and uses them in an asynchronous function called getAllXPath to execute a script on a web page. The getAllXPath function takes three parameters and returns the result of calling the selectDom function after making the evaluateDom and walkTree functions available on the web page.

Run example

npm run import -- "selenium select"

selenium select


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

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

module.exports = getAllXPath

What the code could have been:

// Import necessary functions from the'select tree' and 'walk tree' modules
const { selectDom, evaluateDom } = importer.import('select tree');
const { walkTree } = importer.import('walk tree');

/**
 * Retrieves all XPaths from a given DOM by executing a script on the driver.
 * 
 * @param {object} driver - The driver instance to execute the script on.
 * @param {string} select - The DOM selector.
 * @param {object} ctx - The context in which to select the DOM element (defaults to document).
 * @returns {Promise} A promise resolving to an array of XPaths.
 */
async function getAllXPath(driver, select, ctx = document) {
  // Ensure the necessary functions are available on the window object
  const { evaluateDomString, walkTreeString, selectDomString } = await Promise.all([
    evaluateDom.toString(),
    walkTree.toString(),
    selectDom.toString(),
  ]).then(([evaluateDomString, walkTreeString, selectDomString]) => (
    { evaluateDomString, walkTreeString, selectDomString }
  ));

  // Execute the script on the driver, injecting the necessary functions
  const result = await driver.executeScript(`
    const evaluateDom = ${evaluateDomString};
    const walkTree = ${walkTreeString};
    const selectDom = ${selectDomString};
    return selectDom("${select}", ${ctx === document? 'document' : ctx});
  `);

  // Return the result as an array of XPaths
  return result.walkTree();
}

module.exports = getAllXPath;

Code Breakdown

Importing Functions

const {selectDom, evaluateDom} = importer.import('select tree')
const {walkTree} = importer.import('walk tree')

getAllXPath Function

async function getAllXPath(driver, select, ctx) {
 ...
}

Executing Script on Web Page

return await driver.executeScript((
  function main(evaluateDomString, walkTreeString, selectDomString, select, ctx) {
   ...
  }), evaluateDom, walkTree, selectDom, select, ctx)

Export

module.exports = getAllXPath