The code defines a set of commands for interacting with a web browser using Selenium WebDriver, including launching apps, setting network conditions, and sending DevTools commands. The commands are configured using the configureExecutor
function, which takes an executor instance and a vendor prefix as arguments to customize the command URLs for specific vendors.
npm run import -- "selenium executor"
const _http = require('selenium-webdriver/http');
const Command = {
LAUNCH_APP: 'launchApp',
GET_NETWORK_CONDITIONS: 'getNetworkConditions',
SET_NETWORK_CONDITIONS: 'setNetworkConditions',
DELETE_NETWORK_CONDITIONS: 'deleteNetworkConditions',
SEND_DEVTOOLS_COMMAND: 'sendDevToolsCommand',
SEND_AND_GET_DEVTOOLS_COMMAND: 'sendAndGetDevToolsCommand',
SET_PERMISSION: 'setPermission',
GET_CAST_SINKS: 'getCastSinks',
SET_CAST_SINK_TO_USE: 'setCastSinkToUse',
START_CAST_DESKTOP_MIRRORING: 'startDesktopMirroring',
START_CAST_TAB_MIRRORING: 'setCastTabMirroring',
GET_CAST_ISSUE_MESSAGE: 'getCastIssueMessage',
STOP_CASTING: 'stopCasting',
}
function configureExecutor(executor, vendorPrefix) {
executor.defineCommand(Command.LAUNCH_APP, 'POST', '/session/:sessionId/chromium/launch_app')
executor.defineCommand(Command.GET_NETWORK_CONDITIONS, 'GET', '/session/:sessionId/chromium/network_conditions')
executor.defineCommand(Command.SET_NETWORK_CONDITIONS, 'POST', '/session/:sessionId/chromium/network_conditions')
executor.defineCommand(Command.DELETE_NETWORK_CONDITIONS, 'DELETE', '/session/:sessionId/chromium/network_conditions')
executor.defineCommand(Command.SEND_DEVTOOLS_COMMAND, 'POST', '/session/:sessionId/chromium/send_command')
executor.defineCommand(
Command.SEND_AND_GET_DEVTOOLS_COMMAND,
'POST',
'/session/:sessionId/chromium/send_command_and_get_result',
)
executor.defineCommand(Command.SET_PERMISSION, 'POST', '/session/:sessionId/permissions')
executor.defineCommand(Command.GET_CAST_SINKS, 'GET', `/session/:sessionId/${vendorPrefix}/cast/get_sinks`)
executor.defineCommand(
Command.SET_CAST_SINK_TO_USE,
'POST',
`/session/:sessionId/${vendorPrefix}/cast/set_sink_to_use`,
)
executor.defineCommand(
Command.START_CAST_DESKTOP_MIRRORING,
'POST',
`/session/:sessionId/${vendorPrefix}/cast/start_desktop_mirroring`,
)
executor.defineCommand(
Command.START_CAST_TAB_MIRRORING,
'POST',
`/session/:sessionId/${vendorPrefix}/cast/start_tab_mirroring`,
)
executor.defineCommand(
Command.GET_CAST_ISSUE_MESSAGE,
'GET',
`/session/:sessionId/${vendorPrefix}/cast/get_issue_message`,
)
executor.defineCommand(Command.STOP_CASTING, 'POST', `/session/:sessionId/${vendorPrefix}/cast/stop_casting`)
}
function createExecutor(url, vendorPrefix) {
const agent = new _http.Agent({ keepAlive: true })
const client = url.then((url) => new _http.HttpClient(url, agent))
const executor = new _http.Executor(client)
configureExecutor(executor, vendorPrefix)
return executor
}
module.exports = createExecutor
const _http = require('selenium-webdriver/http');
// Enum for command types
const Command = Object.freeze({
LAUNCH_APP: 'launchApp',
GET_NETWORK_CONDITIONS: 'getNetworkConditions',
SET_NETWORK_CONDITIONS:'setNetworkConditions',
DELETE_NETWORK_CONDITIONS: 'deleteNetworkConditions',
SEND_DEVTOOLS_COMMAND:'sendDevToolsCommand',
SEND_AND_GET_DEVTOOLS_COMMAND:'sendAndGetDevToolsCommand',
SET_PERMISSION:'setPermission',
GET_CAST_SINKS: 'getCastSinks',
SET_CAST_SINK_TO_USE:'setCastSinkToUse',
START_CAST_DESKTOP_MIRRORING:'startDesktopMirroring',
START_CAST_TAB_MIRRORING:'setCastTabMirroring',
GET_CAST_ISSUE_MESSAGE: 'getCastIssueMessage',
STOP_CASTING:'stopCasting',
});
// HTTP client factory function
function createHttpClient(url, vendorPrefix) {
const agent = new _http.Agent({ keepAlive: true });
const client = new _http.HttpClient(url, agent);
return client;
}
// Executor factory function
function createExecutor(url, vendorPrefix) {
const client = createHttpClient(url, vendorPrefix);
const executor = new _http.Executor(client);
return configureExecutor(executor, vendorPrefix);
}
// Executor configuration function
function configureExecutor(executor, vendorPrefix) {
// Define commands
executor.defineCommand(Command.LAUNCH_APP, 'POST', '/session/:sessionId/chromium/launch_app');
executor.defineCommand(Command.GET_NETWORK_CONDITIONS, 'GET', '/session/:sessionId/chromium/network_conditions');
executor.defineCommand(Command.SET_NETWORK_CONDITIONS, 'POST', '/session/:sessionId/chromium/network_conditions');
executor.defineCommand(Command.DELETE_NETWORK_CONDITIONS, 'DELETE', '/session/:sessionId/chromium/network_conditions');
executor.defineCommand(Command.SEND_DEVTOOLS_COMMAND, 'POST', '/session/:sessionId/chromium/send_command');
executor.defineCommand(
Command.SEND_AND_GET_DEVTOOLS_COMMAND,
'POST',
'/session/:sessionId/chromium/send_command_and_get_result',
);
executor.defineCommand(Command.SET_PERMISSION, 'POST', '/session/:sessionId/permissions');
executor.defineCommand(
Command.GET_CAST_SINKS,
'GET',
`/session/:sessionId/${vendorPrefix}/cast/get_sinks`,
);
executor.defineCommand(
Command.SET_CAST_SINK_TO_USE,
'POST',
`/session/:sessionId/${vendorPrefix}/cast/set_sink_to_use`,
);
executor.defineCommand(
Command.START_CAST_DESKTOP_MIRRORING,
'POST',
`/session/:sessionId/${vendorPrefix}/cast/start_desktop_mirroring`,
);
executor.defineCommand(
Command.START_CAST_TAB_MIRRORING,
'POST',
`/session/:sessionId/${vendorPrefix}/cast/start_tab_mirroring`,
);
executor.defineCommand(
Command.GET_CAST_ISSUE_MESSAGE,
'GET',
`/session/:sessionId/${vendorPrefix}/cast/get_issue_message`,
);
executor.defineCommand(Command.STOP_CASTING, 'POST', `/session/:sessionId/${vendorPrefix}/cast/stop_casting`);
return executor;
}
module.exports = createExecutor;
Code Breakdown
This is a JavaScript code snippet that defines a set of commands for interacting with a web browser using Selenium WebDriver.
The code starts by importing the http
module from selenium-webdriver
:
const _http = require('selenium-webdriver/http');
A set of constants is defined to represent different commands that can be executed on the browser:
const Command = {
//...
}
These constants are used to map a command name to a specific command value.
The configureExecutor
function is defined to configure an executor instance with the defined commands:
function configureExecutor(executor, vendorPrefix) {
//...
}
The function takes two arguments:
executor
: an instance of the executor to be configuredvendorPrefix
: a prefix to be used in the command URLs (e.g. chromium
or cast
)The function defines each command using the executor.defineCommand
method, specifying the command name, HTTP method, and URL.
The following commands are defined:
LAUNCH_APP
: Launch an appGET_NETWORK_CONDITIONS
: Get network conditionsSET_NETWORK_CONDITIONS
: Set network conditionsDELETE_NETWORK_CONDITIONS
: Delete network conditionsSEND_DEVTOOLS_COMMAND
: Send DevTools commandSEND_AND_GET_DEVTOOLS_COMMAND
: Send and get DevTools commandSET_PERMISSION
: Set permissionGET_CAST_SINKS
: Get cast sinksSET_CAST_SINK_TO_USE
: Set cast sink to useSTART_CAST_DESKTOP_MIRRORING
: Start cast desktop mirroringSTART_CAST_TAB_MIRRORING
: Start cast tab mirroringGET_CAST_ISSUE_MESSAGE
: Get cast issue messageSTOP_CASTING
: Stop castingEach command is defined with a specific HTTP method (e.g. POST
, GET
, DELETE
) and a URL that includes a :sessionId
placeholder for the session ID.
The vendorPrefix
argument is used to customize the command URLs for specific vendors (e.g. chromium
or cast
).