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

Best Selenium code snippet using org.openqa.selenium.remote.http.Interface Message

Source:SelfRegisteringRemote.java Github

copy

Full Screen

1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.grid.internal.utils;18import static org.openqa.selenium.json.Json.MAP_TYPE;19import static org.openqa.selenium.remote.http.Contents.reader;20import static org.openqa.selenium.remote.http.Contents.string;21import static org.openqa.selenium.remote.http.Contents.utf8String;22import static org.openqa.selenium.remote.http.HttpMethod.GET;23import static org.openqa.selenium.remote.http.HttpMethod.POST;24import org.openqa.grid.common.RegistrationRequest;25import org.openqa.grid.common.exception.GridConfigurationException;26import org.openqa.grid.common.exception.GridException;27import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;28import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;29import org.openqa.grid.shared.GridNodeServer;30import org.openqa.grid.web.servlet.NodeW3CStatusServlet;31import org.openqa.grid.web.servlet.ResourceServlet;32import org.openqa.grid.web.utils.ExtraServletUtil;33import org.openqa.selenium.Platform;34import org.openqa.selenium.json.Json;35import org.openqa.selenium.json.JsonInput;36import org.openqa.selenium.remote.DesiredCapabilities;37import org.openqa.selenium.remote.http.HttpClient;38import org.openqa.selenium.remote.http.HttpRequest;39import org.openqa.selenium.remote.http.HttpResponse;40import org.openqa.selenium.remote.server.log.LoggingManager;41import java.io.Reader;42import java.net.MalformedURLException;43import java.net.URL;44import java.security.InvalidParameterException;45import java.util.HashMap;46import java.util.List;47import java.util.Map;48import java.util.logging.Logger;49import javax.servlet.Servlet;50public class SelfRegisteringRemote {51 private static final Logger LOG = Logger.getLogger(SelfRegisteringRemote.class.getName());52 private final RegistrationRequest registrationRequest;53 private final HttpClient.Factory httpClientFactory;54 private final Map<String, Class<? extends Servlet>> nodeServlets;55 private boolean hasId;56 private boolean timeoutFetchedFromHub;57 private boolean browserTimeoutFetchedFromHub;58 public SelfRegisteringRemote(GridNodeConfiguration configuration) {59 this(RegistrationRequest.build(configuration, null, null));60 }61 public SelfRegisteringRemote(RegistrationRequest request) {62 this.registrationRequest = request;63 this.httpClientFactory = HttpClient.Factory.createDefault();64 this.nodeServlets = new HashMap<>();65 registrationRequest.validate();66 // add the status servlet67 nodeServlets.put("/status", NodeW3CStatusServlet.class);68 nodeServlets.put("/wd/hub/status", NodeW3CStatusServlet.class);69 // add the resource servlet for nodes70 if (!registrationRequest.getConfiguration().isWithOutServlet(ResourceServlet.class)) {71 nodeServlets.put("/resources/*", ResourceServlet.class);72 }73 // add the user supplied servlet(s) for nodes74 addExtraServlets(registrationRequest.getConfiguration().servlets);75 }76 public URL getRemoteURL() {77 String host = registrationRequest.getConfiguration().host;78 Integer port = registrationRequest.getConfiguration().port;79 String url = "http://" + host + ":" + port;80 try {81 return new URL(url);82 } catch (MalformedURLException e) {83 throw new GridConfigurationException("error building the node url " + e.getMessage(), e);84 }85 }86 private GridNodeServer server;87 public void setRemoteServer(GridNodeServer server) {88 this.server = server;89 }90 public boolean startRemoteServer() {91 if (server == null) {92 throw new GridConfigurationException("no server set to register to the hub");93 }94 server.setExtraServlets(nodeServlets);95 return server.boot();96 }97 public void stopRemoteServer() {98 if (server != null) {99 server.stop();100 }101 }102 public void deleteAllBrowsers() {103 registrationRequest.getConfiguration().capabilities.clear();104 }105 /**106 * Adding the browser described by the capability, automatically finding out what platform the107 * node is launched from108 *109 * @param cap describing the browser110 * @param instances number of times this browser can be started on the node.111 */112 public void addBrowser(DesiredCapabilities cap, int instances) {113 String s = cap.getBrowserName();114 if (s == null || "".equals(s)) {115 throw new InvalidParameterException(cap + " does seems to be a valid browser.");116 }117 if (cap.getPlatform() == null) {118 cap.setPlatform(Platform.getCurrent());119 }120 cap.setCapability(RegistrationRequest.MAX_INSTANCES, instances);121 registrationRequest.getConfiguration().capabilities.add(cap);122 registrationRequest.getConfiguration().fixUpCapabilities();123 }124 /**125 * sends 1 registration request, bypassing the retry logic and the proxy already registered check.126 * Use only for testing.127 */128 public void sendRegistrationRequest() {129 registerToHub(false);130 }131 /**132 * register the hub following the configuration :133 * <p>134 * - check if the proxy is already registered before sending a reg request.135 * <p>136 * - register again every X ms is specified in the config of the node.137 */138 public void startRegistrationProcess() {139 // don't advertise that the remote (node) is bound to all IPv4 interfaces (default behavior)140 if (registrationRequest.getConfiguration().host.equals("0.0.0.0")) {141 // remove the value and call fixUpHost to determine the address of a public (non-loopback) IPv4 interface142 registrationRequest.getConfiguration().host = null;143 registrationRequest.getConfiguration().fixUpHost();144 }145 fixUpId();146 LOG.fine("Using the json request : " + new Json().toJson(registrationRequest));147 Boolean register = registrationRequest.getConfiguration().register;148 if (register == null) {149 register = false;150 }151 if (!register) {152 LOG.info("No registration sent ( register = false )");153 } else {154 final int155 registerCycleInterval =156 registrationRequest.getConfiguration().registerCycle != null ?157 registrationRequest.getConfiguration().registerCycle : 0;158 if (registerCycleInterval > 0) {159 new Thread(new Runnable() { // Thread safety reviewed160 @Override161 public void run() {162 boolean first = true;163 LOG.info("Starting auto registration thread. Will try to register every "164 + registerCycleInterval + " ms.");165 while (true) {166 try {167 boolean checkForPresence = true;168 if (first) {169 first = false;170 checkForPresence = false;171 }172 registerToHub(checkForPresence);173 } catch (GridException e) {174 LOG.info("Couldn't register this node: " + e.getMessage());175 }176 try {177 Thread.sleep(registerCycleInterval);178 } catch (InterruptedException e) {179 e.printStackTrace();180 }181 // While we wait for someone to rewrite server logging.182 LoggingManager.perSessionLogHandler().clearThreadTempLogs();183 }184 }185 }).start();186 } else {187 registerToHub(false);188 }189 }190 LoggingManager.perSessionLogHandler().clearThreadTempLogs();191 }192 public void setTimeout(int timeout, int cycle) {193 registrationRequest.getConfiguration().timeout = timeout;194 registrationRequest.getConfiguration().cleanUpCycle = cycle;195 }196 public void setMaxConcurrent(int max) {197 registrationRequest.getConfiguration().maxSession = max;198 }199 public GridNodeConfiguration getConfiguration() {200 return registrationRequest.getConfiguration();201 }202 /**203 * @return the {@link GridNodeServer} for this remote204 */205 protected GridNodeServer getServer() {206 return server;207 }208 /**209 * @return the list of {@link Servlet}s that this remote will bind210 */211 protected Map<String, Class<? extends Servlet>> getNodeServlets() {212 return nodeServlets;213 }214 private void registerToHub(boolean checkPresenceFirst) {215 if (!checkPresenceFirst || !isAlreadyRegistered(registrationRequest)) {216 String tmp =217 "http://" + registrationRequest.getConfiguration().getHubHost() + ":"218 + registrationRequest.getConfiguration().getHubPort() + "/grid/register";219 // browserTimeout and timeout are always fetched from the hub. Nodes don't have default values.220 // If a node has browserTimeout or timeout configured, those will have precedence over the hub.221 LOG.fine(222 "Fetching browserTimeout and timeout values from the hub before sending registration request");223 try {224 GridHubConfiguration hubConfiguration = getHubConfiguration();225 LOG.fine("Hub configuration: " + new Json().toJson(hubConfiguration));226 if (hubConfiguration.timeout == null || hubConfiguration.browserTimeout == null) {227 throw new GridException("Hub browserTimeout or timeout (or both) are null");228 }229 if (registrationRequest.getConfiguration().timeout == null) {230 registrationRequest.getConfiguration().timeout = hubConfiguration.timeout;231 timeoutFetchedFromHub = true;232 }233 if (registrationRequest.getConfiguration().browserTimeout == null) {234 registrationRequest.getConfiguration().browserTimeout = hubConfiguration.browserTimeout;235 browserTimeoutFetchedFromHub = true;236 }237 // The hub restarts and changes its configuration, the node fetches and updates its own again.238 // Only if it was previously fetched from the hub.239 if (timeoutFetchedFromHub) {240 registrationRequest.getConfiguration().timeout = hubConfiguration.timeout;241 }242 if (browserTimeoutFetchedFromHub) {243 registrationRequest.getConfiguration().browserTimeout = hubConfiguration.browserTimeout;244 }245 LOG.fine("Updated node configuration: " + new Json()246 .toJson(registrationRequest.getConfiguration()));247 } catch (Exception e) {248 LOG.warning(249 "Error getting the parameters from the hub. The node may end up with wrong timeouts." +250 e.getMessage());251 }252 try {253 URL registration = new URL(tmp);254 LOG.info("Registering the node to the hub: " + registration);255 HttpRequest request = new HttpRequest(POST, registration.toExternalForm());256 updateConfigWithRealPort();257 String json = new Json().toJson(registrationRequest);258 request.setContent(utf8String(json));259 HttpClient client = httpClientFactory.createClient(registration);260 HttpResponse response = client.execute(request);261 if (response.getStatus() != 200) {262 throw new GridException(String.format("The hub responded with %s", response.getStatus()));263 }264 LOG.info("The node is registered to the hub and ready to use");265 } catch (Exception e) {266 throw new GridException("Error sending the registration request: " + e.getMessage());267 }268 }269 }270 private void addExtraServlets(List<String> servlets) {271 if (servlets == null || servlets.size() == 0) {272 return;273 }274 for (String s : servlets) {275 Class<? extends Servlet> servletClass = ExtraServletUtil.createServlet(s);276 if (servletClass != null) {277 String path = "/extra/" + servletClass.getSimpleName() + "/*";278 LOG.info("binding " + servletClass.getCanonicalName() + " to " + path);279 nodeServlets.put(path, servletClass);280 }281 }282 }283 private void fixUpId() {284 if (hasId) {285 return;286 }287 // make sure 'id' has a value.288 if (registrationRequest.getConfiguration().id == null || registrationRequest289 .getConfiguration().id.isEmpty()) {290 registrationRequest.getConfiguration().id =291 registrationRequest.getConfiguration().getRemoteHost();292 }293 hasId = true;294 }295 void updateConfigWithRealPort() {296 if (registrationRequest.getConfiguration().port != 0) {297 return;298 }299 registrationRequest.getConfiguration().port = server.getRealPort();300 }301 /**302 * uses the hub API to get some of its configuration.303 *304 * @return json object of the current hub configuration305 */306 private GridHubConfiguration getHubConfiguration() throws Exception {307 String hubApi =308 "http://" + registrationRequest.getConfiguration().getHubHost() + ":"309 + registrationRequest.getConfiguration().getHubPort() + "/grid/api/hub";310 URL api = new URL(hubApi);311 HttpClient client = httpClientFactory.createClient(api);312 String url = api.toExternalForm();313 HttpRequest request = new HttpRequest(GET, url);314 HttpResponse response = client.execute(request);315 try (Reader reader = reader(response);316 JsonInput jsonInput = new Json().newInput(reader)) {317 return GridHubConfiguration.loadFromJSON(jsonInput);318 }319 }320 private boolean isAlreadyRegistered(RegistrationRequest node) {321 try {322 String tmp =323 "http://" + node.getConfiguration().getHubHost() + ":"324 + node.getConfiguration().getHubPort() + "/grid/api/proxy";325 URL api = new URL(tmp);326 HttpClient client = httpClientFactory.createClient(api);327 String id = node.getConfiguration().id;328 if (id == null) {329 id = node.getConfiguration().getRemoteHost();330 }331 HttpRequest request = new HttpRequest(GET, api.toExternalForm())332 .addQueryParameter("id", id);333 HttpResponse response = client.execute(request);334 if (response.getStatus() != 200) {335 throw new GridException(String.format("The hub responded with %s", response.getStatus()));336 }337 Map<String, Object> o = extractObject(response);338 return (Boolean) o.get("success");339 } catch (Exception e) {340 throw new GridException("The hub is down or not responding: " + e.getMessage());341 }342 }343 private static Map<String, Object> extractObject(HttpResponse resp) {344 return new Json().toType(string(resp), MAP_TYPE);345 }346}...

Full Screen

Full Screen

Source:WebSocket.java Github

copy

Full Screen

1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.remote.http;18import java.io.Closeable;19import java.util.function.Consumer;20import java.util.logging.Logger;21import static java.util.logging.Level.WARNING;22public interface WebSocket extends Closeable {23 WebSocket send(Message message);24 default WebSocket sendText(CharSequence data) {25 return send(new TextMessage(data));26 }27 default WebSocket sendBinary(byte[] data) {28 return send(new BinaryMessage(data));29 }30 @Override31 void close();32 interface Listener extends Consumer<Message> {33 default void accept(Message message) {34 if (message instanceof BinaryMessage) {35 onBinary(((BinaryMessage) message).data());36 } else if (message instanceof CloseMessage) {37 onClose(((CloseMessage) message).code(), ((CloseMessage) message).reason());38 } else if (message instanceof TextMessage) {39 onText(((TextMessage) message).text());40 }41 }42 default void onBinary(byte[] data) {43 // Does nothing44 }45 default void onClose(int code, String reason) {46 // Does nothing47 }48 default void onText(CharSequence data) {49 // Does nothing50 }51 default void onError(Throwable cause) {52 Logger.getLogger(WebSocket.class.getName()).log(WARNING, cause.getMessage(), cause);53 }54 }55}...

Full Screen

Full Screen

Source:Message.java Github

copy

Full Screen

1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.remote.http;18public interface Message {19}...

Full Screen

Full Screen

Interface Message

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.Message;2import org.openqa.selenium.remote.http.HttpRequest;3import org.openqa.selenium.remote.http.HttpResponse;4import org.openqa.selenium.remote.http.HttpMethod;5import org.openqa.selenium.remote.http.HttpHeader;6import org.openqa.selenium.remote.http.W3CHttpCommandCodec;7import org.openqa.selenium.remote.http.W3CHttpResponseCodec;8import org.openqa.selenium.remote.http.W3CHttpResponse;9import org.openqa.selenium.remote.http.W3CHttpRequest;10import org.openqa.selenium.remote.http.W3CHttpResponse;11W3CHttpCommandCodec codec = new W3CHttpCommandCodec();12W3CHttpResponseCodec responseCodec = new W3CHttpResponseCodec();13W3CHttpRequest request = new W3CHttpRequest(HttpMethod.GET, "/status");14W3CHttpResponse response = new W3CHttpResponse();15HttpRequest encodedRequest = codec.encode(request);16HttpResponse encodedResponse = responseCodec.encode(response);17W3CHttpRequest decodedRequest = codec.decode(encodedRequest);18W3CHttpResponse decodedResponse = responseCodec.decode(encodedResponse);19System.out.println(decodedRequest);20System.out.println(decodedResponse);

Full Screen

Full Screen

Interface Message

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.Message;2import org.openqa.selenium.remote.http.Message.Builder;3import org.openqa.selenium.remote.http.Response;4import org.openqa.selenium.remote.http.Response;5import org.openqa.selenium.remote.http.Request;6import org.openqa.selenium.remote.http.Request;7import org.openqa.selenium.remote.http.HttpHandler;8import org.openqa.selenium.remote.http.HttpHandler;9import org.openqa.selenium.remote.http.HttpClient;10import org.openqa.selenium.remote.http.HttpClient;11import org.openqa.selenium.remote.http.HttpRequest;12import org.openqa.selenium.remote.http.HttpRequest;13import org.openqa.selenium.remote.http.HttpResponse;14import org.openqa.selenium.remote.http.HttpResponse;15import org.openqa.selenium.remote.http.HttpVerb;16import org.openqa.selenium.remote.http.HttpVerb;17import org.openqa.selenium.remote.http.Route;18import org.openqa.selenium.remote.http.Route;19import org.openqa.selenium.remote.http.RouteMatcher;20import org.openqa.selenium.remote.http.RouteMatcher;21import org.openqa.selenium.remote.http.RouteMatcher;22import org.openqa.selenium.remote.http.RouteMatcher;23import org.openqa.selenium.remote.http.Route;24import org.openqa.selenium.remote.http.Route;25import org.openqa.selenium.remote.http.RouteMatcher;26import org.openqa.selenium.remote.http.RouteMatcher;27import org.openqa.selenium.remote.http.Route;28import org.openqa.selenium.remote.http.Route;29import org.openqa.selenium.remote.http.RouteMatcher;30import org.openqa.selenium.remote.http.RouteMatcher;31import org.openqa.selenium.remote.http.Route;

Full Screen

Full Screen

Interface Message

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.Message;2import org.openqa.selenium.remote.http.Message.Header;3import org.openqa.selenium.remote.http.Message.Headers;4import org.openqa.selenium.remote.http.Message.Content;5import org.openqa.selenium.remote.http.Message;6import org.openqa.selenium.remote.http.Message.Header;7import org.openqa.selenium.remote.http.Message.Headers;8import org.openqa.selenium.remote.http.Message.Content;9import org.openqa.selenium.remote.http.Message;10import org.openqa.selenium.remote.http.Message.Header;11import org.openqa.selenium.remote.http.Message.Headers;12import org.openqa.selenium.remote.http.Message.Content;13import org.openqa.selenium.remote.http.Message;14import org.openqa.selenium.remote.http.Message.Header;15import org.openqa.selenium.remote.http.Message.Headers;16import org.openqa.selenium.remote.http.Message.Content;17import org.openqa.selenium.remote.http.Message;18import org.openqa.selenium.remote.http.Message.Header;19import org.openqa.selenium.remote.http.Message.Headers;20import org

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

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

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful