Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/response.js
4505 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
/**
19
* @fileoverview Utilities for working with WebDriver response objects.
20
* @see: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#responses
21
*/
22
23
goog.provide('bot.response');
24
goog.provide('bot.response.ResponseObject');
25
26
goog.require('bot.Error');
27
goog.require('bot.ErrorCode');
28
goog.require('goog.utils');
29
30
31
/**
32
* Type definition for a response object, as defined by the JSON wire protocol.
33
* @typedef {{status: bot.ErrorCode, value: (*|{message: string})}}
34
* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#responses
35
*/
36
bot.response.ResponseObject;
37
38
39
/**
40
* @param {*} value The value to test.
41
* @return {boolean} Whether the given value is a response object.
42
*/
43
bot.response.isResponseObject = function (value) {
44
return goog.utils.isObject(value) && goog.utils.isNumber(value['status']);
45
};
46
47
48
/**
49
* Creates a new success response object with the provided value.
50
* @param {*} value The response value.
51
* @return {!bot.response.ResponseObject} The new response object.
52
*/
53
bot.response.createResponse = function (value) {
54
if (bot.response.isResponseObject(value)) {
55
return /** @type {!bot.response.ResponseObject} */ (value);
56
}
57
return {
58
'status': bot.ErrorCode.SUCCESS,
59
'value': value
60
};
61
};
62
63
64
/**
65
* Converts an error value into its JSON representation as defined by the
66
* WebDriver wire protocol.
67
* @param {(bot.Error|Error|*)} error The error value to convert.
68
* @return {!bot.response.ResponseObject} The new response object.
69
*/
70
bot.response.createErrorResponse = function (error) {
71
if (bot.response.isResponseObject(error)) {
72
return /** @type {!bot.response.ResponseObject} */ (error);
73
}
74
75
var statusCode = error && goog.utils.isNumber(error.code) ? error.code :
76
bot.ErrorCode.UNKNOWN_ERROR;
77
return {
78
'status': /** @type {bot.ErrorCode} */ (statusCode),
79
'value': {
80
'message': (error && error.message || error) + ''
81
}
82
};
83
};
84
85
86
/**
87
* Checks that a response object does not specify an error as defined by the
88
* WebDriver wire protocol. If the response object defines an error, it will
89
* be thrown. Otherwise, the response will be returned as is.
90
* @param {!bot.response.ResponseObject} responseObj The response object to
91
* check.
92
* @return {!bot.response.ResponseObject} The checked response object.
93
* @throws {bot.Error} If the response describes an error.
94
* @see https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#failed-commands
95
*/
96
bot.response.checkResponse = function (responseObj) {
97
var status = responseObj['status'];
98
if (status == bot.ErrorCode.SUCCESS) {
99
return responseObj;
100
}
101
102
// If status is not defined, assume an unknown error.
103
status = status || bot.ErrorCode.UNKNOWN_ERROR;
104
105
var value = responseObj['value'];
106
if (!value || !goog.utils.isObject(value)) {
107
throw new bot.Error(status, value + '');
108
}
109
110
throw new bot.Error(status, value['message'] + '');
111
};
112
113