Path: blob/trunk/javascript/selenium-webdriver/lib/http.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Defines an environment agnostic {@linkplain cmd.Executor19* command executor} that communicates with a remote end using JSON over HTTP.20*21* Clients should implement the {@link Client} interface, which is used by22* the {@link Executor} to send commands to the remote end.23*/2425'use strict'2627const path = require('node:path')28const cmd = require('./command')29const error = require('./error')30const logging = require('./logging')31const promise = require('./promise')32const { Session } = require('./session')33const webElement = require('./webelement')34const { isObject } = require('./util')3536const log_ = logging.getLogger(`${logging.Type.DRIVER}.http`)3738const getAttribute = requireAtom('get-attribute.js', '//javascript/selenium-webdriver/lib/atoms:get-attribute.js')39const isDisplayed = requireAtom('is-displayed.js', '//javascript/selenium-webdriver/lib/atoms:is-displayed.js')40const findElements = requireAtom('find-elements.js', '//javascript/selenium-webdriver/lib/atoms:find-elements.js')4142/**43* @param {string} module44* @param {string} bazelTarget45* @return {!Function}46*/47function requireAtom(module, bazelTarget) {48try {49return require('./atoms/' + module)50} catch (ex) {51try {52const file = bazelTarget.slice(2).replace(':', '/')53log_.log(`../../../bazel-bin/${file}`)54return require(path.resolve(`../../../bazel-bin/${file}`))55} catch (ex2) {56log_.severe(ex2)57throw Error(58`Failed to import atoms module ${module}. If running in dev mode, you` +59` need to run \`bazel build ${bazelTarget}\` from the project` +60`root: ${ex}`,61)62}63}64}6566/**67* Converts a headers map to a HTTP header block string.68* @param {!Map<string, string>} headers The map to convert.69* @return {string} The headers as a string.70*/71function headersToString(headers) {72const ret = []73headers.forEach(function (value, name) {74ret.push(`${name.toLowerCase()}: ${value}`)75})76return ret.join('\n')77}7879/**80* Represents a HTTP request message. This class is a "partial" request and only81* defines the path on the server to send a request to. It is each client's82* responsibility to build the full URL for the final request.83* @final84*/85class Request {86/**87* @param {string} method The HTTP method to use for the request.88* @param {string} path The path on the server to send the request to.89* @param {Object=} opt_data This request's non-serialized JSON payload data.90*/91constructor(method, path, opt_data) {92this.method = /** string */ method93this.path = /** string */ path94this.data = /** Object */ opt_data95this.headers = /** !Map<string, string> */ new Map([['Accept', 'application/json; charset=utf-8']])96}9798/** @override */99toString() {100let ret = `${this.method} ${this.path} HTTP/1.1\n`101ret += headersToString(this.headers) + '\n\n'102if (this.data) {103ret += JSON.stringify(this.data)104}105return ret106}107}108109/**110* Represents a HTTP response message.111* @final112*/113class Response {114/**115* @param {number} status The response code.116* @param {!Object<string>} headers The response headers. All header names117* will be converted to lowercase strings for consistent lookups.118* @param {string} body The response body.119*/120constructor(status, headers, body) {121this.status = /** number */ status122this.body = /** string */ body123this.headers = /** !Map<string, string>*/ new Map()124for (let header in headers) {125this.headers.set(header.toLowerCase(), headers[header])126}127}128129/** @override */130toString() {131let ret = `HTTP/1.1 ${this.status}\n${headersToString(this.headers)}\n\n`132if (this.body) {133ret += this.body134}135return ret136}137}138139/** @enum {!Function} */140const Atom = {141GET_ATTRIBUTE: getAttribute,142IS_DISPLAYED: isDisplayed,143FIND_ELEMENTS: findElements,144}145146function post(path) {147return resource('POST', path)148}149150function del(path) {151return resource('DELETE', path)152}153154function get(path) {155return resource('GET', path)156}157158function resource(method, path) {159return { method: method, path: path }160}161162/** @typedef {{method: string, path: string}} */163var CommandSpec164165/** @typedef {function(!cmd.Command): !cmd.Command} */166var CommandTransformer167168class InternalTypeError extends TypeError {}169170/**171* @param {!cmd.Command} command The initial command.172* @param {Atom} atom The name of the atom to execute.173* @param params174* @return {!Command} The transformed command to execute.175*/176function toExecuteAtomCommand(command, atom, name, ...params) {177if (typeof atom !== 'function') {178throw new InternalTypeError('atom is not a function: ' + typeof atom)179}180181return new cmd.Command(cmd.Name.EXECUTE_SCRIPT)182.setParameter('sessionId', command.getParameter('sessionId'))183.setParameter('script', `/* ${name} */return (${atom}).apply(null, arguments)`)184.setParameter(185'args',186params.map((param) => command.getParameter(param)),187)188}189190/** @const {!Map<string, (CommandSpec|CommandTransformer)>} */191const W3C_COMMAND_MAP = new Map([192// Session management.193[cmd.Name.NEW_SESSION, post('/session')],194[cmd.Name.QUIT, del('/session/:sessionId')],195196// Server status.197[cmd.Name.GET_SERVER_STATUS, get('/status')],198199// timeouts200[cmd.Name.GET_TIMEOUT, get('/session/:sessionId/timeouts')],201[cmd.Name.SET_TIMEOUT, post('/session/:sessionId/timeouts')],202203// Navigation.204[cmd.Name.GET_CURRENT_URL, get('/session/:sessionId/url')],205[cmd.Name.GET, post('/session/:sessionId/url')],206[cmd.Name.GO_BACK, post('/session/:sessionId/back')],207[cmd.Name.GO_FORWARD, post('/session/:sessionId/forward')],208[cmd.Name.REFRESH, post('/session/:sessionId/refresh')],209210// Page inspection.211[cmd.Name.GET_PAGE_SOURCE, get('/session/:sessionId/source')],212[cmd.Name.GET_TITLE, get('/session/:sessionId/title')],213214// Script execution.215[cmd.Name.EXECUTE_SCRIPT, post('/session/:sessionId/execute/sync')],216[cmd.Name.EXECUTE_ASYNC_SCRIPT, post('/session/:sessionId/execute/async')],217218// Frame selection.219[cmd.Name.SWITCH_TO_FRAME, post('/session/:sessionId/frame')],220[cmd.Name.SWITCH_TO_FRAME_PARENT, post('/session/:sessionId/frame/parent')],221222// Window management.223[cmd.Name.GET_CURRENT_WINDOW_HANDLE, get('/session/:sessionId/window')],224[cmd.Name.CLOSE, del('/session/:sessionId/window')],225[cmd.Name.SWITCH_TO_WINDOW, post('/session/:sessionId/window')],226[cmd.Name.SWITCH_TO_NEW_WINDOW, post('/session/:sessionId/window/new')],227[cmd.Name.GET_WINDOW_HANDLES, get('/session/:sessionId/window/handles')],228[cmd.Name.GET_WINDOW_RECT, get('/session/:sessionId/window/rect')],229[cmd.Name.SET_WINDOW_RECT, post('/session/:sessionId/window/rect')],230[cmd.Name.MAXIMIZE_WINDOW, post('/session/:sessionId/window/maximize')],231[cmd.Name.MINIMIZE_WINDOW, post('/session/:sessionId/window/minimize')],232[cmd.Name.FULLSCREEN_WINDOW, post('/session/:sessionId/window/fullscreen')],233234// Actions.235[cmd.Name.ACTIONS, post('/session/:sessionId/actions')],236[cmd.Name.CLEAR_ACTIONS, del('/session/:sessionId/actions')],237[cmd.Name.PRINT_PAGE, post('/session/:sessionId/print')],238239// Locating elements.240[cmd.Name.GET_ACTIVE_ELEMENT, get('/session/:sessionId/element/active')],241[cmd.Name.FIND_ELEMENT, post('/session/:sessionId/element')],242[cmd.Name.FIND_ELEMENTS, post('/session/:sessionId/elements')],243[244cmd.Name.FIND_ELEMENTS_RELATIVE,245(cmd) => {246return toExecuteAtomCommand(cmd, Atom.FIND_ELEMENTS, 'findElements', 'args')247},248],249[cmd.Name.FIND_CHILD_ELEMENT, post('/session/:sessionId/element/:id/element')],250[cmd.Name.FIND_CHILD_ELEMENTS, post('/session/:sessionId/element/:id/elements')],251// Element interaction.252[cmd.Name.GET_ELEMENT_TAG_NAME, get('/session/:sessionId/element/:id/name')],253[cmd.Name.GET_DOM_ATTRIBUTE, get('/session/:sessionId/element/:id/attribute/:name')],254[255cmd.Name.GET_ELEMENT_ATTRIBUTE,256(cmd) => {257return toExecuteAtomCommand(cmd, Atom.GET_ATTRIBUTE, 'getAttribute', 'id', 'name')258},259],260[cmd.Name.GET_ELEMENT_PROPERTY, get('/session/:sessionId/element/:id/property/:name')],261[cmd.Name.GET_ELEMENT_VALUE_OF_CSS_PROPERTY, get('/session/:sessionId/element/:id/css/:propertyName')],262[cmd.Name.GET_ELEMENT_RECT, get('/session/:sessionId/element/:id/rect')],263[cmd.Name.CLEAR_ELEMENT, post('/session/:sessionId/element/:id/clear')],264[cmd.Name.CLICK_ELEMENT, post('/session/:sessionId/element/:id/click')],265[cmd.Name.SEND_KEYS_TO_ELEMENT, post('/session/:sessionId/element/:id/value')],266[cmd.Name.GET_ELEMENT_TEXT, get('/session/:sessionId/element/:id/text')],267[cmd.Name.GET_COMPUTED_ROLE, get('/session/:sessionId/element/:id/computedrole')],268[cmd.Name.GET_COMPUTED_LABEL, get('/session/:sessionId/element/:id/computedlabel')],269[cmd.Name.IS_ELEMENT_ENABLED, get('/session/:sessionId/element/:id/enabled')],270[cmd.Name.IS_ELEMENT_SELECTED, get('/session/:sessionId/element/:id/selected')],271272[273cmd.Name.IS_ELEMENT_DISPLAYED,274(cmd) => {275return toExecuteAtomCommand(cmd, Atom.IS_DISPLAYED, 'isDisplayed', 'id')276},277],278279// Cookie management.280[cmd.Name.GET_ALL_COOKIES, get('/session/:sessionId/cookie')],281[cmd.Name.ADD_COOKIE, post('/session/:sessionId/cookie')],282[cmd.Name.DELETE_ALL_COOKIES, del('/session/:sessionId/cookie')],283[cmd.Name.GET_COOKIE, get('/session/:sessionId/cookie/:name')],284[cmd.Name.DELETE_COOKIE, del('/session/:sessionId/cookie/:name')],285286// Alert management.287[cmd.Name.ACCEPT_ALERT, post('/session/:sessionId/alert/accept')],288[cmd.Name.DISMISS_ALERT, post('/session/:sessionId/alert/dismiss')],289[cmd.Name.GET_ALERT_TEXT, get('/session/:sessionId/alert/text')],290[cmd.Name.SET_ALERT_TEXT, post('/session/:sessionId/alert/text')],291292// Screenshots.293[cmd.Name.SCREENSHOT, get('/session/:sessionId/screenshot')],294[cmd.Name.TAKE_ELEMENT_SCREENSHOT, get('/session/:sessionId/element/:id/screenshot')],295296// Shadow Root297[cmd.Name.GET_SHADOW_ROOT, get('/session/:sessionId/element/:id/shadow')],298[cmd.Name.FIND_ELEMENT_FROM_SHADOWROOT, post('/session/:sessionId/shadow/:id/element')],299[cmd.Name.FIND_ELEMENTS_FROM_SHADOWROOT, post('/session/:sessionId/shadow/:id/elements')],300// Log extensions.301[cmd.Name.GET_LOG, post('/session/:sessionId/se/log')],302[cmd.Name.GET_AVAILABLE_LOG_TYPES, get('/session/:sessionId/se/log/types')],303304// Server Extensions305[cmd.Name.UPLOAD_FILE, post('/session/:sessionId/se/file')],306307// Virtual Authenticator308[cmd.Name.ADD_VIRTUAL_AUTHENTICATOR, post('/session/:sessionId/webauthn/authenticator')],309[cmd.Name.REMOVE_VIRTUAL_AUTHENTICATOR, del('/session/:sessionId/webauthn/authenticator/:authenticatorId')],310[cmd.Name.ADD_CREDENTIAL, post('/session/:sessionId/webauthn/authenticator/:authenticatorId/credential')],311[cmd.Name.GET_CREDENTIALS, get('/session/:sessionId/webauthn/authenticator/:authenticatorId/credentials')],312[313cmd.Name.REMOVE_CREDENTIAL,314del('/session/:sessionId/webauthn/authenticator/:authenticatorId/credentials/:credentialId'),315],316[cmd.Name.REMOVE_ALL_CREDENTIALS, del('/session/:sessionId/webauthn/authenticator/:authenticatorId/credentials')],317[cmd.Name.SET_USER_VERIFIED, post('/session/:sessionId/webauthn/authenticator/:authenticatorId/uv')],318319[cmd.Name.GET_DOWNLOADABLE_FILES, get('/session/:sessionId/se/files')],320[cmd.Name.DOWNLOAD_FILE, post(`/session/:sessionId/se/files`)],321[cmd.Name.DELETE_DOWNLOADABLE_FILES, del(`/session/:sessionId/se/files`)],322323// Federated Credential Management Command324[cmd.Name.CANCEL_DIALOG, post(`/session/:sessionId/fedcm/canceldialog`)],325[cmd.Name.SELECT_ACCOUNT, post(`/session/:sessionId/fedcm/selectaccount`)],326[cmd.Name.GET_FEDCM_TITLE, get(`/session/:sessionId/fedcm/gettitle`)],327[cmd.Name.GET_FEDCM_DIALOG_TYPE, get('/session/:sessionId/fedcm/getdialogtype')],328[cmd.Name.SET_DELAY_ENABLED, post(`/session/:sessionId/fedcm/setdelayenabled`)],329[cmd.Name.RESET_COOLDOWN, post(`/session/:sessionId/fedcm/resetcooldown`)],330[cmd.Name.CLICK_DIALOG_BUTTON, post(`/session/:sessionId/fedcm/clickdialogbutton`)],331[cmd.Name.GET_ACCOUNTS, get(`/session/:sessionId/fedcm/accountlist`)],332])333334/**335* Handles sending HTTP messages to a remote end.336*337* @interface338*/339class Client {340/**341* Sends a request to the server. The client will automatically follow any342* redirects returned by the server, fulfilling the returned promise with the343* final response.344*345* @param {!Request} httpRequest The request to send.346* @return {!Promise<Response>} A promise that will be fulfilled with the347* server's response.348*/349send(httpRequest) {}350}351352/**353* @param {Map<string, CommandSpec>} customCommands354* A map of custom command definitions.355* @param {!cmd.Command} command The command to resolve.356* @return {!Request} A promise that will resolve with the357* command to execute.358*/359function buildRequest(customCommands, command) {360log_.finest(() => `Translating command: ${command.getName()}`)361let spec = customCommands && customCommands.get(command.getName())362if (spec) {363return toHttpRequest(spec)364}365366spec = W3C_COMMAND_MAP.get(command.getName())367if (typeof spec === 'function') {368log_.finest(() => `Transforming command for W3C: ${command.getName()}`)369let newCommand = spec(command)370return buildRequest(customCommands, newCommand)371} else if (spec) {372return toHttpRequest(spec)373}374throw new error.UnknownCommandError('Unrecognized command: ' + command.getName())375376/**377* @param {CommandSpec} resource378* @return {!Request}379*/380function toHttpRequest(resource) {381log_.finest(() => `Building HTTP request: ${JSON.stringify(resource)}`)382let parameters = command.getParameters()383let path = buildPath(resource.path, parameters)384return new Request(resource.method, path, parameters)385}386}387388const CLIENTS = /** !WeakMap<!Executor, !(Client|IThenable<!Client>)> */ new WeakMap()389390/**391* A command executor that communicates with the server using JSON over HTTP.392*393* By default, each instance of this class will use the legacy wire protocol394* from [Selenium project][json]. The executor will automatically switch to the395* [W3C wire protocol][w3c] if the remote end returns a compliant response to396* a new session command.397*398* [json]: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol399* [w3c]: https://w3c.github.io/webdriver/webdriver-spec.html400*401* @implements {cmd.Executor}402*/403class Executor {404/**405* @param {!(Client|IThenable<!Client>)} client The client to use for sending406* requests to the server, or a promise-like object that will resolve407* to the client.408*/409constructor(client) {410CLIENTS.set(this, client)411412/** @private {Map<string, CommandSpec>} */413this.customCommands_ = null414415/** @private {!logging.Logger} */416this.log_ = logging.getLogger(`${logging.Type.DRIVER}.http.Executor`)417}418419/**420* Defines a new command for use with this executor. When a command is sent,421* the {@code path} will be preprocessed using the command's parameters; any422* path segments prefixed with ":" will be replaced by the parameter of the423* same name. For example, given "/person/:name" and the parameters424* "{name: 'Bob'}", the final command path will be "/person/Bob".425*426* @param {string} name The command name.427* @param {string} method The HTTP method to use when sending this command.428* @param {string} path The path to send the command to, relative to429* the WebDriver server's command root and of the form430* "/path/:variable/segment".431*/432defineCommand(name, method, path) {433if (!this.customCommands_) {434this.customCommands_ = new Map()435}436this.customCommands_.set(name, { method, path })437}438439/** @override */440async execute(command) {441let request = buildRequest(this.customCommands_, command)442this.log_.finer(() => `>>> ${request.method} ${request.path}`)443444let client = CLIENTS.get(this)445if (promise.isPromise(client)) {446client = await client447CLIENTS.set(this, client)448}449450let response = await client.send(request)451this.log_.finer(() => `>>>\n${request}\n<<<\n${response}`)452453let httpResponse = /** @type {!Response} */ (response)454455let { isW3C, value } = parseHttpResponse(command, httpResponse)456457if (command.getName() === cmd.Name.NEW_SESSION) {458if (!value || !value.sessionId) {459throw new error.WebDriverError(`Unable to parse new session response: ${response.body}`)460}461462// The remote end is a W3C compliant server if there is no `status`463// field in the response.464if (command.getName() === cmd.Name.NEW_SESSION) {465this.w3c = this.w3c || isW3C466}467468// No implementations use the `capabilities` key yet...469let capabilities = value.capabilities || value.value470return new Session(/** @type {{sessionId: string}} */ (value).sessionId, capabilities)471}472473return typeof value === 'undefined' ? null : value474}475}476477/**478* @param {string} str .479* @return {?} .480*/481function tryParse(str) {482try {483return JSON.parse(str)484/*eslint no-unused-vars: "off"*/485} catch (ignored) {486// Do nothing.487}488}489490/**491* Callback used to parse {@link Response} objects from a492* {@link HttpClient}.493*494* @param {!cmd.Command} command The command the response is for.495* @param {!Response} httpResponse The HTTP response to parse.496* @return {{isW3C: boolean, value: ?}} An object describing the parsed497* response. This object will have two fields: `isW3C` indicates whether498* the response looks like it came from a remote end that conforms with the499* W3C WebDriver spec, and `value`, the actual response value.500* @throws {WebDriverError} If the HTTP response is an error.501*/502function parseHttpResponse(command, httpResponse) {503if (httpResponse.status < 200) {504// This should never happen, but throw the raw response so users report it.505throw new error.WebDriverError(`Unexpected HTTP response:\n${httpResponse}`)506}507508let parsed = tryParse(httpResponse.body)509510if (parsed && typeof parsed === 'object') {511let value = parsed.value512let isW3C = isObject(value) && typeof parsed.status === 'undefined'513514if (!isW3C) {515error.checkLegacyResponse(parsed)516517// Adjust legacy new session responses to look like W3C to simplify518// later processing.519if (command.getName() === cmd.Name.NEW_SESSION) {520value = parsed521}522} else if (httpResponse.status > 399) {523error.throwDecodedError(value)524}525526return { isW3C, value }527}528529if (parsed !== undefined) {530return { isW3C: false, value: parsed }531}532533let value = httpResponse.body.replace(/\r\n/g, '\n')534535// 404 represents an unknown command; anything else > 399 is a generic unknown536// error.537if (httpResponse.status === 404) {538throw new error.UnsupportedOperationError(command.getName() + ': ' + value)539} else if (httpResponse.status >= 400) {540throw new error.WebDriverError(value)541}542543return { isW3C: false, value: value || null }544}545546/**547* Builds a fully qualified path using the given set of command parameters. Each548* path segment prefixed with ':' will be replaced by the value of the549* corresponding parameter. All parameters spliced into the path will be550* removed from the parameter map.551* @param {string} path The original resource path.552* @param {!Object<*>} parameters The parameters object to splice into the path.553* @return {string} The modified path.554*/555function buildPath(path, parameters) {556let pathParameters = path.match(/\/:(\w+)\b/g)557if (pathParameters) {558for (let i = 0; i < pathParameters.length; ++i) {559let key = pathParameters[i].substring(2) // Trim the /:560if (key in parameters) {561let value = parameters[key]562if (webElement.isId(value)) {563// When inserting a WebElement into the URL, only use its ID value,564// not the full JSON.565value = webElement.extractId(value)566}567path = path.replace(pathParameters[i], '/' + value)568delete parameters[key]569} else {570throw new error.InvalidArgumentError('Missing required parameter: ' + key)571}572}573}574return path575}576577// PUBLIC API578579module.exports = {580Executor,581Client,582Request,583Response,584// Exported for testing.585buildPath,586}587588589