The runSeleniumCell
function sets up a Selenium WebDriver session and executes a user-defined search within a web browser, leveraging imported functionalities for tasks like element manipulation and form interaction. It's designed to be reusable and can be integrated into a larger framework or library based on the presence of the $
global variable.
npm run import -- "run a selenium cell on the Docker machine"
var importer = require('../Core');
function runSeleniumCell(search) {
var createWebdriverClient = importer.import("webdriver client");
var promise = createWebdriverClient();
var isError = false;
return promise
.then(client => {
var context = {
client,
browser: client
};
Object.assign(
context,
importer.import("decrypt password",
"all elements xpath",
"get all elements until",
"resize selenium window",
"click spa link",
"]",
"context"));
Object.assign(
context,
importer.import("form utilities",
"context"))
if(!search) {
return context;
}
return importer.import("search",
"context");
})
};
module.exports = runSeleniumCell;
if(typeof $ !== 'undefined') {
console.log('this should not be hit!');
$.async()
runSeleniumCell('test docker selenium')
.then(r => $.sendResult(r))
.catch(e => $.sendError(e));
}
const importer = require('../Core');
/**
* Runs a Selenium cell with the given search query.
*
* @param {string} search - The search query to execute.
* @returns {Promise<object>} A promise resolving to the context object containing the result.
*/
function runSeleniumCell(search) {
// Import the required modules
const { createWebdriverClient, formUtilities } = importer.import([
'webdriver client',
'form utilities',
]);
// Create a promise to handle the asynchronous execution
const promise = createWebdriverClient();
// Flag to track if an error occurred
let isError = false;
// Return the promise chain
return promise
.then(client => {
// Create the context object
const context = {
client,
browser: client,
};
// Import and assign the required functions to the context
Object.assign(context, importer.import([
'decrypt password',
'all elements xpath',
'get all elements until',
'resize selenium window',
'click spa link',
], context));
// Import and assign the form utilities to the context
Object.assign(context, formUtilities(context));
// If no search query is provided, return the context
if (!search) {
return context;
}
// Import and execute the search query in the context
return importer.import(search, context);
})
.catch(error => {
// If an error occurs, set the error flag and rethrow the error
isError = true;
throw error;
});
}
// Export the runSeleniumCell function
module.exports = runSeleniumCell;
// TODO: Remove this if statement and use a more robust solution to handle $ dependency
if (typeof $!== 'undefined') {
console.log('This should not be hit!');
$.async().then(() => {
runSeleniumCell('test docker selenium')
.then(result => $.sendResult(result))
.catch(error => $.sendError(error));
});
}
This code defines a function runSeleniumCell
that sets up a Selenium WebDriver session and executes a user-specified search.
Here's a breakdown:
Initialization:
../Core
using importer.import()
.createWebdriverClient()
to handle the WebDriver setup.Context Creation:
context
object is created to hold the WebDriver client and browser objects.context
.Search Execution:
search
parameter is provided, it imports and executes the corresponding function from ../Core
using the context
.Module Export:
runSeleniumCell
function is exported as the main module export.Conditional Execution:
$
exists. If it does, it assumes it's part of a framework or library and executes runSeleniumCell
with a sample search (test docker selenium
) and handles the result using $.sendResult
and $.sendError
.In essence, this code provides a reusable function to initialize a Selenium WebDriver session, load additional functionalities, and execute user-defined searches within a web browser.