Skip to content

Commit 8a63759

Browse files
authored
feat: Add ability to use secure WebSocket to listen Logcat messages (#2182)
1 parent b2c7d87 commit 8a63759

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import io.appium.java_client.CommandExecutionHelper;
2020
import io.appium.java_client.ExecutesMethod;
2121
import io.appium.java_client.ws.StringWebSocketClient;
22+
import org.openqa.selenium.remote.HttpCommandExecutor;
2223
import org.openqa.selenium.remote.RemoteWebDriver;
24+
import org.openqa.selenium.remote.SessionId;
2325

2426
import java.net.URI;
25-
import java.net.URISyntaxException;
27+
import java.net.URL;
2628
import java.util.function.Consumer;
2729

2830
import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
@@ -36,7 +38,7 @@ public interface ListensToLogcatMessages extends ExecutesMethod {
3638
* is assigned to the default port (4723).
3739
*/
3840
default void startLogcatBroadcast() {
39-
startLogcatBroadcast("127.0.0.1", DEFAULT_APPIUM_PORT);
41+
startLogcatBroadcast("127.0.0.1");
4042
}
4143

4244
/**
@@ -56,15 +58,13 @@ default void startLogcatBroadcast(String host) {
5658
* @param port the port of the host where Appium server is running
5759
*/
5860
default void startLogcatBroadcast(String host, int port) {
61+
var remoteWebDriver = (RemoteWebDriver) this;
62+
URL serverUrl = ((HttpCommandExecutor) remoteWebDriver.getCommandExecutor()).getAddressOfRemoteServer();
63+
var scheme = "https".equals(serverUrl.getProtocol()) ? "wss" : "ws";
5964
CommandExecutionHelper.executeScript(this, "mobile: startLogsBroadcast");
60-
final URI endpointUri;
61-
try {
62-
endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/logcat",
63-
host, port, ((RemoteWebDriver) this).getSessionId()));
64-
} catch (URISyntaxException e) {
65-
throw new IllegalArgumentException(e);
66-
}
67-
getLogcatClient().connect(endpointUri);
65+
SessionId sessionId = remoteWebDriver.getSessionId();
66+
var endpoint = String.format("%s://%s:%s/ws/session/%s/appium/device/logcat", scheme, host, port, sessionId);
67+
getLogcatClient().connect(URI.create(endpoint));
6868
}
6969

7070
/**

src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import io.appium.java_client.CommandExecutionHelper;
2020
import io.appium.java_client.ExecutesMethod;
2121
import io.appium.java_client.ws.StringWebSocketClient;
22+
import org.openqa.selenium.remote.HttpCommandExecutor;
2223
import org.openqa.selenium.remote.RemoteWebDriver;
24+
import org.openqa.selenium.remote.SessionId;
2325

2426
import java.net.URI;
25-
import java.net.URISyntaxException;
27+
import java.net.URL;
2628
import java.util.function.Consumer;
2729

2830
import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
@@ -37,7 +39,7 @@ public interface ListensToSyslogMessages extends ExecutesMethod {
3739
* is assigned to the default port (4723).
3840
*/
3941
default void startSyslogBroadcast() {
40-
startSyslogBroadcast("localhost", DEFAULT_APPIUM_PORT);
42+
startSyslogBroadcast("localhost");
4143
}
4244

4345
/**
@@ -57,15 +59,13 @@ default void startSyslogBroadcast(String host) {
5759
* @param port the port of the host where Appium server is running
5860
*/
5961
default void startSyslogBroadcast(String host, int port) {
62+
var remoteWebDriver = (RemoteWebDriver) this;
63+
URL serverUrl = ((HttpCommandExecutor) remoteWebDriver.getCommandExecutor()).getAddressOfRemoteServer();
64+
var scheme = "https".equals(serverUrl.getProtocol()) ? "wss" : "ws";
6065
CommandExecutionHelper.executeScript(this, "mobile: startLogsBroadcast");
61-
final URI endpointUri;
62-
try {
63-
endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/syslog",
64-
host, port, ((RemoteWebDriver) this).getSessionId()));
65-
} catch (URISyntaxException e) {
66-
throw new IllegalArgumentException(e);
67-
}
68-
getSyslogClient().connect(endpointUri);
66+
SessionId sessionId = remoteWebDriver.getSessionId();
67+
var endpoint = String.format("%s://%s:%s/ws/session/%s/appium/device/syslog", scheme, host, port, sessionId);
68+
getSyslogClient().connect(URI.create(endpoint));
6969
}
7070

7171
/**

0 commit comments

Comments
 (0)