From 5d4062a6cd35bb3ee787a327c75a7fab6fd51b57 Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Tue, 9 Jul 2024 22:31:50 +0300 Subject: [PATCH] feat: Add ability to use secure WebSocket to listen Logcat messages --- .../android/ListensToLogcatMessages.java | 20 +++++++++---------- .../ios/ListensToSyslogMessages.java | 18 ++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java b/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java index 8d16bf7de..7112c5fb1 100644 --- a/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java +++ b/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java @@ -19,10 +19,12 @@ import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; import io.appium.java_client.ws.StringWebSocketClient; +import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.remote.SessionId; import java.net.URI; -import java.net.URISyntaxException; +import java.net.URL; import java.util.function.Consumer; import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT; @@ -36,7 +38,7 @@ public interface ListensToLogcatMessages extends ExecutesMethod { * is assigned to the default port (4723). */ default void startLogcatBroadcast() { - startLogcatBroadcast("127.0.0.1", DEFAULT_APPIUM_PORT); + startLogcatBroadcast("127.0.0.1"); } /** @@ -56,15 +58,13 @@ default void startLogcatBroadcast(String host) { * @param port the port of the host where Appium server is running */ default void startLogcatBroadcast(String host, int port) { + RemoteWebDriver remoteWebDriver = (RemoteWebDriver) this; + URL serverUrl = ((HttpCommandExecutor) remoteWebDriver.getCommandExecutor()).getAddressOfRemoteServer(); + String scheme = "https".equals(serverUrl.getProtocol()) ? "wss" : "ws"; CommandExecutionHelper.executeScript(this, "mobile: startLogsBroadcast"); - final URI endpointUri; - try { - endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/logcat", - host, port, ((RemoteWebDriver) this).getSessionId())); - } catch (URISyntaxException e) { - throw new IllegalArgumentException(e); - } - getLogcatClient().connect(endpointUri); + SessionId sessionId = remoteWebDriver.getSessionId(); + String endpoint = String.format("%s://%s:%s/ws/session/%s/appium/device/logcat", scheme, host, port, sessionId); + getLogcatClient().connect(URI.create(endpoint)); } /** diff --git a/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java b/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java index ef0bd3f9d..9b9265eaa 100644 --- a/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java +++ b/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java @@ -19,10 +19,12 @@ import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; import io.appium.java_client.ws.StringWebSocketClient; +import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.remote.SessionId; import java.net.URI; -import java.net.URISyntaxException; +import java.net.URL; import java.util.function.Consumer; import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT; @@ -57,15 +59,13 @@ default void startSyslogBroadcast(String host) { * @param port the port of the host where Appium server is running */ default void startSyslogBroadcast(String host, int port) { + RemoteWebDriver remoteWebDriver = (RemoteWebDriver) this; + URL serverUrl = ((HttpCommandExecutor) remoteWebDriver.getCommandExecutor()).getAddressOfRemoteServer(); + String scheme = "https".equals(serverUrl.getProtocol()) ? "wss" : "ws"; CommandExecutionHelper.executeScript(this, "mobile: startLogsBroadcast"); - final URI endpointUri; - try { - endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/syslog", - host, port, ((RemoteWebDriver) this).getSessionId())); - } catch (URISyntaxException e) { - throw new IllegalArgumentException(e); - } - getSyslogClient().connect(endpointUri); + SessionId sessionId = remoteWebDriver.getSessionId(); + String endpoint = String.format("%s://%s:%s/ws/session/%s/appium/device/syslog", scheme, host, port, sessionId); + getSyslogClient().connect(URI.create(endpoint)); } /**