How to use ReverseProxyHandler class of org.openqa.selenium.grid.web package

Best Selenium code snippet using org.openqa.selenium.grid.web.ReverseProxyHandler

Source:AutoConfigureNode.java Github

copy

Full Screen

...19import org.openqa.selenium.Capabilities;20import org.openqa.selenium.WebDriverInfo;21import org.openqa.selenium.grid.data.Session;22import org.openqa.selenium.grid.web.CommandHandler;23import org.openqa.selenium.grid.web.ReverseProxyHandler;24import org.openqa.selenium.remote.RemoteWebDriver;25import org.openqa.selenium.remote.http.HttpRequest;26import org.openqa.selenium.remote.http.HttpResponse;27import org.openqa.selenium.remote.service.DriverService;28import java.io.IOException;29import java.net.URISyntaxException;30import java.util.ArrayList;31import java.util.List;32import java.util.ServiceLoader;33import java.util.logging.Logger;34import java.util.stream.Collectors;35import java.util.stream.StreamSupport;36public class AutoConfigureNode {37 public static final Logger log = Logger.getLogger("selenium");38 public static void addSystemDrivers(LocalNode.Builder node) {39 // We don't expect duplicates, but they're fine40 List<WebDriverInfo> infos =41 StreamSupport.stream(ServiceLoader.load(WebDriverInfo.class).spliterator(), false)42 .filter(WebDriverInfo::isAvailable)43 .collect(Collectors.toList());44 // Same45 List<DriverService.Builder> builders = new ArrayList<>();46 ServiceLoader.load(DriverService.Builder.class).forEach(builders::add);47 infos.forEach(info -> {48 Capabilities caps = info.getCanonicalCapabilities();49 builders.stream()50 .filter(builder -> builder.score(caps) > 0)51 .peek(builder -> log.info(String.format("Adding %s %d times", caps, info.getMaximumSimultaneousSessions())))52 .forEach(builder -> {53 for (int i = 0; i < info.getMaximumSimultaneousSessions(); i++) {54 node.add(caps, c -> {55 try {56 DriverService service = builder.build();57 service.start();58 RemoteWebDriver driver = new RemoteWebDriver(service.getUrl(), c);59 return new SessionSpy(service, driver);60 } catch (IOException | URISyntaxException e) {61 throw new RuntimeException(e);62 }63 });64 }65 });66 });67 }68 private static class SessionSpy extends Session implements CommandHandler {69 private final ReverseProxyHandler handler;70 private final DriverService service;71 private final String stop;72 public SessionSpy(DriverService service, RemoteWebDriver driver) throws URISyntaxException {73 super(driver.getSessionId(), service.getUrl().toURI(), driver.getCapabilities());74 handler = new ReverseProxyHandler(service.getUrl());75 this.service = service;76 stop = "/session/" + driver.getSessionId();77 }78 @Override79 public void execute(HttpRequest req, HttpResponse resp) throws IOException {80 handler.execute(req, resp);81 if (DELETE == req.getMethod() && stop.equals(req.getUri())) {82 service.stop();83 }84 }85 }86}...

Full Screen

Full Screen

Source:HandleSession.java Github

copy

Full Screen

...22import org.openqa.selenium.NoSuchSessionException;23import org.openqa.selenium.grid.data.Session;24import org.openqa.selenium.grid.sessionmap.SessionMap;25import org.openqa.selenium.grid.web.CommandHandler;26import org.openqa.selenium.grid.web.ReverseProxyHandler;27import org.openqa.selenium.grid.web.WebDriverUrls;28import org.openqa.selenium.net.Urls;29import org.openqa.selenium.remote.SessionId;30import org.openqa.selenium.remote.http.HttpClient;31import org.openqa.selenium.remote.http.HttpRequest;32import org.openqa.selenium.remote.http.HttpResponse;33import org.openqa.selenium.remote.tracing.DistributedTracer;34import org.openqa.selenium.remote.tracing.Span;35import java.io.IOException;36import java.time.Duration;37import java.util.Objects;38import java.util.concurrent.ExecutionException;39class HandleSession implements CommandHandler {40 private final LoadingCache<SessionId, CommandHandler> knownSessions;41 private final DistributedTracer tracer;42 public HandleSession(43 DistributedTracer tracer,44 HttpClient.Factory httpClientFactory,45 SessionMap sessions) {46 this.tracer = Objects.requireNonNull(tracer);47 Objects.requireNonNull(sessions);48 this.knownSessions = CacheBuilder.newBuilder()49 .expireAfterAccess(Duration.ofMinutes(1))50 .build(new CacheLoader<SessionId, CommandHandler>() {51 @Override52 public CommandHandler load(SessionId id) throws Exception {53 Session session = sessions.get(id);54 if (session instanceof CommandHandler) {55 return (CommandHandler) session;56 }57 HttpClient client = httpClientFactory.createClient(Urls.fromUri(session.getUri()));58 return new ReverseProxyHandler(client);59 }60 });61 }62 @Override63 public void execute(HttpRequest req, HttpResponse resp) throws IOException {64 try (Span span = tracer.createSpan("router.webdriver-command", tracer.getActiveSpan())) {65 span.addTag("http.method", req.getMethod());66 span.addTag("http.url", req.getUri());67 SessionId id = getSessionId(req)68 .orElseThrow(() -> new NoSuchSessionException("Cannot find session: " + req));69 span.addTag("session.id", id);70 try {71 knownSessions.get(id).execute(req, resp);72 span.addTag("http.status", resp.getStatus());...

Full Screen

Full Screen

Source:SessionFactory.java Github

copy

Full Screen

...20import org.openqa.selenium.ImmutableCapabilities;21import org.openqa.selenium.grid.data.Session;22import org.openqa.selenium.grid.sessionmap.SessionMap;23import org.openqa.selenium.grid.web.CommandHandler;24import org.openqa.selenium.grid.web.ReverseProxyHandler;25import java.io.UncheckedIOException;26import java.net.MalformedURLException;27import java.util.Objects;28import java.util.Optional;29import java.util.function.Function;30import java.util.function.Predicate;31class SessionFactory32 implements Predicate<Capabilities>, Function<Capabilities, Optional<SessionAndHandler>> {33 private final SessionMap sessions;34 private final Capabilities capabilities;35 private final Function<Capabilities, Session> generator;36 private volatile boolean available = true;37 SessionFactory(38 SessionMap sessions,39 Capabilities capabilities,40 Function<Capabilities, Session> generator) {41 this.sessions = Objects.requireNonNull(sessions);42 this.capabilities = Objects.requireNonNull(ImmutableCapabilities.copyOf(capabilities));43 this.generator = Objects.requireNonNull(generator);44 }45 public Capabilities getCapabilities() {46 return capabilities;47 }48 public boolean isAvailable() {49 return available;50 }51 @Override52 public boolean test(Capabilities capabilities) {53 if (!isAvailable()) {54 return false;55 }56 return this.capabilities.getCapabilityNames().stream()57 .allMatch(name -> Objects.equals(58 this.capabilities.getCapability(name), capabilities.getCapability(name)));59 }60 @Override61 public Optional<SessionAndHandler> apply(Capabilities capabilities) {62 if (!test(capabilities)) {63 return Optional.empty();64 }65 this.available = false;66 Session session;67 try {68 session = generator.apply(capabilities);69 } catch (Throwable throwable) {70 this.available = true;71 return Optional.empty();72 }73 sessions.add(session);74 CommandHandler handler;75 if (session instanceof CommandHandler) {76 handler = (CommandHandler) session;77 } else {78 try {79 handler = new ReverseProxyHandler(session.getUri().toURL());80 } catch (MalformedURLException e) {81 throw new UncheckedIOException(e);82 }83 }84 String killUrl = "/session/" + session.getId();85 CommandHandler killingHandler = (req, res) -> {86 if (req.getMethod() == DELETE && killUrl.equals(req.getUri())) {87 try {88 sessions.remove(session.getId());89 } finally {90 available = true;91 }92 }93 handler.execute(req, res);...

Full Screen

Full Screen

Source:ProtocolConvertingSession.java Github

copy

Full Screen

...18import static org.openqa.selenium.remote.http.HttpMethod.DELETE;19import org.openqa.selenium.Capabilities;20import org.openqa.selenium.grid.web.CommandHandler;21import org.openqa.selenium.grid.web.ProtocolConverter;22import org.openqa.selenium.grid.web.ReverseProxyHandler;23import org.openqa.selenium.remote.Dialect;24import org.openqa.selenium.remote.SessionId;25import org.openqa.selenium.remote.http.HttpClient;26import org.openqa.selenium.remote.http.HttpRequest;27import org.openqa.selenium.remote.http.HttpResponse;28import java.io.IOException;29import java.net.URL;30import java.util.Objects;31public abstract class ProtocolConvertingSession extends BaseActiveSession {32 private final CommandHandler handler;33 private final String killUrl;34 protected ProtocolConvertingSession(35 HttpClient client,36 SessionId id,37 URL url,38 Dialect downstream,39 Dialect upstream,40 Capabilities capabilities) {41 super(id, url, downstream, upstream, capabilities);42 Objects.requireNonNull(client);43 if (downstream.equals(upstream)) {44 this.handler = new ReverseProxyHandler(client);45 } else {46 this.handler = new ProtocolConverter(client, downstream, upstream);47 }48 this.killUrl = "/session/" + id;49 }50 @Override51 public void execute(HttpRequest req, HttpResponse resp) throws IOException {52 handler.execute(req, resp);53 if (req.getMethod() == DELETE && killUrl.equals(req.getUri())) {54 stop();55 }56 }57}...

Full Screen

Full Screen

ReverseProxyHandler

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.grid.web.ReverseProxyHandler;2import org.openqa.selenium.net.NetworkUtils;3import org.openqa.selenium.remote.http.HttpClient;4import org.openqa.selenium.remote.http.HttpHandler;5import org.openqa.selenium.remote.http.HttpRequest;6import org.openqa.selenium.remote.http.HttpResponse;7public class ReverseProxyHandlerDemo {8 public static void main(String[] args) {9 HttpClient client = HttpClient.Factory.createDefault().createClient(new NetworkUtils().getIpOfLoopbackIp4());10 HttpRequest request = new HttpRequest("GET", "/search?q=webdriver");11 HttpResponse response = handler.execute(request);12 System.out.println(response);13 }14}15HttpResponse{statusCode=200, headers={Content-Type=[text/html; charset=ISO-8859-1], Expires=[-1], Cache-Control=[private, max-age=0], Content-Encoding=[gzip], Server=[gws], Date=[Thu, 04 Mar 2021 08:53:06 GMT], Content-Length=[11371], X-XSS-Protection=[0], X-Frame-Options=[SAMEORIGIN], Alt-Svc=[h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"], Set-Cookie=[1P_JAR=2021-03-04-08; expires=Sat, 03-Apr-2021 08:53:06 GMT; path=/; domain=.google.com, NID=208=Zb6hXy8Vv1LlHlYDhJGjZd0VwJNwZf6F0H6J5Ue9vU6d5j6Y5c6Q4x4ZwF4Zb4A4Q4Q2bS9nLgJdYzLl1nC4dWfJ1eZKxJ; expires=Fri, 04-Sep-2021 08:53:06 GMT

Full Screen

Full Screen

ReverseProxyHandler

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.grid.web.ReverseProxyHandler;2public class MyReverseProxyHandler extends ReverseProxyHandler {3 private final String host;4 private final int port;5 public MyReverseProxyHandler(String host, int port) {6 this.host = host;7 this.port = port;8 }9 protected URI getTargetUri(HttpServletRequest request) {10 try {11 String path = request.getRequestURI();12 if (request.getQueryString() != null) {13 path += "?" + request.getQueryString();14 }15 return new URI("http", null, host, port, path, null, null);16 } catch (URISyntaxException e) {17 throw new UncheckedIOException(e);18 }19 }20}21import org.openqa.selenium.net.NetworkUtils;22import org.openqa.selenium.net.PortProber;23import org.openqa.selenium.remote.http.HttpClient;24import org.openqa.selenium.remote.http.HttpHandler;25import org.openqa.selenium.remote.http.HttpRequest;26import org.openqa.selenium.remote.http.HttpResponse;27import java.io.IOException;28import java.net.URI;29import java.util.logging.Logger;30public class Main {31 private static final Logger LOG = Logger.getLogger(Main.class.getName());32 public static void main(String[] args) {33 int port = PortProber.findFreePort();34 NetworkUtils networkUtils = new NetworkUtils();35 String host = networkUtils.getPrivateLocalAddress();36 HttpHandler handler = new MyReverseProxyHandler(host, port);37 HttpClient.Factory factory = HttpClient.Factory.createDefault();38 new GridServer(39 .start();40 try {41 HttpClient client = factory.createClient(uri);42 HttpResponse response = client.execute(new HttpRequest("GET", "/status"));43 LOG.info("Status: " + response);44 } catch (IOException e) {45 LOG.info("Unable to contact server: " + e.getMessage());46 }47 }48}49import org.openqa.selenium.net.NetworkUtils;50import org.openqa.selenium.net.PortProber;51import org.openqa.selenium.remote.http.HttpClient;52import org.openqa.selenium.remote.http.HttpHandler;53import org.openqa.selenium.remote.http.HttpRequest;54import org.openqa.selenium.remote.http.HttpResponse;55import java.io.IOException;56import java.net.URI;57import java.util.logging.Logger;

Full Screen

Full Screen

ReverseProxyHandler

Using AI Code Generation

copy

Full Screen

1package org.openqa.selenium.grid.web;2import com.google.common.collect.ImmutableList;3import com.google.common.collect.ImmutableMap;4import com.google.common.collect.ImmutableSet;5import com.google.common.collect.ImmutableSortedMap;6import com.google.common.collect.ImmutableSortedSet;7import com.google.common.collect.Lists;8import com.google.common.collect.Maps;9import com.google.common.collect.Sets;10import com.google.common.collect.SortedSetMultimap;11import com.google.common.collect.TreeMultimap;12import com.google.common.io.ByteStreams;13import com.google.common.io.CharStreams;14import com.google.common.io.Files;15import com.google.common.io.Resources;16import com.google.common.net.MediaType;17import com.google.common.reflect.ClassPath;18import com.google.common.reflect.ClassPath.ClassInfo;19import com.google.common.reflect.ClassPath.ResourceInfo;20import com.google.common.reflect.TypeToken;21import com.google.common.util.concurrent.AbstractIdleService;22import com.google.common.util.concurrent.Service;23import com.google.inject.AbstractModule;24import com.google.inject.Binding;25import com.google.inject.Injector;26import com.google.inject.Key;27import com.google.inject.Module;28import com.google.inject.TypeLiteral;29import com.google.inject.name.Named;30import com.google.inject.util.Modules;31import com.google.inject.util.Types;32import com.google.template.soy.SoyFileSet;

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.

Most used methods in ReverseProxyHandler

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