forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiDi.java
More file actions
140 lines (115 loc) · 4.44 KB
/
BiDi.java
File metadata and controls
140 lines (115 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.bidi;
import java.io.Closeable;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.openqa.selenium.internal.Require;
public class BiDi implements Closeable {
private final Duration timeout;
private final Connection connection;
/**
* @deprecated Use constructor with timeout parameter: {@link #BiDi(Connection, Duration)}
*/
@Deprecated(forRemoval = true)
public BiDi(Connection connection) {
this(connection, Duration.ofSeconds(30));
}
public BiDi(Connection connection, Duration timeout) {
this.connection = Require.nonNull("WebSocket connection", connection);
this.timeout = Require.nonNull("WebSocket timeout", timeout);
}
@Override
public void close() {
clearListeners();
disconnectSession();
connection.close();
}
public void disconnectSession() {
// TODO: Identify how to close a BiDi session.
// Seems like https://w3c.github.io/webdriver-bidi/#issue-9f7aff26 needs to be fleshed out.
}
public <X> X send(Command<X> command) {
Require.nonNull("Command to send", command);
return connection.sendAndWait(command, timeout);
}
public <X> X send(Command<X> command, Duration timeout) {
Require.nonNull("Command to send", command);
Require.nonNull("Timeout", timeout);
return connection.sendAndWait(command, timeout);
}
public <X> long addListener(Event<X> event, Consumer<X> handler) {
Require.nonNull("Event to listen for", event);
Require.nonNull("Handler to call", handler);
send(
new Command<>(
"session.subscribe", Map.of("events", Collections.singletonList(event.getMethod()))));
return connection.addListener(event, handler);
}
public <X> long addListener(String browsingContextId, Event<X> event, Consumer<X> handler) {
Require.nonNull("Event to listen for", event);
Require.nonNull("Browsing context id", browsingContextId);
Require.nonNull("Handler to call", handler);
send(
new Command<>(
"session.subscribe",
Map.of(
"contexts",
Collections.singletonList(browsingContextId),
"events",
Collections.singletonList(event.getMethod()))));
return connection.addListener(event, handler);
}
public <X> long addListener(Set<String> browsingContextIds, Event<X> event, Consumer<X> handler) {
Require.nonNull("List of browsing context ids", browsingContextIds);
Require.nonNull("Event to listen for", event);
Require.nonNull("Handler to call", handler);
send(
new Command<>(
"session.subscribe",
Map.of(
"contexts",
browsingContextIds,
"events",
Collections.singletonList(event.getMethod()))));
return connection.addListener(event, handler);
}
public <X> void clearListener(Event<X> event) {
Require.nonNull("Event to listen for", event);
// The browser throws an error if we try to unsubscribe an event that was not subscribed in the
// first place
if (connection.isEventSubscribed(event)) {
send(
new Command<>(
"session.unsubscribe",
Map.of("events", Collections.singletonList(event.getMethod()))));
connection.clearListener(event);
}
}
public void removeListener(long id) {
connection.removeListener(id);
}
public void clearListeners() {
connection.clearListeners();
}
public BiDiSessionStatus getBidiSessionStatus() {
return send(new Command<>("session.status", Collections.emptyMap(), BiDiSessionStatus.class));
}
}