Skip to content

Commit 345676a

Browse files
Mykola MokhnachSrinivasanTarget
Mykola Mokhnach
authored andcommitted
Add wrappers for Android logcat broadcaster (#858)
* Add wrappers for Android logcat broadcaster * Address PR comments * Remove unused arguments * Tune execute args * Add missing dependencies * Add license headers * Rewrite the stuff using functional approach * Minimize the app to push log messages
1 parent 8cf79ea commit 345676a

11 files changed

+557
-3
lines changed

build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ dependencies {
7676
compile 'org.springframework:spring-context:5.0.3.RELEASE'
7777
compile 'org.aspectj:aspectjweaver:1.8.13'
7878
compile 'org.openpnp:opencv:3.2.0-1'
79-
79+
compile 'javax.websocket:javax.websocket-api:1.1'
80+
compile 'org.glassfish.tyrus:tyrus-client:1.1'
81+
compile 'org.glassfish.tyrus:tyrus-container-grizzly:1.1'
82+
8083
testCompile 'junit:junit:4.12'
8184
testCompile 'org.hamcrest:hamcrest-all:1.3'
8285
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class AndroidDriver<T extends WebElement>
5454
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
5555
HasSupportedPerformanceDataType, AuthenticatesByFinger,
5656
CanRecordScreen, SupportsSpecialEmulatorCommands,
57-
SupportsNetworkStateManagement, HasAndroidClipboard {
57+
SupportsNetworkStateManagement, ListensToLogcatMessages, HasAndroidClipboard {
5858

5959
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
6060

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.android;
18+
19+
import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
20+
import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;
21+
22+
import com.google.common.collect.ImmutableMap;
23+
24+
import io.appium.java_client.ExecutesMethod;
25+
import io.appium.java_client.ws.StringWebSocketClient;
26+
import org.openqa.selenium.remote.RemoteWebDriver;
27+
28+
import java.net.URI;
29+
import java.net.URISyntaxException;
30+
import java.util.Collections;
31+
import java.util.function.Consumer;
32+
33+
public interface ListensToLogcatMessages extends ExecutesMethod {
34+
StringWebSocketClient logcatClient = new StringWebSocketClient();
35+
36+
/**
37+
* Start logcat messages broadcast via web socket.
38+
* This method assumes that Appium server is running on localhost and
39+
* is assigned to the default port (4723).
40+
*/
41+
default void startLogcatBroadcast() {
42+
startLogcatBroadcast("localhost", DEFAULT_APPIUM_PORT);
43+
}
44+
45+
/**
46+
* Start logcat messages broadcast via web socket.
47+
* This method assumes that Appium server is assigned to the default port (4723).
48+
*
49+
* @param host the name of the host where Appium server is running
50+
*/
51+
default void startLogcatBroadcast(String host) {
52+
startLogcatBroadcast(host, DEFAULT_APPIUM_PORT);
53+
}
54+
55+
/**
56+
* Start logcat messages broadcast via web socket.
57+
*
58+
* @param host the name of the host where Appium server is running
59+
* @param port the port of the host where Appium server is running
60+
*/
61+
default void startLogcatBroadcast(String host, int port) {
62+
execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: startLogsBroadcast",
63+
"args", Collections.emptyList()));
64+
final URI endpointUri;
65+
try {
66+
endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/logcat",
67+
host, port, ((RemoteWebDriver) this).getSessionId()));
68+
} catch (URISyntaxException e) {
69+
throw new IllegalArgumentException(e);
70+
}
71+
logcatClient.connect(endpointUri);
72+
}
73+
74+
/**
75+
* Adds a new log messages broadcasting handler.
76+
* Several handlers might be assigned to a single server.
77+
* Multiple calls to this method will cause such handler
78+
* to be called multiple times.
79+
*
80+
* @param handler a function, which accepts a single argument, which is the actual log message
81+
*/
82+
default void addLogcatMessagesListener(Consumer<String> handler) {
83+
logcatClient.addMessageHandler(handler);
84+
}
85+
86+
/**
87+
* Adds a new log broadcasting errors handler.
88+
* Several handlers might be assigned to a single server.
89+
* Multiple calls to this method will cause such handler
90+
* to be called multiple times.
91+
*
92+
* @param handler a function, which accepts a single argument, which is the actual exception instance
93+
*/
94+
default void addLogcatErrorsListener(Consumer<Throwable> handler) {
95+
logcatClient.addErrorHandler(handler);
96+
}
97+
98+
/**
99+
* Adds a new log broadcasting connection handler.
100+
* Several handlers might be assigned to a single server.
101+
* Multiple calls to this method will cause such handler
102+
* to be called multiple times.
103+
*
104+
* @param handler a function, which is executed as soon as the client is successfully
105+
* connected to the web socket
106+
*/
107+
default void addLogcatConnectionListener(Runnable handler) {
108+
logcatClient.addConnectionHandler(handler);
109+
}
110+
111+
/**
112+
* Adds a new log broadcasting disconnection handler.
113+
* Several handlers might be assigned to a single server.
114+
* Multiple calls to this method will cause such handler
115+
* to be called multiple times.
116+
*
117+
* @param handler a function, which is executed as soon as the client is successfully
118+
* disconnected from the web socket
119+
*/
120+
default void addLogcatDisconnectionListener(Runnable handler) {
121+
logcatClient.addDisconnectionHandler(handler);
122+
}
123+
124+
/**
125+
* Removes all existing logcat handlers.
126+
*/
127+
default void removeAllLogcatListeners() {
128+
logcatClient.removeAllHandlers();
129+
}
130+
131+
/**
132+
* Stops logcat messages broadcast via web socket.
133+
*/
134+
default void stopLogcatBroadcast() {
135+
execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: stopLogsBroadcast",
136+
"args", Collections.emptyList()));
137+
}
138+
}

src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public final class AppiumServiceBuilder
7575
File.separator + BUILD_FOLDER
7676
+ File.separator + LIB_FOLDER
7777
+ File.separator + MAIN_JS;
78-
private static final int DEFAULT_APPIUM_PORT = 4723;
78+
public static final int DEFAULT_APPIUM_PORT = 4723;
7979
private static final String BASH = "bash";
8080
private static final String CMD_EXE = "cmd.exe";
8181
private static final String NODE = "node";
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.ws;
18+
19+
import java.util.List;
20+
21+
public interface CanHandleConnects {
22+
23+
/**
24+
* @return The list of web socket connection handlers.
25+
*/
26+
List<Runnable> getConnectionHandlers();
27+
28+
/**
29+
* Register a new message handler.
30+
*
31+
* @param handler a callback function, which is going to be executed when web socket connection event arrives
32+
*/
33+
default void addConnectionHandler(Runnable handler) {
34+
getConnectionHandlers().add(handler);
35+
}
36+
37+
/**
38+
* Removes existing web socket connection handlers.
39+
*/
40+
default void removeConnectionHandlers() {
41+
getConnectionHandlers().clear();
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.ws;
18+
19+
import java.util.List;
20+
21+
public interface CanHandleDisconnects {
22+
23+
/**
24+
* @return The list of web socket disconnection handlers.
25+
*/
26+
List<Runnable> getDisconnectionHandlers();
27+
28+
/**
29+
* Register a new web socket disconnect handler.
30+
*
31+
* @param handler a callback function, which is going to be executed when web socket disconnect event arrives
32+
*/
33+
default void addDisconnectionHandler(Runnable handler) {
34+
getDisconnectionHandlers().add(handler);
35+
}
36+
37+
/**
38+
* Removes existing disconnection handlers.
39+
*/
40+
default void removeDisconnectionHandlers() {
41+
getDisconnectionHandlers().clear();
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.ws;
18+
19+
import java.util.List;
20+
import java.util.function.Consumer;
21+
22+
public interface CanHandleErrors {
23+
24+
/**
25+
* @return The list of web socket error handlers.
26+
*/
27+
List<Consumer<Throwable>> getErrorHandlers();
28+
29+
/**
30+
* Register a new error handler.
31+
*
32+
* @param handler a callback function, which accepts the received exception instance as a parameter
33+
*/
34+
default void addErrorHandler(Consumer<Throwable> handler) {
35+
getErrorHandlers().add(handler);
36+
}
37+
38+
/**
39+
* Removes existing error handlers.
40+
*/
41+
default void removeErrorHandlers() {
42+
getErrorHandlers().clear();
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.ws;
18+
19+
import java.util.List;
20+
import java.util.function.Consumer;
21+
22+
public interface CanHandleMessages<T> {
23+
/**
24+
* @return The list of web socket message handlers.
25+
*/
26+
List<Consumer<T>> getMessageHandlers();
27+
28+
/**
29+
* Register a new message handler.
30+
*
31+
* @param handler a callback function, which accepts the received message as a parameter
32+
*/
33+
default void addMessageHandler(Consumer<T> handler) {
34+
getMessageHandlers().add(handler);
35+
}
36+
37+
/**
38+
* Removes existing message handlers.
39+
*/
40+
default void removeMessageHandlers() {
41+
getMessageHandlers().clear();
42+
}
43+
}
44+

0 commit comments

Comments
 (0)