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.
npm run import -- "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
// 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
Code Breakdown
The code starts by importing two functions from external modules:
queryDom
and evaluateDom
from the 'select tree'
modulewalkTree
from the 'walk tree'
moduleconst {queryDom, evaluateDom} = importer.import('select tree')
const {walkTree} = importer.import('walk tree')
getAllQuery
FunctionThe getAllQuery
function is defined as an asynchronous function that takes three parameters:
driver
: an object with an executeScript
methodselect
: an object or string to be used in the queryDom
functionctx
: an optional context objectasync function getAllQuery(driver, select, ctx) {
...
}
The function executes a script in a browser context using the driver.executeScript
method. The script defines a main
function that:
window.evaluateDom
, window.walkTree
, and window.queryDom
are defined, and if not, sets them to the imported functions using eval
.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)
The getAllQuery
function is exported as a module.
module.exports = getAllQuery