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.
npm run import -- "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
// 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;
const {selectDom, evaluateDom} = importer.import('select tree')
const {walkTree} = importer.import('walk tree')
selectDom
and evaluateDom
, are imported from a module named'select tree' using the importer.import
function.walkTree
, is imported from a module named 'walk tree' using the importer.import
function.async function getAllXPath(driver, select, ctx) {
...
}
getAllXPath
is an asynchronous function that takes three parameters:
driver
: an object representing a driver (likely a web driver).select
: a string or object representing the DOM element to select.ctx
: an optional object representing the context in which to select the element.return await driver.executeScript((
function main(evaluateDomString, walkTreeString, selectDomString, select, ctx) {
...
}), evaluateDom, walkTree, selectDom, select, ctx)
driver.executeScript
function is used to execute a script on the web page represented by the driver
object.main
that takes five parameters:
evaluateDomString
, walkTreeString
, and selectDomString
: strings representing the source code of the functions evaluateDom
, walkTree
, and selectDom
, respectively.select
and ctx
: the select
parameter passed to getAllXPath
and the ctx
parameter passed to getAllXPath
, respectively.main
function:
evaluateDom
, walkTree
, and selectDom
functions are assigned to the window
object if they do not already exist.selectDom
function is called with the select
and ctx
parameters and the result is returned.module.exports = getAllXPath
getAllXPath
function is exported as a module.