// 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');272829/**30* Type definition for a response object, as defined by the JSON wire protocol.31* @typedef {{status: bot.ErrorCode, value: (*|{message: string})}}32* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#responses33*/34bot.response.ResponseObject;353637/**38* @param {*} value The value to test.39* @return {boolean} Whether the given value is a response object.40*/41bot.response.isResponseObject = function (value) {42return goog.isObject(value) && goog.isNumber(value['status']);43};444546/**47* Creates a new success response object with the provided value.48* @param {*} value The response value.49* @return {!bot.response.ResponseObject} The new response object.50*/51bot.response.createResponse = function (value) {52if (bot.response.isResponseObject(value)) {53return /** @type {!bot.response.ResponseObject} */ (value);54}55return {56'status': bot.ErrorCode.SUCCESS,57'value': value58};59};606162/**63* Converts an error value into its JSON representation as defined by the64* WebDriver wire protocol.65* @param {(bot.Error|Error|*)} error The error value to convert.66* @return {!bot.response.ResponseObject} The new response object.67*/68bot.response.createErrorResponse = function (error) {69if (bot.response.isResponseObject(error)) {70return /** @type {!bot.response.ResponseObject} */ (error);71}7273var statusCode = error && goog.isNumber(error.code) ? error.code :74bot.ErrorCode.UNKNOWN_ERROR;75return {76'status': /** @type {bot.ErrorCode} */ (statusCode),77'value': {78'message': (error && error.message || error) + ''79}80};81};828384/**85* Checks that a response object does not specify an error as defined by the86* WebDriver wire protocol. If the response object defines an error, it will87* be thrown. Otherwise, the response will be returned as is.88* @param {!bot.response.ResponseObject} responseObj The response object to89* check.90* @return {!bot.response.ResponseObject} The checked response object.91* @throws {bot.Error} If the response describes an error.92* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#failed-commands93*/94bot.response.checkResponse = function (responseObj) {95var status = responseObj['status'];96if (status == bot.ErrorCode.SUCCESS) {97return responseObj;98}99100// If status is not defined, assume an unknown error.101status = status || bot.ErrorCode.UNKNOWN_ERROR;102103var value = responseObj['value'];104if (!value || !goog.isObject(value)) {105throw new bot.Error(status, value + '');106}107108throw new bot.Error(status, value['message'] + '');109};110111112