webdriver | close all windows | list sessions | Search

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.

Run example

npm run import -- "selenium executor"

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

What the code could have been:

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.

Importing Dependencies

The code starts by importing the http module from selenium-webdriver:

const _http = require('selenium-webdriver/http');

Defining Commands

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.

Configuring Executor

The configureExecutor function is defined to configure an executor instance with the defined commands:

function configureExecutor(executor, vendorPrefix) {
  //...
}

The function takes two arguments:

The function defines each command using the executor.defineCommand method, specifying the command name, HTTP method, and URL.

Command Definitions

The following commands are defined:

  1. LAUNCH_APP: Launch an app
  2. GET_NETWORK_CONDITIONS: Get network conditions
  3. SET_NETWORK_CONDITIONS: Set network conditions
  4. DELETE_NETWORK_CONDITIONS: Delete network conditions
  5. SEND_DEVTOOLS_COMMAND: Send DevTools command
  6. SEND_AND_GET_DEVTOOLS_COMMAND: Send and get DevTools command
  7. SET_PERMISSION: Set permission
  8. GET_CAST_SINKS: Get cast sinks
  9. SET_CAST_SINK_TO_USE: Set cast sink to use
  10. START_CAST_DESKTOP_MIRRORING: Start cast desktop mirroring
  11. START_CAST_TAB_MIRRORING: Start cast tab mirroring
  12. GET_CAST_ISSUE_MESSAGE: Get cast issue message
  13. STOP_CASTING: Stop casting

Each 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.

Vendor Prefix

The vendorPrefix argument is used to customize the command URLs for specific vendors (e.g. chromium or cast).