How to use onOpen method of io.appium.java_client.ws.StringWebSocketClient class

Best io.appium code snippet using io.appium.java_client.ws.StringWebSocketClient.onOpen

Run io.appium automation tests on LambdaTest cloud grid

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

StringWebSocketClient.java

Source: StringWebSocketClient.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.ws;
18
19import java.net.URI;
20import java.time.Duration;
21import java.util.List;
22import java.util.concurrent.CopyOnWriteArrayList;
23import java.util.function.Consumer;
24
25import javax.annotation.Nullable;
26
27import org.openqa.selenium.remote.http.ClientConfig;
28import org.openqa.selenium.remote.http.HttpClient;
29import org.openqa.selenium.remote.http.HttpMethod;
30import org.openqa.selenium.remote.http.HttpRequest;
31import org.openqa.selenium.remote.http.WebSocket;
32
33public class StringWebSocketClient implements WebSocket.Listener,
34        CanHandleMessages<String>, CanHandleErrors, CanHandleConnects, CanHandleDisconnects {
35    private final List<Consumer<String>> messageHandlers = new CopyOnWriteArrayList<>();
36    private final List<Consumer<Throwable>> errorHandlers = new CopyOnWriteArrayList<>();
37    private final List<Runnable> connectHandlers = new CopyOnWriteArrayList<>();
38    private final List<Runnable> disconnectHandlers = new CopyOnWriteArrayList<>();
39
40    private volatile boolean isListening = false;
41
42    private URI endpoint;
43
44    private void setEndpoint(URI endpoint) {
45        this.endpoint = endpoint;
46    }
47
48    @Nullable
49    public URI getEndpoint() {
50        return this.endpoint;
51    }
52
53    public boolean isListening() {
54        return isListening;
55    }
56
57    /**
58     * Connects web socket client.
59     *
60     * @param endpoint The full address of an endpoint to connect to.
61     *                 Usually starts with 'ws://'.
62     */
63    public void connect(URI endpoint) {
64        if (endpoint.equals(this.getEndpoint()) && isListening) {
65            return;
66        }
67
68        ClientConfig clientConfig = ClientConfig.defaultConfig()
69                .readTimeout(Duration.ZERO)
70                .baseUri(endpoint); // To avoid NPE in org.openqa.selenium.remote.http.netty.NettyMessages (line 78)
71        HttpClient client = HttpClient.Factory.createDefault().createClient(clientConfig);
72        HttpRequest request = new HttpRequest(HttpMethod.GET, endpoint.toString());
73        client.openSocket(request, this);
74        onOpen();
75
76        setEndpoint(endpoint);
77    }
78
79    public void onOpen() {
80        getConnectionHandlers().forEach(Runnable::run);
81        isListening = true;
82    }
83
84    @Override
85    public void onClose(int code, String reason) {
86        getDisconnectionHandlers().forEach(Runnable::run);
87        isListening = false;
88    }
89
90    @Override
91    public void onError(Throwable t) {
92        getErrorHandlers().forEach(x -> x.accept(t));
93    }
94
95    @Override
96    public void onText(CharSequence data) {
97        String text = data.toString();
98        getMessageHandlers().forEach(x -> x.accept(text));
99    }
100
101    @Override
102    public List<Consumer<String>> getMessageHandlers() {
103        return messageHandlers;
104    }
105
106    @Override
107    public List<Consumer<Throwable>> getErrorHandlers() {
108        return errorHandlers;
109    }
110
111    @Override
112    public List<Runnable> getConnectionHandlers() {
113        return connectHandlers;
114    }
115
116    @Override
117    public List<Runnable> getDisconnectionHandlers() {
118        return disconnectHandlers;
119    }
120
121    /**
122     * Remove all the registered handlers.
123     */
124    public void removeAllHandlers() {
125        removeMessageHandlers();
126        removeErrorHandlers();
127        removeConnectionHandlers();
128        removeDisconnectionHandlers();
129    }
130}
131
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 onOpen code on LambdaTest Cloud Grid

Execute automation tests with onOpen 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)