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 16 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.
Jump to
Jump to file
Failed to load files.
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);
}
}
48 changes: 0 additions & 48 deletions java/src/org/openqa/selenium/chromium/ChromiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.openqa.selenium.remote.Browser.OPERA;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -42,9 +41,6 @@
import org.openqa.selenium.ScriptKey;
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;
import org.openqa.selenium.devtools.CdpVersionFinder;
Expand All @@ -67,7 +63,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 All @@ -78,7 +73,6 @@
*/
public class ChromiumDriver extends RemoteWebDriver
implements HasAuthentication,
HasBiDi,
HasCasting,
HasCdp,
HasDevTools,
Expand All @@ -103,8 +97,6 @@ public class ChromiumDriver extends RemoteWebDriver
private final HasLaunchApp launch;
private Optional<Connection> connection;
private final Optional<DevTools> devTools;
private final Optional<URI> biDiUri;
private final Optional<BiDi> biDi;
protected HasCasting casting;
protected HasCdp cdp;
private final Map<Integer, ScriptKey> scriptKeys = new HashMap<>();
Expand All @@ -123,22 +115,6 @@ protected ChromiumDriver(
HttpClient.Factory factory = HttpClient.Factory.createDefault();
Capabilities originalCapabilities = super.getCapabilities();

Optional<String> webSocketUrl =
Optional.ofNullable((String) originalCapabilities.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);

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

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

URI wsUri =
biDiUri.orElseThrow(
() -> new BiDiException("This version of Chromium driver does not support BiDi"));

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 List<Map<String, String>> getCastSinks() {
return casting.getCastSinks();
Expand Down
59 changes: 1 addition & 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 @@ -36,9 +35,6 @@
import org.openqa.selenium.PersistentCapabilities;
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;
import org.openqa.selenium.devtools.CdpVersionFinder;
Expand Down Expand Up @@ -79,7 +75,7 @@
* </pre>
*/
public class FirefoxDriver extends RemoteWebDriver
implements WebStorage, HasExtensions, HasFullPageScreenshot, HasContext, HasDevTools, HasBiDi {
implements WebStorage, HasExtensions, HasFullPageScreenshot, HasContext, HasDevTools {

private static final Logger LOG = Logger.getLogger(FirefoxDriver.class.getName());
private final Capabilities capabilities;
Expand All @@ -88,10 +84,8 @@ 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;

/**
* Creates a new FirefoxDriver using the {@link GeckoDriverService#createDefaultService)} server
Expand Down Expand Up @@ -189,22 +183,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 @@ -353,41 +331,6 @@ public DevTools getDevTools() {
.orElseThrow(() -> new DevToolsException("Unable to initialize CDP connection"));
}

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

URI wsUri =
biDiUri.orElseThrow(
() ->
new BiDiException("This version of Firefox or geckodriver does not support BiDi"));

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.isPresent()) {
throw new BiDiException("This version of Firefox or geckodriver does not support Bidi");
}

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

@Override
public void quit() {
super.quit();
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);
}
}