2016-05-15 11:47:21 -07:00
|
|
|
// Licensed to the Software Freedom Conservancy (SFC) under one
|
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
|
// distributed with this work for additional information
|
|
|
|
|
// regarding copyright ownership. The SFC licenses this file
|
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
// KIND, either express or implied. See the License for the
|
|
|
|
|
// specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @fileoverview Defines an environment agnostic {@linkplain cmd.Executor
|
|
|
|
|
* command executor} that communicates with a remote end using JSON over HTTP.
|
|
|
|
|
*
|
|
|
|
|
* Clients should implement the {@link Client} interface, which is used by
|
|
|
|
|
* the {@link Executor} to send commands to the remote end.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
'use strict'
|
2016-05-15 11:47:21 -07:00
|
|
|
|
2024-04-16 01:50:58 +05:30
|
|
|
const path = require('node:path')
|
2020-08-03 17:56:31 +03:00
|
|
|
const cmd = require('./command')
|
|
|
|
|
const error = require('./error')
|
|
|
|
|
const logging = require('./logging')
|
|
|
|
|
const promise = require('./promise')
|
|
|
|
|
const { Session } = require('./session')
|
2022-08-11 22:07:00 +02:00
|
|
|
const webElement = require('./webelement')
|
2022-08-28 11:26:01 +03:00
|
|
|
const { isObject } = require('./util')
|
2016-05-15 11:47:21 -07:00
|
|
|
|
2023-10-11 06:54:01 -04:00
|
|
|
const log_ = logging.getLogger(`${logging.Type.DRIVER}.http`)
|
|
|
|
|
|
2025-03-25 14:28:37 +01:00
|
|
|
const getAttribute = requireAtom('get-attribute.js', '//javascript/selenium-webdriver/lib/atoms:get-attribute.js')
|
|
|
|
|
const isDisplayed = requireAtom('is-displayed.js', '//javascript/selenium-webdriver/lib/atoms:is-displayed.js')
|
|
|
|
|
const findElements = requireAtom('find-elements.js', '//javascript/selenium-webdriver/lib/atoms:find-elements.js')
|
2019-05-05 14:49:34 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} module
|
|
|
|
|
* @param {string} bazelTarget
|
|
|
|
|
* @return {!Function}
|
|
|
|
|
*/
|
|
|
|
|
function requireAtom(module, bazelTarget) {
|
2016-11-21 12:02:38 -08:00
|
|
|
try {
|
2020-08-03 17:56:31 +03:00
|
|
|
return require('./atoms/' + module)
|
2016-11-21 12:02:38 -08:00
|
|
|
} catch (ex) {
|
2019-05-05 14:49:34 -07:00
|
|
|
try {
|
2020-08-03 17:56:31 +03:00
|
|
|
const file = bazelTarget.slice(2).replace(':', '/')
|
2023-10-11 06:54:01 -04:00
|
|
|
log_.log(`../../../bazel-bin/${file}`)
|
2020-08-03 17:56:31 +03:00
|
|
|
return require(path.resolve(`../../../bazel-bin/${file}`))
|
2019-05-05 14:49:34 -07:00
|
|
|
} catch (ex2) {
|
2023-11-06 10:27:49 +01:00
|
|
|
log_.severe(ex2)
|
2019-05-05 14:49:34 -07:00
|
|
|
throw Error(
|
2020-08-03 17:56:31 +03:00
|
|
|
`Failed to import atoms module ${module}. If running in dev mode, you` +
|
2022-01-17 08:32:57 -05:00
|
|
|
` need to run \`bazel build ${bazelTarget}\` from the project` +
|
2024-02-07 16:07:24 +00:00
|
|
|
`root: ${ex}`,
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
2019-05-05 14:49:34 -07:00
|
|
|
}
|
2016-11-21 12:02:38 -08:00
|
|
|
}
|
2019-05-05 14:49:34 -07:00
|
|
|
}
|
2016-11-21 12:02:38 -08:00
|
|
|
|
2016-05-15 11:47:21 -07:00
|
|
|
/**
|
|
|
|
|
* Converts a headers map to a HTTP header block string.
|
|
|
|
|
* @param {!Map<string, string>} headers The map to convert.
|
|
|
|
|
* @return {string} The headers as a string.
|
|
|
|
|
*/
|
|
|
|
|
function headersToString(headers) {
|
2021-02-08 20:09:38 +02:00
|
|
|
const ret = []
|
2020-08-03 17:56:31 +03:00
|
|
|
headers.forEach(function (value, name) {
|
|
|
|
|
ret.push(`${name.toLowerCase()}: ${value}`)
|
|
|
|
|
})
|
|
|
|
|
return ret.join('\n')
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a HTTP request message. This class is a "partial" request and only
|
|
|
|
|
* defines the path on the server to send a request to. It is each client's
|
|
|
|
|
* responsibility to build the full URL for the final request.
|
|
|
|
|
* @final
|
|
|
|
|
*/
|
|
|
|
|
class Request {
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} method The HTTP method to use for the request.
|
|
|
|
|
* @param {string} path The path on the server to send the request to.
|
|
|
|
|
* @param {Object=} opt_data This request's non-serialized JSON payload data.
|
|
|
|
|
*/
|
|
|
|
|
constructor(method, path, opt_data) {
|
2020-08-03 17:56:31 +03:00
|
|
|
this.method = /** string */ method
|
|
|
|
|
this.path = /** string */ path
|
|
|
|
|
this.data = /** Object */ opt_data
|
2024-02-07 16:07:24 +00:00
|
|
|
this.headers = /** !Map<string, string> */ new Map([['Accept', 'application/json; charset=utf-8']])
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @override */
|
|
|
|
|
toString() {
|
2020-08-03 17:56:31 +03:00
|
|
|
let ret = `${this.method} ${this.path} HTTP/1.1\n`
|
|
|
|
|
ret += headersToString(this.headers) + '\n\n'
|
2016-05-15 11:47:21 -07:00
|
|
|
if (this.data) {
|
2020-08-03 17:56:31 +03:00
|
|
|
ret += JSON.stringify(this.data)
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
return ret
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a HTTP response message.
|
|
|
|
|
* @final
|
|
|
|
|
*/
|
|
|
|
|
class Response {
|
|
|
|
|
/**
|
|
|
|
|
* @param {number} status The response code.
|
|
|
|
|
* @param {!Object<string>} headers The response headers. All header names
|
|
|
|
|
* will be converted to lowercase strings for consistent lookups.
|
|
|
|
|
* @param {string} body The response body.
|
|
|
|
|
*/
|
|
|
|
|
constructor(status, headers, body) {
|
2020-08-03 17:56:31 +03:00
|
|
|
this.status = /** number */ status
|
|
|
|
|
this.body = /** string */ body
|
|
|
|
|
this.headers = /** !Map<string, string>*/ new Map()
|
2016-05-15 11:47:21 -07:00
|
|
|
for (let header in headers) {
|
2020-08-03 17:56:31 +03:00
|
|
|
this.headers.set(header.toLowerCase(), headers[header])
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @override */
|
|
|
|
|
toString() {
|
2020-08-03 17:56:31 +03:00
|
|
|
let ret = `HTTP/1.1 ${this.status}\n${headersToString(this.headers)}\n\n`
|
2016-05-15 11:47:21 -07:00
|
|
|
if (this.body) {
|
2020-08-03 17:56:31 +03:00
|
|
|
ret += this.body
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
return ret
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-21 12:02:38 -08:00
|
|
|
/** @enum {!Function} */
|
2016-11-02 19:18:47 -07:00
|
|
|
const Atom = {
|
2016-11-21 12:02:38 -08:00
|
|
|
GET_ATTRIBUTE: getAttribute,
|
2020-04-25 09:10:55 +01:00
|
|
|
IS_DISPLAYED: isDisplayed,
|
|
|
|
|
FIND_ELEMENTS: findElements,
|
2020-08-03 17:56:31 +03:00
|
|
|
}
|
2016-11-02 19:18:47 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
function post(path) {
|
|
|
|
|
return resource('POST', path)
|
|
|
|
|
}
|
2024-03-08 12:13:00 +00:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
function del(path) {
|
|
|
|
|
return resource('DELETE', path)
|
|
|
|
|
}
|
2024-03-08 12:13:00 +00:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
function get(path) {
|
|
|
|
|
return resource('GET', path)
|
|
|
|
|
}
|
2024-03-08 12:13:00 +00:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
function resource(method, path) {
|
|
|
|
|
return { method: method, path: path }
|
|
|
|
|
}
|
2016-05-15 11:47:21 -07:00
|
|
|
|
2016-11-02 19:18:47 -07:00
|
|
|
/** @typedef {{method: string, path: string}} */
|
2024-04-16 01:50:58 +05:30
|
|
|
var CommandSpec
|
2016-11-02 19:18:47 -07:00
|
|
|
|
2017-10-29 20:13:40 -07:00
|
|
|
/** @typedef {function(!cmd.Command): !cmd.Command} */
|
2024-04-16 01:50:58 +05:30
|
|
|
var CommandTransformer
|
2016-11-02 19:18:47 -07:00
|
|
|
|
2021-11-27 11:13:27 -05:00
|
|
|
class InternalTypeError extends TypeError {}
|
2016-11-21 12:02:38 -08:00
|
|
|
|
2016-11-02 19:18:47 -07:00
|
|
|
/**
|
|
|
|
|
* @param {!cmd.Command} command The initial command.
|
|
|
|
|
* @param {Atom} atom The name of the atom to execute.
|
2021-11-30 16:27:02 +05:30
|
|
|
* @param params
|
|
|
|
|
* @return {!Command} The transformed command to execute.
|
2016-11-02 19:18:47 -07:00
|
|
|
*/
|
2023-01-02 16:13:27 -06:00
|
|
|
function toExecuteAtomCommand(command, atom, name, ...params) {
|
2017-10-29 20:13:40 -07:00
|
|
|
if (typeof atom !== 'function') {
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new InternalTypeError('atom is not a function: ' + typeof atom)
|
2017-10-29 20:13:40 -07:00
|
|
|
}
|
2016-11-21 12:02:38 -08:00
|
|
|
|
2017-10-29 20:13:40 -07:00
|
|
|
return new cmd.Command(cmd.Name.EXECUTE_SCRIPT)
|
2020-08-03 17:56:31 +03:00
|
|
|
.setParameter('sessionId', command.getParameter('sessionId'))
|
2024-02-07 16:07:24 +00:00
|
|
|
.setParameter('script', `/* ${name} */return (${atom}).apply(null, arguments)`)
|
2020-08-03 17:56:31 +03:00
|
|
|
.setParameter(
|
|
|
|
|
'args',
|
2024-02-07 16:07:24 +00:00
|
|
|
params.map((param) => command.getParameter(param)),
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
2016-11-02 19:18:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @const {!Map<string, (CommandSpec|CommandTransformer)>} */
|
2016-05-15 11:47:21 -07:00
|
|
|
const W3C_COMMAND_MAP = new Map([
|
2017-12-27 15:12:04 -08:00
|
|
|
// Session management.
|
|
|
|
|
[cmd.Name.NEW_SESSION, post('/session')],
|
|
|
|
|
[cmd.Name.QUIT, del('/session/:sessionId')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
|
|
|
|
// Server status.
|
|
|
|
|
[cmd.Name.GET_SERVER_STATUS, get('/status')],
|
|
|
|
|
|
|
|
|
|
// timeouts
|
2017-12-27 15:12:04 -08:00
|
|
|
[cmd.Name.GET_TIMEOUT, get('/session/:sessionId/timeouts')],
|
|
|
|
|
[cmd.Name.SET_TIMEOUT, post('/session/:sessionId/timeouts')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Navigation.
|
|
|
|
|
[cmd.Name.GET_CURRENT_URL, get('/session/:sessionId/url')],
|
|
|
|
|
[cmd.Name.GET, post('/session/:sessionId/url')],
|
|
|
|
|
[cmd.Name.GO_BACK, post('/session/:sessionId/back')],
|
|
|
|
|
[cmd.Name.GO_FORWARD, post('/session/:sessionId/forward')],
|
|
|
|
|
[cmd.Name.REFRESH, post('/session/:sessionId/refresh')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Page inspection.
|
|
|
|
|
[cmd.Name.GET_PAGE_SOURCE, get('/session/:sessionId/source')],
|
|
|
|
|
[cmd.Name.GET_TITLE, get('/session/:sessionId/title')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Script execution.
|
|
|
|
|
[cmd.Name.EXECUTE_SCRIPT, post('/session/:sessionId/execute/sync')],
|
|
|
|
|
[cmd.Name.EXECUTE_ASYNC_SCRIPT, post('/session/:sessionId/execute/async')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Frame selection.
|
|
|
|
|
[cmd.Name.SWITCH_TO_FRAME, post('/session/:sessionId/frame')],
|
|
|
|
|
[cmd.Name.SWITCH_TO_FRAME_PARENT, post('/session/:sessionId/frame/parent')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Window management.
|
|
|
|
|
[cmd.Name.GET_CURRENT_WINDOW_HANDLE, get('/session/:sessionId/window')],
|
|
|
|
|
[cmd.Name.CLOSE, del('/session/:sessionId/window')],
|
|
|
|
|
[cmd.Name.SWITCH_TO_WINDOW, post('/session/:sessionId/window')],
|
2019-02-20 17:01:52 +02:00
|
|
|
[cmd.Name.SWITCH_TO_NEW_WINDOW, post('/session/:sessionId/window/new')],
|
2017-12-27 15:12:04 -08:00
|
|
|
[cmd.Name.GET_WINDOW_HANDLES, get('/session/:sessionId/window/handles')],
|
|
|
|
|
[cmd.Name.GET_WINDOW_RECT, get('/session/:sessionId/window/rect')],
|
|
|
|
|
[cmd.Name.SET_WINDOW_RECT, post('/session/:sessionId/window/rect')],
|
|
|
|
|
[cmd.Name.MAXIMIZE_WINDOW, post('/session/:sessionId/window/maximize')],
|
|
|
|
|
[cmd.Name.MINIMIZE_WINDOW, post('/session/:sessionId/window/minimize')],
|
|
|
|
|
[cmd.Name.FULLSCREEN_WINDOW, post('/session/:sessionId/window/fullscreen')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Actions.
|
2017-10-08 20:05:35 -07:00
|
|
|
[cmd.Name.ACTIONS, post('/session/:sessionId/actions')],
|
|
|
|
|
[cmd.Name.CLEAR_ACTIONS, del('/session/:sessionId/actions')],
|
2021-11-30 16:27:02 +05:30
|
|
|
[cmd.Name.PRINT_PAGE, post('/session/:sessionId/print')],
|
|
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Locating elements.
|
2016-10-19 14:27:27 -07:00
|
|
|
[cmd.Name.GET_ACTIVE_ELEMENT, get('/session/:sessionId/element/active')],
|
2017-12-27 15:12:04 -08:00
|
|
|
[cmd.Name.FIND_ELEMENT, post('/session/:sessionId/element')],
|
|
|
|
|
[cmd.Name.FIND_ELEMENTS, post('/session/:sessionId/elements')],
|
2020-08-03 17:56:31 +03:00
|
|
|
[
|
|
|
|
|
cmd.Name.FIND_ELEMENTS_RELATIVE,
|
|
|
|
|
(cmd) => {
|
2024-02-07 16:07:24 +00:00
|
|
|
return toExecuteAtomCommand(cmd, Atom.FIND_ELEMENTS, 'findElements', 'args')
|
2020-08-03 17:56:31 +03:00
|
|
|
},
|
|
|
|
|
],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.FIND_CHILD_ELEMENT, post('/session/:sessionId/element/:id/element')],
|
|
|
|
|
[cmd.Name.FIND_CHILD_ELEMENTS, post('/session/:sessionId/element/:id/elements')],
|
2017-12-27 15:12:04 -08:00
|
|
|
// Element interaction.
|
|
|
|
|
[cmd.Name.GET_ELEMENT_TAG_NAME, get('/session/:sessionId/element/:id/name')],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.GET_DOM_ATTRIBUTE, get('/session/:sessionId/element/:id/attribute/:name')],
|
2021-11-30 16:27:02 +05:30
|
|
|
[
|
|
|
|
|
cmd.Name.GET_ELEMENT_ATTRIBUTE,
|
|
|
|
|
(cmd) => {
|
2024-02-07 16:07:24 +00:00
|
|
|
return toExecuteAtomCommand(cmd, Atom.GET_ATTRIBUTE, 'getAttribute', 'id', 'name')
|
2021-11-30 16:27:02 +05:30
|
|
|
},
|
|
|
|
|
],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.GET_ELEMENT_PROPERTY, get('/session/:sessionId/element/:id/property/:name')],
|
|
|
|
|
[cmd.Name.GET_ELEMENT_VALUE_OF_CSS_PROPERTY, get('/session/:sessionId/element/:id/css/:propertyName')],
|
2018-01-07 18:55:16 -08:00
|
|
|
[cmd.Name.GET_ELEMENT_RECT, get('/session/:sessionId/element/:id/rect')],
|
2017-12-27 15:12:04 -08:00
|
|
|
[cmd.Name.CLEAR_ELEMENT, post('/session/:sessionId/element/:id/clear')],
|
|
|
|
|
[cmd.Name.CLICK_ELEMENT, post('/session/:sessionId/element/:id/click')],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.SEND_KEYS_TO_ELEMENT, post('/session/:sessionId/element/:id/value')],
|
2017-12-27 15:12:04 -08:00
|
|
|
[cmd.Name.GET_ELEMENT_TEXT, get('/session/:sessionId/element/:id/text')],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.GET_COMPUTED_ROLE, get('/session/:sessionId/element/:id/computedrole')],
|
|
|
|
|
[cmd.Name.GET_COMPUTED_LABEL, get('/session/:sessionId/element/:id/computedlabel')],
|
2017-12-27 15:12:04 -08:00
|
|
|
[cmd.Name.IS_ELEMENT_ENABLED, get('/session/:sessionId/element/:id/enabled')],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.IS_ELEMENT_SELECTED, get('/session/:sessionId/element/:id/selected')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
[
|
|
|
|
|
cmd.Name.IS_ELEMENT_DISPLAYED,
|
|
|
|
|
(cmd) => {
|
2023-01-02 16:13:27 -06:00
|
|
|
return toExecuteAtomCommand(cmd, Atom.IS_DISPLAYED, 'isDisplayed', 'id')
|
2020-08-03 17:56:31 +03:00
|
|
|
},
|
|
|
|
|
],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Cookie management.
|
|
|
|
|
[cmd.Name.GET_ALL_COOKIES, get('/session/:sessionId/cookie')],
|
|
|
|
|
[cmd.Name.ADD_COOKIE, post('/session/:sessionId/cookie')],
|
|
|
|
|
[cmd.Name.DELETE_ALL_COOKIES, del('/session/:sessionId/cookie')],
|
|
|
|
|
[cmd.Name.GET_COOKIE, get('/session/:sessionId/cookie/:name')],
|
|
|
|
|
[cmd.Name.DELETE_COOKIE, del('/session/:sessionId/cookie/:name')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Alert management.
|
|
|
|
|
[cmd.Name.ACCEPT_ALERT, post('/session/:sessionId/alert/accept')],
|
|
|
|
|
[cmd.Name.DISMISS_ALERT, post('/session/:sessionId/alert/dismiss')],
|
|
|
|
|
[cmd.Name.GET_ALERT_TEXT, get('/session/:sessionId/alert/text')],
|
|
|
|
|
[cmd.Name.SET_ALERT_TEXT, post('/session/:sessionId/alert/text')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2017-12-27 15:12:04 -08:00
|
|
|
// Screenshots.
|
|
|
|
|
[cmd.Name.SCREENSHOT, get('/session/:sessionId/screenshot')],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.TAKE_ELEMENT_SCREENSHOT, get('/session/:sessionId/element/:id/screenshot')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
2021-11-16 21:05:25 +00:00
|
|
|
// Shadow Root
|
|
|
|
|
[cmd.Name.GET_SHADOW_ROOT, get('/session/:sessionId/element/:id/shadow')],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.FIND_ELEMENT_FROM_SHADOWROOT, post('/session/:sessionId/shadow/:id/element')],
|
|
|
|
|
[cmd.Name.FIND_ELEMENTS_FROM_SHADOWROOT, post('/session/:sessionId/shadow/:id/elements')],
|
2019-07-11 16:56:17 -07:00
|
|
|
// Log extensions.
|
|
|
|
|
[cmd.Name.GET_LOG, post('/session/:sessionId/se/log')],
|
|
|
|
|
[cmd.Name.GET_AVAILABLE_LOG_TYPES, get('/session/:sessionId/se/log/types')],
|
2021-11-30 16:27:02 +05:30
|
|
|
|
|
|
|
|
// Server Extensions
|
|
|
|
|
[cmd.Name.UPLOAD_FILE, post('/session/:sessionId/se/file')],
|
2022-05-31 11:40:57 +05:30
|
|
|
|
|
|
|
|
// Virtual Authenticator
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.ADD_VIRTUAL_AUTHENTICATOR, post('/session/:sessionId/webauthn/authenticator')],
|
|
|
|
|
[cmd.Name.REMOVE_VIRTUAL_AUTHENTICATOR, del('/session/:sessionId/webauthn/authenticator/:authenticatorId')],
|
|
|
|
|
[cmd.Name.ADD_CREDENTIAL, post('/session/:sessionId/webauthn/authenticator/:authenticatorId/credential')],
|
|
|
|
|
[cmd.Name.GET_CREDENTIALS, get('/session/:sessionId/webauthn/authenticator/:authenticatorId/credentials')],
|
2022-06-02 23:41:17 +05:30
|
|
|
[
|
|
|
|
|
cmd.Name.REMOVE_CREDENTIAL,
|
2024-02-07 16:07:24 +00:00
|
|
|
del('/session/:sessionId/webauthn/authenticator/:authenticatorId/credentials/:credentialId'),
|
2022-06-02 23:41:17 +05:30
|
|
|
],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.REMOVE_ALL_CREDENTIALS, del('/session/:sessionId/webauthn/authenticator/:authenticatorId/credentials')],
|
|
|
|
|
[cmd.Name.SET_USER_VERIFIED, post('/session/:sessionId/webauthn/authenticator/:authenticatorId/uv')],
|
2024-01-02 21:17:53 -06:00
|
|
|
|
|
|
|
|
[cmd.Name.GET_DOWNLOADABLE_FILES, get('/session/:sessionId/se/files')],
|
|
|
|
|
[cmd.Name.DOWNLOAD_FILE, post(`/session/:sessionId/se/files`)],
|
2024-02-07 16:07:24 +00:00
|
|
|
[cmd.Name.DELETE_DOWNLOADABLE_FILES, del(`/session/:sessionId/se/files`)],
|
2025-01-06 15:09:34 +05:30
|
|
|
|
|
|
|
|
// Federated Credential Management Command
|
|
|
|
|
[cmd.Name.CANCEL_DIALOG, post(`/session/:sessionId/fedcm/canceldialog`)],
|
|
|
|
|
[cmd.Name.SELECT_ACCOUNT, post(`/session/:sessionId/fedcm/selectaccount`)],
|
|
|
|
|
[cmd.Name.GET_FEDCM_TITLE, get(`/session/:sessionId/fedcm/gettitle`)],
|
|
|
|
|
[cmd.Name.GET_FEDCM_DIALOG_TYPE, get('/session/:sessionId/fedcm/getdialogtype')],
|
|
|
|
|
[cmd.Name.SET_DELAY_ENABLED, post(`/session/:sessionId/fedcm/setdelayenabled`)],
|
|
|
|
|
[cmd.Name.RESET_COOLDOWN, post(`/session/:sessionId/fedcm/resetcooldown`)],
|
|
|
|
|
[cmd.Name.CLICK_DIALOG_BUTTON, post(`/session/:sessionId/fedcm/clickdialogbutton`)],
|
|
|
|
|
[cmd.Name.GET_ACCOUNTS, get(`/session/:sessionId/fedcm/accountlist`)],
|
2020-08-03 17:56:31 +03:00
|
|
|
])
|
2016-05-15 11:47:21 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handles sending HTTP messages to a remote end.
|
|
|
|
|
*
|
|
|
|
|
* @interface
|
|
|
|
|
*/
|
|
|
|
|
class Client {
|
|
|
|
|
/**
|
|
|
|
|
* Sends a request to the server. The client will automatically follow any
|
|
|
|
|
* redirects returned by the server, fulfilling the returned promise with the
|
|
|
|
|
* final response.
|
|
|
|
|
*
|
|
|
|
|
* @param {!Request} httpRequest The request to send.
|
2016-06-08 08:26:34 -07:00
|
|
|
* @return {!Promise<Response>} A promise that will be fulfilled with the
|
|
|
|
|
* server's response.
|
2016-05-15 11:47:21 -07:00
|
|
|
*/
|
2024-04-16 01:50:58 +05:30
|
|
|
send(httpRequest) {}
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-02 19:18:47 -07:00
|
|
|
/**
|
|
|
|
|
* @param {Map<string, CommandSpec>} customCommands
|
|
|
|
|
* A map of custom command definitions.
|
|
|
|
|
* @param {!cmd.Command} command The command to resolve.
|
2017-10-29 20:13:40 -07:00
|
|
|
* @return {!Request} A promise that will resolve with the
|
2016-11-02 19:18:47 -07:00
|
|
|
* command to execute.
|
|
|
|
|
*/
|
2021-11-30 16:27:02 +05:30
|
|
|
function buildRequest(customCommands, command) {
|
2023-10-11 06:54:01 -04:00
|
|
|
log_.finest(() => `Translating command: ${command.getName()}`)
|
2020-08-03 17:56:31 +03:00
|
|
|
let spec = customCommands && customCommands.get(command.getName())
|
2016-11-02 19:18:47 -07:00
|
|
|
if (spec) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return toHttpRequest(spec)
|
2016-11-02 19:18:47 -07:00
|
|
|
}
|
|
|
|
|
|
2021-11-30 16:27:02 +05:30
|
|
|
spec = W3C_COMMAND_MAP.get(command.getName())
|
|
|
|
|
if (typeof spec === 'function') {
|
2023-10-11 06:54:01 -04:00
|
|
|
log_.finest(() => `Transforming command for W3C: ${command.getName()}`)
|
2021-11-30 16:27:02 +05:30
|
|
|
let newCommand = spec(command)
|
|
|
|
|
return buildRequest(customCommands, newCommand)
|
|
|
|
|
} else if (spec) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return toHttpRequest(spec)
|
2016-11-02 19:18:47 -07:00
|
|
|
}
|
2024-02-07 16:07:24 +00:00
|
|
|
throw new error.UnknownCommandError('Unrecognized command: ' + command.getName())
|
2016-11-02 19:18:47 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {CommandSpec} resource
|
2017-10-29 20:13:40 -07:00
|
|
|
* @return {!Request}
|
2016-11-02 19:18:47 -07:00
|
|
|
*/
|
|
|
|
|
function toHttpRequest(resource) {
|
2023-10-11 06:54:01 -04:00
|
|
|
log_.finest(() => `Building HTTP request: ${JSON.stringify(resource)}`)
|
2020-08-03 17:56:31 +03:00
|
|
|
let parameters = command.getParameters()
|
|
|
|
|
let path = buildPath(resource.path, parameters)
|
|
|
|
|
return new Request(resource.method, path, parameters)
|
2016-11-02 19:18:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-07 16:07:24 +00:00
|
|
|
const CLIENTS = /** !WeakMap<!Executor, !(Client|IThenable<!Client>)> */ new WeakMap()
|
2017-10-29 20:13:40 -07:00
|
|
|
|
2016-05-15 11:47:21 -07:00
|
|
|
/**
|
|
|
|
|
* A command executor that communicates with the server using JSON over HTTP.
|
|
|
|
|
*
|
|
|
|
|
* By default, each instance of this class will use the legacy wire protocol
|
|
|
|
|
* from [Selenium project][json]. The executor will automatically switch to the
|
|
|
|
|
* [W3C wire protocol][w3c] if the remote end returns a compliant response to
|
|
|
|
|
* a new session command.
|
|
|
|
|
*
|
|
|
|
|
* [json]: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
|
|
|
|
|
* [w3c]: https://w3c.github.io/webdriver/webdriver-spec.html
|
|
|
|
|
*
|
|
|
|
|
* @implements {cmd.Executor}
|
|
|
|
|
*/
|
|
|
|
|
class Executor {
|
|
|
|
|
/**
|
2016-08-06 08:37:25 -07:00
|
|
|
* @param {!(Client|IThenable<!Client>)} client The client to use for sending
|
2022-08-11 22:07:00 +02:00
|
|
|
* requests to the server, or a promise-like object that will resolve
|
2016-08-06 08:37:25 -07:00
|
|
|
* to the client.
|
2016-05-15 11:47:21 -07:00
|
|
|
*/
|
|
|
|
|
constructor(client) {
|
2020-08-03 17:56:31 +03:00
|
|
|
CLIENTS.set(this, client)
|
2016-05-15 11:47:21 -07:00
|
|
|
|
2016-11-02 19:18:47 -07:00
|
|
|
/** @private {Map<string, CommandSpec>} */
|
2020-08-03 17:56:31 +03:00
|
|
|
this.customCommands_ = null
|
2016-05-15 11:47:21 -07:00
|
|
|
|
|
|
|
|
/** @private {!logging.Logger} */
|
2023-10-11 06:54:01 -04:00
|
|
|
this.log_ = logging.getLogger(`${logging.Type.DRIVER}.http.Executor`)
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines a new command for use with this executor. When a command is sent,
|
|
|
|
|
* the {@code path} will be preprocessed using the command's parameters; any
|
|
|
|
|
* path segments prefixed with ":" will be replaced by the parameter of the
|
|
|
|
|
* same name. For example, given "/person/:name" and the parameters
|
|
|
|
|
* "{name: 'Bob'}", the final command path will be "/person/Bob".
|
|
|
|
|
*
|
|
|
|
|
* @param {string} name The command name.
|
|
|
|
|
* @param {string} method The HTTP method to use when sending this command.
|
|
|
|
|
* @param {string} path The path to send the command to, relative to
|
|
|
|
|
* the WebDriver server's command root and of the form
|
|
|
|
|
* "/path/:variable/segment".
|
|
|
|
|
*/
|
|
|
|
|
defineCommand(name, method, path) {
|
|
|
|
|
if (!this.customCommands_) {
|
2020-08-03 17:56:31 +03:00
|
|
|
this.customCommands_ = new Map()
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
this.customCommands_.set(name, { method, path })
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @override */
|
2017-10-29 20:13:40 -07:00
|
|
|
async execute(command) {
|
2021-11-30 16:27:02 +05:30
|
|
|
let request = buildRequest(this.customCommands_, command)
|
2020-08-03 17:56:31 +03:00
|
|
|
this.log_.finer(() => `>>> ${request.method} ${request.path}`)
|
2017-10-29 20:13:40 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let client = CLIENTS.get(this)
|
2017-10-29 20:13:40 -07:00
|
|
|
if (promise.isPromise(client)) {
|
2020-08-03 17:56:31 +03:00
|
|
|
client = await client
|
|
|
|
|
CLIENTS.set(this, client)
|
2017-10-29 20:13:40 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let response = await client.send(request)
|
|
|
|
|
this.log_.finer(() => `>>>\n${request}\n<<<\n${response}`)
|
2017-10-29 20:13:40 -07:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let httpResponse = /** @type {!Response} */ (response)
|
2022-05-31 11:40:57 +05:30
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let { isW3C, value } = parseHttpResponse(command, httpResponse)
|
2017-10-29 20:13:40 -07:00
|
|
|
|
|
|
|
|
if (command.getName() === cmd.Name.NEW_SESSION) {
|
|
|
|
|
if (!value || !value.sessionId) {
|
2024-02-07 16:07:24 +00:00
|
|
|
throw new error.WebDriverError(`Unable to parse new session response: ${response.body}`)
|
2017-10-29 20:13:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The remote end is a W3C compliant server if there is no `status`
|
|
|
|
|
// field in the response.
|
|
|
|
|
if (command.getName() === cmd.Name.NEW_SESSION) {
|
2020-08-03 17:56:31 +03:00
|
|
|
this.w3c = this.w3c || isW3C
|
2017-10-29 20:13:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No implementations use the `capabilities` key yet...
|
2020-08-03 17:56:31 +03:00
|
|
|
let capabilities = value.capabilities || value.value
|
2024-02-07 16:07:24 +00:00
|
|
|
return new Session(/** @type {{sessionId: string}} */ (value).sessionId, capabilities)
|
2017-10-29 20:13:40 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
return typeof value === 'undefined' ? null : value
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} str .
|
|
|
|
|
* @return {?} .
|
|
|
|
|
*/
|
|
|
|
|
function tryParse(str) {
|
|
|
|
|
try {
|
2020-08-03 17:56:31 +03:00
|
|
|
return JSON.parse(str)
|
2024-04-16 01:50:58 +05:30
|
|
|
/*eslint no-unused-vars: "off"*/
|
2016-05-15 11:47:21 -07:00
|
|
|
} catch (ignored) {
|
|
|
|
|
// Do nothing.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback used to parse {@link Response} objects from a
|
|
|
|
|
* {@link HttpClient}.
|
2017-01-23 08:19:24 -08:00
|
|
|
*
|
|
|
|
|
* @param {!cmd.Command} command The command the response is for.
|
2016-05-15 11:47:21 -07:00
|
|
|
* @param {!Response} httpResponse The HTTP response to parse.
|
2017-03-09 22:38:01 -08:00
|
|
|
* @return {{isW3C: boolean, value: ?}} An object describing the parsed
|
|
|
|
|
* response. This object will have two fields: `isW3C` indicates whether
|
|
|
|
|
* the response looks like it came from a remote end that conforms with the
|
|
|
|
|
* W3C WebDriver spec, and `value`, the actual response value.
|
2016-05-15 11:47:21 -07:00
|
|
|
* @throws {WebDriverError} If the HTTP response is an error.
|
|
|
|
|
*/
|
2017-03-09 22:38:01 -08:00
|
|
|
function parseHttpResponse(command, httpResponse) {
|
|
|
|
|
if (httpResponse.status < 200) {
|
|
|
|
|
// This should never happen, but throw the raw response so users report it.
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new error.WebDriverError(`Unexpected HTTP response:\n${httpResponse}`)
|
2017-03-09 22:38:01 -08:00
|
|
|
}
|
2017-01-23 08:19:24 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let parsed = tryParse(httpResponse.body)
|
2022-05-31 11:40:57 +05:30
|
|
|
|
2017-03-09 22:38:01 -08:00
|
|
|
if (parsed && typeof parsed === 'object') {
|
2020-08-03 17:56:31 +03:00
|
|
|
let value = parsed.value
|
2022-08-28 11:26:01 +03:00
|
|
|
let isW3C = isObject(value) && typeof parsed.status === 'undefined'
|
2017-03-09 22:38:01 -08:00
|
|
|
|
|
|
|
|
if (!isW3C) {
|
2020-08-03 17:56:31 +03:00
|
|
|
error.checkLegacyResponse(parsed)
|
2017-03-09 22:38:01 -08:00
|
|
|
|
|
|
|
|
// Adjust legacy new session responses to look like W3C to simplify
|
|
|
|
|
// later processing.
|
2017-10-08 20:26:39 -07:00
|
|
|
if (command.getName() === cmd.Name.NEW_SESSION) {
|
2020-08-03 17:56:31 +03:00
|
|
|
value = parsed
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
2017-03-09 22:38:01 -08:00
|
|
|
} else if (httpResponse.status > 399) {
|
2020-08-03 17:56:31 +03:00
|
|
|
error.throwDecodedError(value)
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
2017-01-23 08:19:24 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
return { isW3C, value }
|
2017-03-09 22:38:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parsed !== undefined) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return { isW3C: false, value: parsed }
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
let value = httpResponse.body.replace(/\r\n/g, '\n')
|
2016-05-15 11:47:21 -07:00
|
|
|
|
|
|
|
|
// 404 represents an unknown command; anything else > 399 is a generic unknown
|
|
|
|
|
// error.
|
2021-11-30 16:27:02 +05:30
|
|
|
if (httpResponse.status === 404) {
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new error.UnsupportedOperationError(command.getName() + ': ' + value)
|
2016-05-15 11:47:21 -07:00
|
|
|
} else if (httpResponse.status >= 400) {
|
2020-08-03 17:56:31 +03:00
|
|
|
throw new error.WebDriverError(value)
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
return { isW3C: false, value: value || null }
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds a fully qualified path using the given set of command parameters. Each
|
|
|
|
|
* path segment prefixed with ':' will be replaced by the value of the
|
|
|
|
|
* corresponding parameter. All parameters spliced into the path will be
|
|
|
|
|
* removed from the parameter map.
|
|
|
|
|
* @param {string} path The original resource path.
|
|
|
|
|
* @param {!Object<*>} parameters The parameters object to splice into the path.
|
|
|
|
|
* @return {string} The modified path.
|
|
|
|
|
*/
|
|
|
|
|
function buildPath(path, parameters) {
|
2020-08-03 17:56:31 +03:00
|
|
|
let pathParameters = path.match(/\/:(\w+)\b/g)
|
2016-05-15 11:47:21 -07:00
|
|
|
if (pathParameters) {
|
|
|
|
|
for (let i = 0; i < pathParameters.length; ++i) {
|
2020-08-03 17:56:31 +03:00
|
|
|
let key = pathParameters[i].substring(2) // Trim the /:
|
2016-05-15 11:47:21 -07:00
|
|
|
if (key in parameters) {
|
2020-08-03 17:56:31 +03:00
|
|
|
let value = parameters[key]
|
2022-08-11 22:07:00 +02:00
|
|
|
if (webElement.isId(value)) {
|
2016-05-15 11:47:21 -07:00
|
|
|
// When inserting a WebElement into the URL, only use its ID value,
|
|
|
|
|
// not the full JSON.
|
2022-08-11 22:07:00 +02:00
|
|
|
value = webElement.extractId(value)
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
path = path.replace(pathParameters[i], '/' + value)
|
|
|
|
|
delete parameters[key]
|
2016-05-15 11:47:21 -07:00
|
|
|
} else {
|
2024-02-07 16:07:24 +00:00
|
|
|
throw new error.InvalidArgumentError('Missing required parameter: ' + key)
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
return path
|
2016-05-15 11:47:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUBLIC API
|
|
|
|
|
|
2021-11-30 16:27:02 +05:30
|
|
|
module.exports = {
|
2022-08-28 11:26:01 +03:00
|
|
|
Executor,
|
|
|
|
|
Client,
|
|
|
|
|
Request,
|
|
|
|
|
Response,
|
2021-11-30 16:27:02 +05:30
|
|
|
// Exported for testing.
|
2022-08-28 11:26:01 +03:00
|
|
|
buildPath,
|
2021-11-30 16:27:02 +05:30
|
|
|
}
|