// 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 Utilities for working with WebDriver response objects.19* @see: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#responses20*/2122goog.provide('bot.response');23goog.provide('bot.response.ResponseObject');2425goog.require('bot.Error');26goog.require('bot.ErrorCode');27goog.require('goog.utils');282930/**31* Type definition for a response object, as defined by the JSON wire protocol.32* @typedef {{status: bot.ErrorCode, value: (*|{message: string})}}33* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#responses34*/35bot.response.ResponseObject;363738/**39* @param {*} value The value to test.40* @return {boolean} Whether the given value is a response object.41*/42bot.response.isResponseObject = function (value) {43return goog.utils.isObject(value) && goog.utils.isNumber(value['status']);44};454647/**48* Creates a new success response object with the provided value.49* @param {*} value The response value.50* @return {!bot.response.ResponseObject} The new response object.51*/52bot.response.createResponse = function (value) {53if (bot.response.isResponseObject(value)) {54return /** @type {!bot.response.ResponseObject} */ (value);55}56return {57'status': bot.ErrorCode.SUCCESS,58'value': value59};60};616263/**64* Converts an error value into its JSON representation as defined by the65* WebDriver wire protocol.66* @param {(bot.Error|Error|*)} error The error value to convert.67* @return {!bot.response.ResponseObject} The new response object.68*/69bot.response.createErrorResponse = function (error) {70if (bot.response.isResponseObject(error)) {71return /** @type {!bot.response.ResponseObject} */ (error);72}7374var statusCode = error && goog.utils.isNumber(error.code) ? error.code :75bot.ErrorCode.UNKNOWN_ERROR;76return {77'status': /** @type {bot.ErrorCode} */ (statusCode),78'value': {79'message': (error && error.message || error) + ''80}81};82};838485/**86* Checks that a response object does not specify an error as defined by the87* WebDriver wire protocol. If the response object defines an error, it will88* be thrown. Otherwise, the response will be returned as is.89* @param {!bot.response.ResponseObject} responseObj The response object to90* check.91* @return {!bot.response.ResponseObject} The checked response object.92* @throws {bot.Error} If the response describes an error.93* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#failed-commands94*/95bot.response.checkResponse = function (responseObj) {96var status = responseObj['status'];97if (status == bot.ErrorCode.SUCCESS) {98return responseObj;99}100101// If status is not defined, assume an unknown error.102status = status || bot.ErrorCode.UNKNOWN_ERROR;103104var value = responseObj['value'];105if (!value || !goog.utils.isObject(value)) {106throw new bot.Error(status, value + '');107}108109throw new bot.Error(status, value['message'] + '');110};111112113