* Waits for a WebDriver server to be healthy and accepting requests. * @param {string} url Base URL of the server to query. * @param {number} timeout How long to wait for the server. * @param {Promise=} opt_cancelToken A promise used as a cancellation signal: * if resolved before the server
(url, timeout, opt_cancelToken)
| 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 |
nothing calls this directly
no test coverage detected