Skip to content
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
2 changes: 1 addition & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class IOSDriver<T extends WebElement>
extends AppiumDriver<T>
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
FindsByIosUIAutomation<T>, LocksDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
FindsByIosClassChain<T>, PushesFiles, CanRecordScreen, HasIOSClipboard {
FindsByIosClassChain<T>, PushesFiles, CanRecordScreen, HasIOSClipboard, ListensToSyslogMessages {

private static final String IOS_PLATFORM = MobilePlatform.IOS;

Expand Down
138 changes: 138 additions & 0 deletions src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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.ios;

import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;

import com.google.common.collect.ImmutableMap;

import io.appium.java_client.ExecutesMethod;
import io.appium.java_client.ws.StringWebSocketClient;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.function.Consumer;

public interface ListensToSyslogMessages extends ExecutesMethod {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mykola-mokhnach Its same as Logcat Listerners which you have added some time back. Any reason why both are separate interfaces apart from naming convention of Logcat for Android and Syslog for iOS? Do you forcast anything in future where we will have it in android and not in iOS or viceviz.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I think something may change there, so we keep it separate for both platforms. Would you prefer to combine the interfaces?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its fine

StringWebSocketClient syslogClient = new StringWebSocketClient();

/**
* Start syslog messages broadcast via web socket.
* This method assumes that Appium server is running on localhost and
* is assigned to the default port (4723).
*/
default void startSyslogBroadcast() {
startSyslogBroadcast("localhost", DEFAULT_APPIUM_PORT);
}

/**
* Start syslog messages broadcast via web socket.
* This method assumes that Appium server is assigned to the default port (4723).
*
* @param host the name of the host where Appium server is running
*/
default void startSyslogBroadcast(String host) {
startSyslogBroadcast(host, DEFAULT_APPIUM_PORT);
}

/**
* Start syslog messages broadcast via web socket.
*
* @param host the name of the host where Appium server is running
* @param port the port of the host where Appium server is running
*/
default void startSyslogBroadcast(String host, int port) {
execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: startLogsBroadcast",
"args", Collections.emptyList()));
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);
}
syslogClient.connect(endpointUri);
}

/**
* Adds a new log messages broadcasting handler.
* Several handlers might be assigned to a single server.
* Multiple calls to this method will cause such handler
* to be called multiple times.
*
* @param handler a function, which accepts a single argument, which is the actual log message
*/
default void addSyslogMessagesListener(Consumer<String> handler) {
syslogClient.addMessageHandler(handler);
}

/**
* Adds a new log broadcasting errors handler.
* Several handlers might be assigned to a single server.
* Multiple calls to this method will cause such handler
* to be called multiple times.
*
* @param handler a function, which accepts a single argument, which is the actual exception instance
*/
default void addSyslogErrorsListener(Consumer<Throwable> handler) {
syslogClient.addErrorHandler(handler);
}

/**
* Adds a new log broadcasting connection handler.
* Several handlers might be assigned to a single server.
* Multiple calls to this method will cause such handler
* to be called multiple times.
*
* @param handler a function, which is executed as soon as the client is successfully
* connected to the web socket
*/
default void addSyslogConnectionListener(Runnable handler) {
syslogClient.addConnectionHandler(handler);
}

/**
* Adds a new log broadcasting disconnection handler.
* Several handlers might be assigned to a single server.
* Multiple calls to this method will cause such handler
* to be called multiple times.
*
* @param handler a function, which is executed as soon as the client is successfully
* disconnected from the web socket
*/
default void addSyslogDisconnectionListener(Runnable handler) {
syslogClient.addDisconnectionHandler(handler);
}

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

/**
* Stops syslog messages broadcast via web socket.
*/
default void stopSyslogBroadcast() {
execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: stopLogsBroadcast",
"args", Collections.emptyList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.appium.java_client.ios;

import static org.junit.Assert.assertTrue;

import org.apache.commons.lang3.time.DurationFormatUtils;
import org.junit.Test;

import java.time.Duration;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class IOSSyslogListenerTest extends UICatalogIOSTest {

@Test
public void verifySyslogListenerCanBeAssigned() {
final Semaphore messageSemaphore = new Semaphore(1);
final Duration timeout = Duration.ofSeconds(15);

driver.addSyslogMessagesListener((msg) -> messageSemaphore.release());
driver.addSyslogConnectionListener(() -> System.out.println("Connected to the web socket"));
driver.addSyslogDisconnectionListener(() -> System.out.println("Disconnected from the web socket"));
driver.addSyslogErrorsListener(Throwable::printStackTrace);
try {
driver.startSyslogBroadcast();
messageSemaphore.acquire();
// This is needed for pushing some internal log messages
driver.runAppInBackground(Duration.ofSeconds(1));
assertTrue(String.format("Didn't receive any log message after %s timeout",
DurationFormatUtils.formatDuration(timeout.toMillis(), "H:mm:ss", true)),
messageSemaphore.tryAcquire(timeout.toMillis(), TimeUnit.MILLISECONDS));
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} finally {
messageSemaphore.release();
driver.stopSyslogBroadcast();
}
}
}