How to use getLogcatClient method of io.appium.java_client.android.ListensToLogcatMessages class

Best io.appium code snippet using io.appium.java_client.android.ListensToLogcatMessages.getLogcatClient

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

ListensToLogcatMessages.java

Source: ListensToLogcatMessages.java Github

copy
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
17package io.appium.java_client.android;
18
19import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
20import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT;
21
22import com.google.common.collect.ImmutableMap;
23
24import io.appium.java_client.ExecutesMethod;
25import io.appium.java_client.ws.StringWebSocketClient;
26import org.openqa.selenium.remote.RemoteWebDriver;
27
28import java.net.URI;
29import java.net.URISyntaxException;
30import java.util.Collections;
31import java.util.function.Consumer;
32
33public interface ListensToLogcatMessages extends ExecutesMethod {
34    StringWebSocketClient getLogcatClient();
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        getLogcatClient().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        getLogcatClient().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        getLogcatClient().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        getLogcatClient().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        getLogcatClient().addDisconnectionHandler(handler);
122    }
123
124    /**
125     * Removes all existing logcat handlers.
126     */
127    default void removeAllLogcatListeners() {
128        getLogcatClient().removeAllHandlers();
129    }
130
131    /**
132     * Stops logcat messages broadcast via web socket.
133     */
134    default void stopLogcatBroadcast() {
135        removeAllLogcatListeners();
136        execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: stopLogsBroadcast",
137                "args", Collections.emptyList()));
138    }
139}
140
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger getLogcatClient code on LambdaTest Cloud Grid

Execute automation tests with getLogcatClient on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)