Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init web socket clients lazily #912

Merged
merged 1 commit into from
May 25, 2018
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
11 changes: 11 additions & 0 deletions src/main/java/io/appium/java_client/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.ws.StringWebSocketClient;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.HttpCommandExecutor;
Expand Down Expand Up @@ -66,6 +67,8 @@ public class AndroidDriver<T extends WebElement>

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

private StringWebSocketClient logcatClient;

/**
* Creates a new instance based on command {@code executor} and {@code capabilities}.
*
Expand Down Expand Up @@ -193,4 +196,12 @@ public AndroidBatteryInfo getBatteryInfo() {
return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
"script", "mobile: batteryInfo", "args", Collections.emptyList())));
}

@Override
public synchronized StringWebSocketClient getLogcatClient() {
if (logcatClient == null) {
logcatClient = new StringWebSocketClient();
}
return logcatClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.function.Consumer;

public interface ListensToLogcatMessages extends ExecutesMethod {
StringWebSocketClient logcatClient = new StringWebSocketClient();
StringWebSocketClient getLogcatClient();

/**
* Start logcat messages broadcast via web socket.
Expand Down Expand Up @@ -68,7 +68,7 @@ default void startLogcatBroadcast(String host, int port) {
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
logcatClient.connect(endpointUri);
getLogcatClient().connect(endpointUri);
}

/**
Expand All @@ -80,7 +80,7 @@ default void startLogcatBroadcast(String host, int port) {
* @param handler a function, which accepts a single argument, which is the actual log message
*/
default void addLogcatMessagesListener(Consumer<String> handler) {
logcatClient.addMessageHandler(handler);
getLogcatClient().addMessageHandler(handler);
}

/**
Expand All @@ -92,7 +92,7 @@ default void addLogcatMessagesListener(Consumer<String> handler) {
* @param handler a function, which accepts a single argument, which is the actual exception instance
*/
default void addLogcatErrorsListener(Consumer<Throwable> handler) {
logcatClient.addErrorHandler(handler);
getLogcatClient().addErrorHandler(handler);
}

/**
Expand All @@ -105,7 +105,7 @@ default void addLogcatErrorsListener(Consumer<Throwable> handler) {
* connected to the web socket
*/
default void addLogcatConnectionListener(Runnable handler) {
logcatClient.addConnectionHandler(handler);
getLogcatClient().addConnectionHandler(handler);
}

/**
Expand All @@ -118,14 +118,14 @@ default void addLogcatConnectionListener(Runnable handler) {
* disconnected from the web socket
*/
default void addLogcatDisconnectionListener(Runnable handler) {
logcatClient.addDisconnectionHandler(handler);
getLogcatClient().addDisconnectionHandler(handler);
}

/**
* Removes all existing logcat handlers.
*/
default void removeAllLogcatListeners() {
logcatClient.removeAllHandlers();
getLogcatClient().removeAllHandlers();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.ws.StringWebSocketClient;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class IOSDriver<T extends WebElement>

private static final String IOS_PLATFORM = MobilePlatform.IOS;

private StringWebSocketClient syslogClient;

/**
* Creates a new instance based on command {@code executor} and {@code capabilities}.
*
Expand Down Expand Up @@ -225,4 +228,12 @@ class IOSAlert implements Alert {
}

}

@Override
public synchronized StringWebSocketClient getSyslogClient() {
if (syslogClient == null) {
syslogClient = new StringWebSocketClient();
}
return syslogClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import java.util.function.Consumer;

public interface ListensToSyslogMessages extends ExecutesMethod {
StringWebSocketClient syslogClient = new StringWebSocketClient();

StringWebSocketClient getSyslogClient();

/**
* Start syslog messages broadcast via web socket.
Expand Down Expand Up @@ -68,7 +69,7 @@ default void startSyslogBroadcast(String host, int port) {
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
syslogClient.connect(endpointUri);
getSyslogClient().connect(endpointUri);
}

/**
Expand All @@ -80,7 +81,7 @@ default void startSyslogBroadcast(String host, int port) {
* @param handler a function, which accepts a single argument, which is the actual log message
*/
default void addSyslogMessagesListener(Consumer<String> handler) {
syslogClient.addMessageHandler(handler);
getSyslogClient().addMessageHandler(handler);
}

/**
Expand All @@ -92,7 +93,7 @@ default void addSyslogMessagesListener(Consumer<String> handler) {
* @param handler a function, which accepts a single argument, which is the actual exception instance
*/
default void addSyslogErrorsListener(Consumer<Throwable> handler) {
syslogClient.addErrorHandler(handler);
getSyslogClient().addErrorHandler(handler);
}

/**
Expand All @@ -105,7 +106,7 @@ default void addSyslogErrorsListener(Consumer<Throwable> handler) {
* connected to the web socket
*/
default void addSyslogConnectionListener(Runnable handler) {
syslogClient.addConnectionHandler(handler);
getSyslogClient().addConnectionHandler(handler);
}

/**
Expand All @@ -118,14 +119,14 @@ default void addSyslogConnectionListener(Runnable handler) {
* disconnected from the web socket
*/
default void addSyslogDisconnectionListener(Runnable handler) {
syslogClient.addDisconnectionHandler(handler);
getSyslogClient().addDisconnectionHandler(handler);
}

/**
* Removes all existing syslog handlers.
*/
default void removeAllSyslogListeners() {
syslogClient.removeAllHandlers();
getSyslogClient().removeAllHandlers();
}

/**
Expand Down