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,13 +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;

import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
Expand Down Expand Up @@ -64,13 +60,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,93 @@
/*
* 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.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;

/**
* Shared endpoint resolution and WebSocket startup helpers for device log broadcast listeners.
*/
@NullMarked
public final class LogBroadcastEndpoints {
private LogBroadcastEndpoints() {
}

/**
* Starts server-side log streaming and connects the WebSocket client to the 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 = (AppiumCommandExecutor) remoteWebDriver.getCommandExecutor();
CommandExecutionHelper.executeScript(driver, "mobile: startLogsBroadcast");
URI endpoint = buildBroadcastUri(
executor.getAddressOfRemoteServer(),
host,
port,
remoteWebDriver.getSessionId(),
deviceLogType
);
client.connect(endpoint);
}

/**
* Builds a device log broadcast WebSocket URI using {@code wss} when {@code serverUrl} uses
* {@code https}, or {@code ws} otherwise.
*
* @param serverUrl the active remote server URL, or {@code null} if unknown
* @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,
String host,
int port,
@Nullable SessionId sessionId,
String deviceLogType
) {
String scheme = serverUrl != null && "https".equalsIgnoreCase(serverUrl.getProtocol()) ? "wss" : "ws";
String endpoint = String.format(
"%s://%s:%s/ws/session/%s/appium/device/%s",
scheme, Strings.formatHost(host), port, sessionId, deviceLogType
);
return URI.create(endpoint);
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/appium/java_client/internal/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ private Strings() {
public static boolean isNullOrEmpty(@Nullable String string) {
return string == null || string.isEmpty();
}

/**
* Wraps a bare IPv6 host literal in brackets if not already bracketed.
*
* @param host the host name or IP literal
* @return the URL-safe host representation
*/
public static String formatHost(String host) {
if (!host.startsWith("[") && host.indexOf(':') != host.lastIndexOf(':')) {
return "[" + host + "]";
}
return host;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +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;

import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
Expand Down Expand Up @@ -65,13 +61,7 @@ default void startSyslogBroadcast(String host) {
* @param port the port of the host where Appium server is running
*/
default void startSyslogBroadcast(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/syslog", scheme, host, port, sessionId);
getSyslogClient().connect(URI.create(endpoint));
LogBroadcastEndpoints.startBroadcast(this, getSyslogClient(), host, port, "syslog");
}

/**
Expand Down
Loading
Loading