Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] POC: Porting WebDriver classic command to BiDi #13190

Open
wants to merge 15 commits into
base: trunk
Choose a base branch
from
Open
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: 2 additions & 2 deletions java/src/org/openqa/selenium/bidi/HasBiDi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import java.util.Optional;

public interface HasBiDi {
Optional<BiDi> maybeGetBiDi();

default BiDi getBiDi() {
return maybeGetBiDi()
.orElseThrow(() -> new BiDiException("Unable to create a BiDi connection"));
}

Optional<BiDi> maybeGetBiDi();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,39 @@
package org.openqa.selenium.bidi.browsingcontext;

public enum ReadinessState {
NONE("none"),
INTERACTIVE("interactive"),
COMPLETE("complete");
// Mapping the page load strategy values used in BiDi To Classic
// Refer: https://w3c.github.io/webdriver-bidi/#type-browsingContext-ReadinessState
// Refer: https://www.w3.org/TR/webdriver2/#navigation
NONE("none", "none"),
INTERACTIVE("interactive", "eager"),
COMPLETE("complete", "normal");
pujagani marked this conversation as resolved.
Show resolved Hide resolved

private final String text;
private final String readinessState;

ReadinessState(String text) {
this.text = text;
private final String pageLoadStrategy;

ReadinessState(String readinessState, String pageLoadStrategy) {
this.readinessState = readinessState;
this.pageLoadStrategy = pageLoadStrategy;
}

public String getPageLoadStrategy() {
return pageLoadStrategy;
}

public static ReadinessState getReadinessState(String pageLoadStrategy) {
if (pageLoadStrategy != null) {
for (ReadinessState b : ReadinessState.values()) {
if (pageLoadStrategy.equalsIgnoreCase(b.pageLoadStrategy)) {
return b;
}
}
}
return null;
}

@Override
public String toString() {
return String.valueOf(text);
return String.valueOf(readinessState);
}
}
32 changes: 5 additions & 27 deletions java/src/org/openqa/selenium/chromium/ChromiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.BiDiException;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.devtools.CdpEndpointFinder;
import org.openqa.selenium.devtools.CdpInfo;
Expand All @@ -67,7 +66,6 @@
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.html5.RemoteWebStorage;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.ConnectionFailedException;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.mobile.RemoteNetworkConnection;
Expand Down Expand Up @@ -104,7 +102,7 @@ public class ChromiumDriver extends RemoteWebDriver
private Optional<Connection> connection;
private final Optional<DevTools> devTools;
private final Optional<URI> biDiUri;
private final Optional<BiDi> biDi;
private Optional<BiDi> biDi;
protected HasCasting casting;
protected HasCdp cdp;
private final Map<Integer, ScriptKey> scriptKeys = new HashMap<>();
Expand Down Expand Up @@ -137,8 +135,6 @@ protected ChromiumDriver(
return null;
});

this.biDi = createBiDi(biDiUri);

Optional<URI> reportedUri =
CdpEndpointFinder.getReportedUri(capabilityKey, originalCapabilities);
Optional<HttpClient> client =
Expand Down Expand Up @@ -346,32 +342,14 @@ public Optional<DevTools> maybeGetDevTools() {
return devTools;
}

private Optional<BiDi> createBiDi(Optional<URI> biDiUri) {
if (biDiUri.isEmpty()) {
return Optional.empty();
}

URI wsUri =
biDiUri.orElseThrow(
() ->
new BiDiException(
"Check if this browser version supports BiDi and if the 'webSocketUrl: true'"
+ " capability is set."));

HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
ClientConfig wsConfig = ClientConfig.defaultConfig().baseUri(wsUri);
HttpClient wsClient = clientFactory.createClient(wsConfig);

org.openqa.selenium.bidi.Connection biDiConnection =
new org.openqa.selenium.bidi.Connection(wsClient, wsUri.toString());

return Optional.of(new BiDi(biDiConnection));
}

@Override
public Optional<BiDi> maybeGetBiDi() {
return biDi;
}

public void setBiDi(BiDi bidi) {
this.biDi = Optional.of(bidi);
}

@Override
public List<Map<String, String>> getCastSinks() {
Expand Down
63 changes: 5 additions & 58 deletions java/src/org/openqa/selenium/firefox/FirefoxDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.openqa.selenium.remote.CapabilityType.PROXY;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
Expand All @@ -37,7 +36,6 @@
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.bidi.BiDi;
import org.openqa.selenium.bidi.BiDiException;
import org.openqa.selenium.bidi.HasBiDi;
import org.openqa.selenium.devtools.CdpEndpointFinder;
import org.openqa.selenium.devtools.CdpInfo;
Expand Down Expand Up @@ -87,10 +85,10 @@ public class FirefoxDriver extends RemoteWebDriver
private final HasFullPageScreenshot fullPageScreenshot;
private final HasContext context;
private final Optional<URI> cdpUri;
private final Optional<URI> biDiUri;
private Connection connection;
private DevTools devTools;
private final Optional<BiDi> biDi;

private Optional<BiDi> biDi = Optional.empty();

/**
* Creates a new FirefoxDriver using the {@link GeckoDriverService#createDefaultService)} server
Expand Down Expand Up @@ -189,22 +187,6 @@ private FirefoxDriver(
e);
}

Optional<String> webSocketUrl =
Optional.ofNullable((String) capabilities.getCapability("webSocketUrl"));

this.biDiUri =
webSocketUrl.map(
uri -> {
try {
return new URI(uri);
} catch (URISyntaxException e) {
LOG.warning(e.getMessage());
}
return null;
});

this.biDi = createBiDi(biDiUri);

this.cdpUri = cdpUri;
this.capabilities =
cdpUri
Expand Down Expand Up @@ -352,44 +334,9 @@ public DevTools getDevTools() {
return maybeGetDevTools()
.orElseThrow(() -> new DevToolsException("Unable to initialize CDP connection"));
}

private Optional<BiDi> createBiDi(Optional<URI> biDiUri) {
if (biDiUri.isEmpty()) {
return Optional.empty();
}

URI wsUri =
biDiUri.orElseThrow(
() ->
new BiDiException(
"Check if this browser version supports BiDi and if the 'webSocketUrl: true'"
+ " capability is set."));

HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
ClientConfig wsConfig = ClientConfig.defaultConfig().baseUri(wsUri);
HttpClient wsClient = clientFactory.createClient(wsConfig);

org.openqa.selenium.bidi.Connection biDiConnection =
new org.openqa.selenium.bidi.Connection(wsClient, wsUri.toString());

return Optional.of(new BiDi(biDiConnection));
}

@Override
public Optional<BiDi> maybeGetBiDi() {
return biDi;
}

@Override
public BiDi getBiDi() {
if (biDiUri.isEmpty()) {
throw new BiDiException(
"Check if this browser version supports BiDi and if the 'webSocketUrl: true' capability"
+ " is set.");
}

return maybeGetBiDi()
.orElseThrow(() -> new BiDiException("Unable to initialize Bidi connection"));

public void setBiDi(BiDi bidi) {
this.biDi = Optional.ofNullable(bidi);
}

@Override
Expand Down
36 changes: 36 additions & 0 deletions java/src/org/openqa/selenium/remote/BiDiCommandExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.remote;

import java.io.IOException;

public class BiDiCommandExecutor implements CommandExecutor {

private final RemoteWebDriver driver;
private final BiDiCommandMapper commandMapper;

public BiDiCommandExecutor(RemoteWebDriver driver) {
this.driver = driver;
commandMapper = new BiDiCommandMapper(driver);
}

@Override
public Response execute(Command command) throws IOException {
return commandMapper.map(command).apply(command, driver);
}
}
Loading
Loading