Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import io.appium.java_client.AppiumClientConfig;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -37,6 +38,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand Down Expand Up @@ -261,4 +263,23 @@ void failsToProvideBiDiWithoutTheWebSocketUrlCapability() throws IOException {

assertThrows(BiDiException.class, seleniumDriver::getHandle);
}

@Test
void propagatesDirectConnectServerUrlToSeleniumClientConfig() throws IOException {
extraCapabilities = "\"appium:directConnectProtocol\":\"https\","
+ "\"appium:directConnectHost\":\"203.0.113.1\","
+ "\"appium:directConnectPort\":4443,"
+ "\"appium:directConnectPath\":\"/wd/hub\",";
var clientConfig = AppiumClientConfig.defaultConfig()
.baseUri(URI.create("http://127.0.0.1:" + server.getAddress().getPort()))
.directConnect(true);
var appiumDriver = new AndroidDriver(clientConfig,
new UiAutomator2Options().setDeviceName("Android Emulator"));

var seleniumDriver = SeleniumBridge.asRemoteWebDriver(appiumDriver);

assertEquals(URI.create("https://203.0.113.1:4443/wd/hub"),
seleniumDriver.getClientConfig().baseUri());
seleniumDriver.closeBiDi();
}
}
16 changes: 11 additions & 5 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import io.appium.java_client.remote.options.BaseOptions;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import lombok.Getter;
import org.jspecify.annotations.Nullable;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.OutputType;
Expand Down Expand Up @@ -64,8 +64,6 @@ public class AppiumDriver extends AppiumRemoteWebDriver implements

private static final ErrorHandler ERROR_HANDLER = new ErrorHandler(new ErrorCodesMobile());
// frequently used command parameters
@Getter
private final URL remoteAddress;
private final ExecuteMethod executeMethod;

/**
Expand All @@ -80,7 +78,6 @@ public AppiumDriver(AppiumCommandExecutor executor, Capabilities capabilities) {
super(executor, capabilities);
this.executeMethod = new AppiumExecutionMethod(this);
super.setErrorHandler(ERROR_HANDLER);
this.remoteAddress = executor.getAddressOfRemoteServer();
}

/**
Expand Down Expand Up @@ -216,11 +213,20 @@ public AppiumDriver(URL remoteSessionAddress, String platformName, String automa
setCommandExecutor(executor);
this.executeMethod = new AppiumExecutionMethod(this);
super.setErrorHandler(ERROR_HANDLER);
this.remoteAddress = executor.getAddressOfRemoteServer();

setSessionId(sessionAddress.getId());
}

/**
* Gets the remote server address to which commands are sent.
*
* @return the remote server URL, or {@code null} if not configured
*/
@Nullable
public URL getRemoteAddress() {
Comment thread
mykola-mokhnach marked this conversation as resolved.
Resolved
return ((AppiumCommandExecutor) getCommandExecutor()).getAddressOfRemoteServer();
}

@Override
public ExecuteMethod getExecuteMethod() {
return executeMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@

import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.ExecutesMethod;
import io.appium.java_client.remote.AppiumCommandExecutor;
import io.appium.java_client.remote.AppiumRemoteWebDriver;
import io.appium.java_client.remote.SessionId;
import io.appium.java_client.internal.LogBroadcastEndpoints;
import io.appium.java_client.ws.StringWebSocketClient;

import java.net.URI;
import java.net.URL;
import java.util.function.Consumer;

Expand All @@ -40,21 +37,29 @@ public interface ListensToLogcatMessages extends ExecutesMethod {

/**
* Start logcat messages broadcast via web socket.
* This method assumes that Appium server is running on localhost and
* is assigned to the default port (4723).
* This method uses the current remote server host (mapping wildcard addresses to {@code 127.0.0.1})
* and port when available, falling back to {@code 127.0.0.1:4723}.
*/
default void startLogcatBroadcast() {
startLogcatBroadcast("127.0.0.1");
URL serverUrl = LogBroadcastEndpoints.resolveServerUrl(this);
startLogcatBroadcast(
LogBroadcastEndpoints.resolveHost(serverUrl, "127.0.0.1"),
LogBroadcastEndpoints.resolvePort(serverUrl, DEFAULT_APPIUM_PORT)
);
}

/**
* Start logcat messages broadcast via web socket.
* This method assumes that Appium server is assigned to the default port (4723).
* This method uses the current remote server port when available,
* falling back to the default port (4723).
*
* @param host the name of the host where Appium server is running
*/
default void startLogcatBroadcast(String host) {
startLogcatBroadcast(host, DEFAULT_APPIUM_PORT);
startLogcatBroadcast(
host,
LogBroadcastEndpoints.resolvePort(LogBroadcastEndpoints.resolveServerUrl(this), DEFAULT_APPIUM_PORT)
);
}

/**
Expand All @@ -64,13 +69,7 @@ default void startLogcatBroadcast(String host) {
* @param port the port of the host where Appium server is running
*/
default void startLogcatBroadcast(String host, int port) {
var remoteWebDriver = (AppiumRemoteWebDriver) this;
URL serverUrl = ((AppiumCommandExecutor) remoteWebDriver.getCommandExecutor()).getAddressOfRemoteServer();
var scheme = "https".equals(serverUrl.getProtocol()) ? "wss" : "ws";
CommandExecutionHelper.executeScript(this, "mobile: startLogsBroadcast");
SessionId sessionId = remoteWebDriver.getSessionId();
var endpoint = String.format("%s://%s:%s/ws/session/%s/appium/device/logcat", scheme, host, port, sessionId);
getLogcatClient().connect(URI.create(endpoint));
LogBroadcastEndpoints.startBroadcast(this, getLogcatClient(), host, port, "logcat");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* 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 io.appium.java_client.internal;

import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.ExecutesMethod;
import io.appium.java_client.remote.AppiumCommandExecutor;
import io.appium.java_client.remote.AppiumRemoteWebDriver;
import io.appium.java_client.remote.DirectConnect;
import io.appium.java_client.remote.SessionId;
import io.appium.java_client.ws.StringWebSocketClient;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

import java.net.URI;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.Set;

/**
* Shared endpoint resolution and WebSocket startup helpers for device log broadcast listeners.
*/
@NullMarked
public final class LogBroadcastEndpoints {
private static final Set<String> LOOPBACK_HOSTS = Set.of("localhost", "127.0.0.1", "::1", "0:0:0:0:0:0:0:1");
private static final Set<String> WILDCARD_HOSTS = Set.of("0.0.0.0", "::", "0:0:0:0:0:0:0:0");

private LogBroadcastEndpoints() {
}

/**
* Resolves the active remote server URL from a driver's command executor.
*
* @param driver the driver executing log broadcast commands
* @return the current remote server URL, or {@code null} if unavailable
*/
@Nullable
public static URL resolveServerUrl(ExecutesMethod driver) {
return getExecutor(driver).getAddressOfRemoteServer();
}

/**
* Resolves the host for a log broadcast WebSocket connection from the server URL,
* mapping missing or wildcard bind hosts ({@code 0.0.0.0}, {@code [::]}) to {@code fallbackHost}.
*
* @param serverUrl the active remote server URL, or {@code null} if unknown
* @param fallbackHost the loopback host to return when {@code serverUrl} has no usable host
* @return the resolved broadcast host
*/
public static String resolveHost(@Nullable URL serverUrl, String fallbackHost) {
String host = serverUrl != null ? serverUrl.getHost() : "";
if (host.isEmpty() || WILDCARD_HOSTS.contains(normalizeHost(host))) {
return fallbackHost;
}
return host;
}

/**
* Resolves the port for a log broadcast WebSocket connection from the server URL.
*
* @param serverUrl the active remote server URL, or {@code null} if unknown
* @param fallbackPort the port to return when {@code serverUrl} defines neither an explicit nor default port
* @return the resolved broadcast port
*/
public static int resolvePort(@Nullable URL serverUrl, int fallbackPort) {
if (serverUrl == null) {
return fallbackPort;
}
if (serverUrl.getPort() > 0) {
return serverUrl.getPort();
}
return serverUrl.getDefaultPort() > 0 ? serverUrl.getDefaultPort() : fallbackPort;
}

/**
* Starts server-side log streaming and connects the WebSocket client to the resolved broadcast endpoint.
*
* @param driver the driver executing {@code mobile: startLogsBroadcast}
* @param client the target WebSocket client
* @param host the target WebSocket host
* @param port the target WebSocket port
* @param deviceLogType the log endpoint suffix (such as {@code logcat} or {@code syslog})
*/
public static void startBroadcast(
ExecutesMethod driver,
StringWebSocketClient client,
String host,
int port,
String deviceLogType
) {
var remoteWebDriver = (AppiumRemoteWebDriver) driver;
var executor = getExecutor(driver);
CommandExecutionHelper.executeScript(driver, "mobile: startLogsBroadcast");
URI endpoint = buildBroadcastUri(
executor.getAddressOfRemoteServer(),
executor.getReplacedHttpServerUrls(),
host,
port,
remoteWebDriver.getSessionId(),
deviceLogType
);
client.connect(endpoint);
}

/**
* Builds a device log broadcast WebSocket URI. Uses {@code wss} when {@code serverUrl} uses
* {@code https}, unless the requested {@code host:port} matches a plain-{@code http} server URL
* previously replaced (e.g. by {@code directConnect}) and does not match the active {@code serverUrl}.
*
* @param serverUrl the active remote server URL, or {@code null} if unknown
* @param replacedHttpServerUrls plain-{@code http} server URLs previously replaced on the executor
* @param host the target WebSocket host
* @param port the target WebSocket port
* @param sessionId the active session identifier
* @param deviceLogType the log endpoint suffix (such as {@code logcat} or {@code syslog})
* @return the WebSocket endpoint URI
*/
public static URI buildBroadcastUri(
@Nullable URL serverUrl,
List<URL> replacedHttpServerUrls,
String host,
int port,
@Nullable SessionId sessionId,
String deviceLogType
) {
String scheme = chooseScheme(serverUrl, replacedHttpServerUrls, host, port);
String endpoint = String.format(
"%s://%s:%s/ws/session/%s/appium/device/%s",
scheme, DirectConnect.formatHost(host), port, sessionId, deviceLogType
);
return URI.create(endpoint);
}

private static AppiumCommandExecutor getExecutor(ExecutesMethod driver) {
var remoteWebDriver = (AppiumRemoteWebDriver) driver;
return (AppiumCommandExecutor) remoteWebDriver.getCommandExecutor();
}

private static String chooseScheme(
@Nullable URL serverUrl,
List<URL> replacedHttpServerUrls,
String host,
int port
) {
if (serverUrl == null || !"https".equalsIgnoreCase(serverUrl.getProtocol())) {
return "ws";
}
if (!matchesEndpoint(serverUrl, host, port)
&& replacedHttpServerUrls.stream().anyMatch(url -> matchesEndpoint(url, host, port))) {
return "ws";
}
return "wss";
}

private static boolean matchesEndpoint(URL candidateUrl, String host, int port) {
if (resolvePort(candidateUrl, -1) != port) {
return false;
}
String candidateHost = normalizeComparableHost(candidateUrl.getHost());
String targetHost = normalizeComparableHost(host);
if (candidateHost.isEmpty() || targetHost.isEmpty()) {
return false;
}
return candidateHost.equals(targetHost)
|| LOOPBACK_HOSTS.contains(candidateHost) && LOOPBACK_HOSTS.contains(targetHost);
}

private static String normalizeComparableHost(String host) {
String normalized = normalizeHost(host);
return WILDCARD_HOSTS.contains(normalized) ? "127.0.0.1" : normalized;
}

private static String normalizeHost(String host) {
String trimmed = host.trim().toLowerCase(Locale.ROOT);
if (trimmed.startsWith("[") && trimmed.endsWith("]") && trimmed.length() > 2) {
return trimmed.substring(1, trimmed.length() - 1);
}
return trimmed;
}
}
Loading
Loading