How to use CloseMessage class of org.openqa.selenium.remote.http package

Best Selenium code snippet using org.openqa.selenium.remote.http.CloseMessage

Run Selenium automation tests on LambdaTest cloud grid

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

copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.remote.http.okhttp;
19
20import okhttp3.OkHttpClient;
21import okhttp3.Request;
22import okhttp3.Response;
23import okhttp3.WebSocketListener;
24import okio.ByteString;
25import org.openqa.selenium.remote.http.BinaryMessage;
26import org.openqa.selenium.remote.http.ClientConfig;
27import org.openqa.selenium.remote.http.CloseMessage;
28import org.openqa.selenium.remote.http.Filter;
29import org.openqa.selenium.remote.http.HttpRequest;
30import org.openqa.selenium.remote.http.HttpResponse;
31import org.openqa.selenium.remote.http.Message;
32import org.openqa.selenium.remote.http.TextMessage;
33import org.openqa.selenium.remote.http.WebSocket;
34
35import java.util.Objects;
36import java.util.concurrent.atomic.AtomicReference;
37import java.util.function.BiFunction;
38import java.util.function.Function;
39
40class OkHttpWebSocket implements WebSocket {
41
42  private final okhttp3.WebSocket socket;
43
44  private OkHttpWebSocket(okhttp3.OkHttpClient client, okhttp3.Request request, Listener listener) {
45    Objects.requireNonNull(client, "HTTP client to use must be set.");
46    Objects.requireNonNull(request, "Request to send must be set.");
47    Objects.requireNonNull(listener, "WebSocket listener must be set.");
48
49    socket = client.newWebSocket(request, new WebSocketListener() {
50      @Override
51      public void onMessage(okhttp3.WebSocket webSocket, String text) {
52        if (text != null) {
53          listener.onText(text);
54        }
55      }
56
57      @Override
58      public void onClosed(okhttp3.WebSocket webSocket, int code, String reason) {
59        listener.onClose(code, reason);
60      }
61
62      @Override
63      public void onFailure(okhttp3.WebSocket webSocket, Throwable t, Response response) {
64        listener.onError(t);
65      }
66    });
67  }
68
69  static BiFunction<HttpRequest, WebSocket.Listener, WebSocket> create(ClientConfig config) {
70    Filter filter = config.filter();
71
72    Function<HttpRequest, HttpRequest> filterRequest = req -> {
73      AtomicReference<HttpRequest> ref = new AtomicReference<>();
74      filter.andFinally(in -> {
75        ref.set(in);
76        return new HttpResponse();
77      }).execute(req);
78      return ref.get();
79    };
80
81    OkHttpClient client = new CreateOkClient().apply(config);
82    return (req, listener) -> {
83      HttpRequest filtered = filterRequest.apply(req);
84
85      Request okReq = OkMessages.toOkHttpRequest(config.baseUri(), filtered);
86
87      return new OkHttpWebSocket(client, okReq, listener);
88    };
89  }
90
91  @Override
92  public WebSocket send(Message message) {
93    if (message instanceof BinaryMessage) {
94      byte[] data = ((BinaryMessage) message).data();
95      socket.send(ByteString.of(data, 0, data.length));
96    } else if (message instanceof CloseMessage) {
97      socket.close(((CloseMessage) message).code(), ((CloseMessage) message).reason());
98    } else if (message instanceof TextMessage) {
99      socket.send(((TextMessage) message).text());
100    }
101
102    return this;
103  }
104
105  @Override
106  public void close() {
107    socket.close(1000, "WebDriver closing socket");
108  }
109}
110
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

Most used methods in CloseMessage

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)