Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions java/src/org/openqa/selenium/bidi/BiDi.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,8 @@ public void clearListeners() {
public BiDiSessionStatus getBidiSessionStatus() {
return send(new Command<>("session.status", emptyMap(), BiDiSessionStatus.class));
}

public Handle asHandle() {
return new Handle(this);
}
}
5 changes: 5 additions & 0 deletions java/src/org/openqa/selenium/bidi/BiDiProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public Optional<BiDi> maybeGetBiDi() {
public BiDi getBiDi() {
return biDi.get();
}

@Override
public Handle getHandle() {
return biDi.get().asHandle();
}
};
}

Expand Down
51 changes: 51 additions & 0 deletions java/src/org/openqa/selenium/bidi/Handle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.util.function.Consumer;
import org.openqa.selenium.Beta;

/**
* An opaque handle to the active BiDi connection, used by {@link Module} subclasses to send
* commands and subscribe to events.
*
* <p>Constructor and all methods are package-private: external callers that hold a {@code Handle}
* reference cannot invoke it, only pass it to module constructors inside this package. Create an
* instance via {@link BiDi#asHandle()}.
*/
@Beta
public class Handle {

Comment thread
pujagani marked this conversation as resolved.
private final BiDi bidi;

Handle(BiDi bidi) {
this.bidi = bidi;
}

<X> X send(Command<X> command) {
return bidi.send(command);
}

<X> String subscribe(Event<X> event, Consumer<X> handler) {
return bidi.addListener(event, handler);
}
Comment thread
pujagani marked this conversation as resolved.

void unsubscribe(String subscriptionId) {
bidi.removeListener(subscriptionId);
}
}
2 changes: 2 additions & 0 deletions java/src/org/openqa/selenium/bidi/HasBiDi.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ default BiDi getBiDi() {
*/
@Deprecated(since = "4.46", forRemoval = true)
Optional<BiDi> maybeGetBiDi();

Handle getHandle();
Comment thread
qodo-code-review[bot] marked this conversation as resolved.
}
55 changes: 55 additions & 0 deletions java/src/org/openqa/selenium/bidi/Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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.util.function.Consumer;
import org.openqa.selenium.Beta;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.internal.Require;

/**
* Base class for generated BiDi module classes.
*
* <p>Subclasses delegate all wire operations through the driver's shared {@link Handle} — no new
* WebSocket connection is opened.
*/
@Beta
public abstract class Module {

private final Handle handle;

protected Module(WebDriver driver) {
Require.nonNull("WebDriver", driver);
if (!(driver instanceof HasBiDi)) {
throw new BiDiException("WebDriver does not support BiDi protocol");
}
this.handle = ((HasBiDi) driver).getHandle();
}

protected final <X> X send(Command<X> command) {
return handle.send(command);
}

protected final <X> String subscribe(Event<X> event, Consumer<X> handler) {
return handle.subscribe(event, handler);
}

protected final void unsubscribe(String subscriptionId) {
handle.unsubscribe(subscriptionId);
}
Comment thread
pujagani marked this conversation as resolved.
}
18 changes: 18 additions & 0 deletions java/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.BiDiException;
import org.openqa.selenium.bidi.Connection;
import org.openqa.selenium.bidi.Handle;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
Expand Down Expand Up @@ -475,6 +477,22 @@ public Optional<BiDi> maybeGetBiDi() {
return biDi;
}

// Calls maybeGetBiDi() rather than reading the private field directly so that subclasses
// that override maybeGetBiDi() (e.g. AppiumDriver) are not broken before they migrate to
// overriding getHandle(). Remove the @SuppressWarnings and switch to the field once
// maybeGetBiDi() is deleted.
@SuppressWarnings("deprecation")
@Override
public Handle getHandle() {
return maybeGetBiDi()
.map(BiDi::asHandle)
.orElseThrow(
() ->
new BiDiException(
"Check if this browser version supports BiDi and if the"
+ " 'webSocketUrl: true' capability is set."));
}
Comment thread
pujagani marked this conversation as resolved.

// Misc

@Override
Expand Down
Loading