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
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/chromium/AddHasCasting.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.openqa.selenium.chromium;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNullElse;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -56,7 +55,7 @@ public HasCasting getImplementation(Capabilities capabilities, ExecuteMethod exe
return new HasCasting() {
@Override
public List<Map<String, String>> getCastSinks() {
return requireNonNullElse(executeMethod.execute(GET_CAST_SINKS, null), emptyList());
return executeMethod.execute(GET_CAST_SINKS, null, emptyList());
}

@Override
Expand All @@ -82,7 +81,7 @@ public void startTabMirroring(String deviceName) {

@Override
public String getCastIssueMessage() {
return executeMethod.executeRequired(GET_CAST_ISSUE_MESSAGE, null).toString();
return executeMethod.execute(GET_CAST_ISSUE_MESSAGE).toString();
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions java/src/org/openqa/selenium/chromium/AddHasCdp.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public HasCdp getImplementation(Capabilities capabilities, ExecuteMethod execute
Require.nonNull("Command name", commandName);
Require.nonNull("Parameters", parameters);

return executeMethod.executeRequired(
EXECUTE_CDP, Map.of("cmd", commandName, "params", parameters));
return executeMethod.executeAs(EXECUTE_CDP, Map.of("cmd", commandName, "params", parameters));
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public HasNetworkConditions getImplementation(
return new HasNetworkConditions() {
@Override
public ChromiumNetworkConditions getNetworkConditions() {
@SuppressWarnings("unchecked")
Map<String, Object> result = executeMethod.executeRequired(GET_NETWORK_CONDITIONS, null);
Map<String, Object> result = executeMethod.execute(GET_NETWORK_CONDITIONS);
return new ChromiumNetworkConditions()
.setOffline((Boolean) result.getOrDefault(OFFLINE, false))
.setLatency(Duration.ofMillis((Long) result.getOrDefault(LATENCY, 0)))
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/firefox/AddHasContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setContext(FirefoxCommandContext context) {

@Override
public FirefoxCommandContext getContext() {
String context = executeMethod.executeRequired(GET_CONTEXT, null);
String context = executeMethod.execute(GET_CONTEXT);
return FirefoxCommandContext.fromString(context);
}
};
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/firefox/AddHasExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public String installExtension(Path path, Boolean temporary) {
throw new InvalidArgumentException(path + " is an invalid path", e);
}

return executeMethod.executeRequired(
return executeMethod.executeAs(
INSTALL_EXTENSION, Map.of("addon", encoded, "temporary", temporary));
}

Expand Down
33 changes: 30 additions & 3 deletions java/src/org/openqa/selenium/remote/ExecuteMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.remote;

import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;

import java.util.Map;
import org.jspecify.annotations.NullMarked;
Expand All @@ -37,9 +38,35 @@ public interface ExecuteMethod {
* @param parameters The parameters to execute that command with
* @return The result of {@link Response#getValue()}.
*/
@Nullable <T> T execute(String commandName, @Nullable Map<String, ?> parameters);
@Nullable Object execute(String commandName, @Nullable Map<String, ?> parameters);

default <T> T executeRequired(String commandName, @Nullable Map<String, ?> parameters) {
return requireNonNull(execute(commandName, parameters));
/**
* Execute the given command and return the default value if the command return null.
*
* @return non-nullable value of type T.
*/
@SuppressWarnings("unchecked")
default <T> T execute(String commandName, @Nullable Map<String, ?> parameters, T defaultValue) {
return (T) requireNonNullElse(execute(commandName, parameters), defaultValue);
}

/**
* Execute the given command and cast the returned value to T.
*
* @return non-nullable value of type T.
*/
@SuppressWarnings("unchecked")
default <T> T executeAs(String commandName, @Nullable Map<String, ?> parameters) {
return (T) requireNonNull(execute(commandName, parameters));
}
Comment thread
qodo-code-review[bot] marked this conversation as resolved.

/**
* Execute the given command without parameters and cast the returned value to T.
*
* @return non-nullable value of type T.
*/
@SuppressWarnings("unchecked")
default <T> T execute(String commandName) {
return (T) requireNonNull(execute(commandName, null));
}
}
9 changes: 4 additions & 5 deletions java/src/org/openqa/selenium/remote/FedCmDialogImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void selectAccount(int index) {
@Nullable
@Override
public String getDialogType() {
return executeMethod.execute(DriverCommand.GET_FEDCM_DIALOG_TYPE, null);
return (String) executeMethod.execute(DriverCommand.GET_FEDCM_DIALOG_TYPE, null);
}

@Override
Expand All @@ -58,21 +58,20 @@ public void clickDialog() {
@Nullable
@Override
public String getTitle() {
Map<String, String> result = executeMethod.executeRequired(DriverCommand.GET_FEDCM_TITLE, null);
Map<String, String> result = executeMethod.execute(DriverCommand.GET_FEDCM_TITLE);
return result.get("title");
}

@Nullable
@Override
public String getSubtitle() {
Map<String, String> result = executeMethod.executeRequired(DriverCommand.GET_FEDCM_TITLE, null);
Map<String, String> result = executeMethod.execute(DriverCommand.GET_FEDCM_TITLE);
return result.get("subtitle");
}

@Override
public List<FederatedCredentialManagementAccount> getAccounts() {
List<Map<String, String>> accounts =
executeMethod.executeRequired(DriverCommand.GET_ACCOUNTS, null);
List<Map<String, String>> accounts = executeMethod.execute(DriverCommand.GET_ACCOUNTS);

return accounts.stream()
.map(map -> new FederatedCredentialManagementAccount(map))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class LocalExecuteMethod implements ExecuteMethod {
@Nullable
@Override
public <T> T execute(String commandName, @Nullable Map<String, ?> parameters) {
public Object execute(String commandName, @Nullable Map<String, ?> parameters) {
throw new WebDriverException("Cannot execute remote command: " + commandName);
}
}
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/remote/RemoteExecuteMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public RemoteExecuteMethod(RemoteWebDriver driver) {
this.driver = Require.nonNull("Remote WebDriver", driver);
}

@SuppressWarnings("unchecked")
@Override
public @Nullable <T> T execute(String commandName, @Nullable Map<String, ?> parameters) {
public @Nullable Object execute(String commandName, @Nullable Map<String, ?> parameters) {
Response response;

if (parameters == null || parameters.isEmpty()) {
Expand All @@ -42,7 +41,7 @@ public RemoteExecuteMethod(RemoteWebDriver driver) {
response = driver.execute(commandName, parameters);
}

return (T) response.getValue();
return response.getValue();
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions java/src/org/openqa/selenium/remote/RemoteLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ private Set<String> getAvailableLocalLogs() {

@Override
public Set<String> getAvailableLogTypes() {
List<String> rawList =
executeMethod.executeRequired(DriverCommand.GET_AVAILABLE_LOG_TYPES, null);
List<String> rawList = executeMethod.execute(DriverCommand.GET_AVAILABLE_LOG_TYPES);
Set<String> builder = new LinkedHashSet<>();
builder.addAll(rawList);
builder.addAll(getAvailableLocalLogs());
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/safari/AddHasPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void setPermissions(String permission, boolean value) {

@Override
public Map<String, Boolean> getPermissions() {
Map<?, ?> resultMap = executeMethod.executeRequired(GET_PERMISSIONS, null);
Map<?, ?> resultMap = executeMethod.execute(GET_PERMISSIONS);

Map<String, Boolean> permissionMap = new HashMap<>();
for (Map.Entry<?, ?> entry : resultMap.entrySet()) {
Expand Down
2 changes: 1 addition & 1 deletion java/test/org/openqa/selenium/remote/RemoteLogsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void throwsOnBogusRemoteLogsResponse() {
@Test
void canGetAvailableLogTypes() {
List<String> remoteAvailableLogTypes = List.of(LogType.PROFILER, LogType.SERVER);
when(executeMethod.executeRequired(DriverCommand.GET_AVAILABLE_LOG_TYPES, null))
when(executeMethod.execute(DriverCommand.GET_AVAILABLE_LOG_TYPES))
.thenReturn(remoteAvailableLogTypes);

Set<String> localAvailableLogTypes = Set.of(LogType.PROFILER, LogType.CLIENT);
Expand Down
Loading