Skip to content

Commit 0a2a4a9

Browse files
authored
[java] Add status endpoint
Related to #10832
1 parent f2b65eb commit 0a2a4a9

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

java/src/org/openqa/selenium/remote/RemoteWebDriver.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import org.openqa.selenium.interactions.Interactive;
5151
import org.openqa.selenium.interactions.Sequence;
5252
import org.openqa.selenium.internal.Require;
53+
import org.openqa.selenium.json.Json;
54+
import org.openqa.selenium.json.JsonException;
5355
import org.openqa.selenium.logging.LocalLogs;
5456
import org.openqa.selenium.logging.LogType;
5557
import org.openqa.selenium.logging.LoggingHandler;
@@ -60,6 +62,9 @@
6062
import org.openqa.selenium.remote.http.ClientConfig;
6163
import org.openqa.selenium.remote.http.ConnectionFailedException;
6264
import org.openqa.selenium.remote.http.HttpClient;
65+
import org.openqa.selenium.remote.http.HttpMethod;
66+
import org.openqa.selenium.remote.http.HttpRequest;
67+
import org.openqa.selenium.remote.http.HttpResponse;
6368
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;
6469
import org.openqa.selenium.remote.tracing.TracedHttpClient;
6570
import org.openqa.selenium.remote.tracing.Tracer;
@@ -94,6 +99,7 @@
9499
import static org.openqa.selenium.remote.CapabilityType.LOGGING_PREFS;
95100
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
96101
import static org.openqa.selenium.remote.CapabilityType.SUPPORTS_JAVASCRIPT;
102+
import static org.openqa.selenium.remote.http.Contents.string;
97103

98104
@Augmentable
99105
public class RemoteWebDriver implements WebDriver,
@@ -305,6 +311,20 @@ public Capabilities getCapabilities() {
305311
return capabilities;
306312
}
307313

314+
public static Status status(URL remoteAddress) {
315+
HttpResponse response = new HttpResponse();
316+
try (HttpClient client = HttpClient.Factory.createDefault().createClient(remoteAddress)) {
317+
HttpRequest request = new HttpRequest(HttpMethod.GET, "/status");
318+
response = client.execute(request);
319+
Map<String, Object> responseMap = new Json().toType(string(response), Map.class);
320+
Map<String, Object> value = (Map<String, Object>) responseMap.get("value");
321+
322+
return new Status((boolean) value.get("ready"), (String) value.get("message"));
323+
} catch (JsonException e) {
324+
throw new WebDriverException("Unable to parse remote response: " + string(response), e);
325+
}
326+
}
327+
308328
@Override
309329
public void get(String url) {
310330
execute(DriverCommand.GET(url));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.openqa.selenium.remote;
19+
20+
public class Status {
21+
private final boolean ready;
22+
private final String message;
23+
24+
public Status(boolean ready, String message) {
25+
this.ready = ready;
26+
this.message = message;
27+
}
28+
29+
public boolean isReady() {
30+
return ready;
31+
}
32+
33+
public String getMessage() {
34+
return message;
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
package org.openqa.selenium.grid.router;
19+
20+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.openqa.selenium.grid.config.TomlConfig;
24+
import org.openqa.selenium.grid.router.DeploymentTypes.Deployment;
25+
import org.openqa.selenium.remote.RemoteWebDriver;
26+
import org.openqa.selenium.remote.Status;
27+
import org.openqa.selenium.testing.drivers.Browser;
28+
29+
import java.io.StringReader;
30+
31+
class RemoteWebDriverStatusTest {
32+
33+
@Test
34+
void shouldBeAbleToGetRemoteEndStatus() {
35+
Browser browser = Browser.FIREFOX;
36+
37+
Deployment deployment = DeploymentTypes.STANDALONE.start(
38+
browser.getCapabilities(),
39+
new TomlConfig(new StringReader(
40+
"[node]\n" +
41+
"driver-implementation = " + browser.displayName())));
42+
43+
Status status = RemoteWebDriver.status(deployment.getServer().getUrl());
44+
assertThat(status.isReady()).isTrue();
45+
assertThat(status.getMessage()).isEqualTo("Selenium Grid ready.");
46+
}
47+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
package org.openqa.selenium.remote;
19+
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Tag;
24+
import org.junit.jupiter.api.Test;
25+
import org.openqa.selenium.firefox.FirefoxDriver;
26+
import org.openqa.selenium.remote.service.DriverService;
27+
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.io.UncheckedIOException;
31+
import java.net.URI;
32+
import java.net.URL;
33+
34+
@Tag("UnitTests")
35+
public class WebDriverStatusTest {
36+
37+
@Test
38+
public void shouldGetStatusForWebDriverInstance() throws IOException {
39+
URI uri = URI.create("http://localhost:9898");
40+
URL url = uri.toURL();
41+
42+
DriverService service = new FakeDriverService() {
43+
@Override
44+
public URL getUrl() {
45+
return url;
46+
}
47+
};
48+
49+
assertThrows(UncheckedIOException.class,
50+
() -> FirefoxDriver.status(service.getUrl()));
51+
}
52+
53+
private static class FakeDriverService extends DriverService {
54+
55+
private boolean started;
56+
57+
FakeDriverService() throws IOException {
58+
super(new File("."), 0, DEFAULT_TIMEOUT, null, null);
59+
}
60+
61+
@Override
62+
public void start() {
63+
started = true;
64+
}
65+
66+
@Override
67+
public boolean isRunning() {
68+
return started;
69+
}
70+
71+
@Override
72+
protected void waitUntilAvailable() {
73+
// return immediately
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)