Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/selenium-webdriver/http/util.js
1864 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 Various HTTP utilities.
20
*/
21
22
'use strict'
23
24
const Executor = require('./index').Executor
25
const HttpClient = require('./index').HttpClient
26
const HttpRequest = require('./index').Request
27
const Command = require('../lib/command').Command
28
const CommandName = require('../lib/command').Name
29
const error = require('../lib/error')
30
31
/**
32
* Queries a WebDriver server for its current status.
33
* @param {string} url Base URL of the server to query.
34
* @return {!Promise<!Object>} A promise that resolves with
35
* a hash of the server status.
36
*/
37
function getStatus(url) {
38
const client = new HttpClient(url)
39
const executor = new Executor(client)
40
const command = new Command(CommandName.GET_SERVER_STATUS)
41
return executor.execute(command)
42
}
43
44
class CancellationError {}
45
46
/**
47
* Waits for a WebDriver server to be healthy and accepting requests.
48
* @param {string} url Base URL of the server to query.
49
* @param {number} timeout How long to wait for the server.
50
* @param {Promise=} opt_cancelToken A promise used as a cancellation signal:
51
* if resolved before the server is ready, the wait will be terminated
52
* early with a {@link CancellationError}.
53
* @return {!Promise} A promise that will resolve when the server is ready, or
54
* if the wait is cancelled.
55
*/
56
function waitForServer(url, timeout, opt_cancelToken) {
57
return new Promise((onResolve, onReject) => {
58
let start = Date.now()
59
60
let done = false
61
let resolve = (status) => {
62
done = true
63
onResolve(status)
64
}
65
let reject = (err) => {
66
done = true
67
onReject(err)
68
}
69
70
if (opt_cancelToken) {
71
opt_cancelToken.then((_) => reject(new CancellationError()))
72
}
73
74
checkServerStatus()
75
76
function checkServerStatus() {
77
return getStatus(url).then((status) => resolve(status), onError)
78
}
79
80
function onError(e) {
81
// Some servers don't support the status command. If they are able to
82
// response with an error, then can consider the server ready.
83
if (e instanceof error.UnsupportedOperationError) {
84
resolve({})
85
return
86
}
87
88
if (Date.now() - start > timeout) {
89
reject(Error('Timed out waiting for the WebDriver server at ' + url))
90
} else {
91
setTimeout(function () {
92
if (!done) {
93
checkServerStatus()
94
}
95
}, 50)
96
}
97
}
98
})
99
}
100
101
/**
102
* Polls a URL with GET requests until it returns a 2xx response or the
103
* timeout expires.
104
* @param {string} url The URL to poll.
105
* @param {number} timeout How long to wait, in milliseconds.
106
* @param {Promise=} opt_cancelToken A promise used as a cancellation signal:
107
* if resolved before the a 2xx response is received, the wait will be
108
* terminated early with a {@link CancellationError}.
109
* @return {!Promise} A promise that will resolve when a 2xx is received from
110
* the given URL, or if the wait is cancelled.
111
*/
112
function waitForUrl(url, timeout, opt_cancelToken) {
113
return new Promise((onResolve, onReject) => {
114
let client = new HttpClient(url)
115
let request = new HttpRequest('GET', '')
116
let start = Date.now()
117
118
let done = false
119
let resolve = () => {
120
done = true
121
onResolve()
122
}
123
let reject = (err) => {
124
done = true
125
onReject(err)
126
}
127
128
if (opt_cancelToken) {
129
opt_cancelToken.then((_) => reject(new CancellationError()))
130
}
131
132
testUrl()
133
134
function testUrl() {
135
client.send(request).then(onResponse, onError)
136
}
137
138
function onError() {
139
if (Date.now() - start > timeout) {
140
reject(Error('Timed out waiting for the URL to return 2xx: ' + url))
141
} else {
142
setTimeout(function () {
143
if (!done) {
144
testUrl()
145
}
146
}, 50)
147
}
148
}
149
150
function onResponse(response) {
151
if (done) {
152
return
153
}
154
if (response.status > 199 && response.status < 300) {
155
resolve()
156
return
157
}
158
onError()
159
}
160
})
161
}
162
163
// PUBLIC API
164
module.exports.getStatus = getStatus
165
module.exports.CancellationError = CancellationError
166
module.exports.waitForServer = waitForServer
167
module.exports.waitForUrl = waitForUrl
168
169