Source:Spring Boot - How to log all requests and responses with exceptions in single place?
webdriver.gecko.driver="/lib/geckodriver-v0.26.0-win64/geckodriver.exe"
Best Selenium code snippet using org.openqa.selenium.remote.tracing.HttpTracing
Source:GridStatusHandler.java  
...26import org.openqa.selenium.remote.http.HttpResponse;27import org.openqa.selenium.remote.tracing.AttributeKey;28import org.openqa.selenium.remote.tracing.EventAttribute;29import org.openqa.selenium.remote.tracing.EventAttributeValue;30import org.openqa.selenium.remote.tracing.HttpTracing;31import org.openqa.selenium.remote.tracing.Span;32import org.openqa.selenium.remote.tracing.Status;33import org.openqa.selenium.remote.tracing.Tracer;34import java.io.IOException;35import java.util.HashMap;36import java.util.List;37import java.util.Map;38import java.util.concurrent.CompletableFuture;39import java.util.concurrent.ExecutionException;40import java.util.concurrent.ExecutorService;41import java.util.concurrent.Executors;42import java.util.concurrent.Future;43import java.util.concurrent.ScheduledExecutorService;44import java.util.concurrent.TimeoutException;45import static java.util.concurrent.TimeUnit.MILLISECONDS;46import static java.util.concurrent.TimeUnit.SECONDS;47import static java.util.stream.Collectors.toList;48import static org.openqa.selenium.json.Json.MAP_TYPE;49import static org.openqa.selenium.remote.http.Contents.asJson;50import static org.openqa.selenium.remote.http.Contents.string;51import static org.openqa.selenium.remote.http.HttpMethod.GET;52import static org.openqa.selenium.remote.tracing.HttpTracing.newSpanAsChildOf;53import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;54import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;55import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE_EVENT;56class GridStatusHandler implements HttpHandler {57  private static final ScheduledExecutorService58      SCHEDULED_SERVICE =59      Executors.newScheduledThreadPool(60          1,61          r -> {62            Thread thread = new Thread(r, "Scheduled grid status executor");63            thread.setDaemon(true);64            return thread;65          });66  private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool(67      r -> {68        Thread thread = new Thread(r, "Grid status executor");69        thread.setDaemon(true);70        return thread;71      });72  private final Json json;73  private final Tracer tracer;74  private final HttpClient.Factory clientFactory;75  private final Distributor distributor;76  GridStatusHandler(Json json, Tracer tracer, HttpClient.Factory clientFactory, Distributor distributor) {77    this.json = Require.nonNull("JSON encoder", json);78    this.tracer = Require.nonNull("Tracer", tracer);79    this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);80    this.distributor = Require.nonNull("Distributor", distributor);81  }82  @Override83  public HttpResponse execute(HttpRequest req) {84    long start = System.currentTimeMillis();85    try (Span span = newSpanAsChildOf(tracer, req, "router.status")) {86      Map<String, EventAttributeValue> attributeMap = new HashMap<>();87      attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(),88                       EventAttribute.setValue(getClass().getName()));89      DistributorStatus status;90      try {91        status = EXECUTOR_SERVICE.submit(span.wrap(distributor::getStatus)).get(2, SECONDS);92      } catch (ExecutionException | TimeoutException e) {93        span.setAttribute("error", true);94        span.setStatus(Status.CANCELLED);95        EXCEPTION.accept(attributeMap, e);96        attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),97                         EventAttribute.setValue("Unable to get distributor status due to execution error or timeout: " + e.getMessage()));98        span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);99        return new HttpResponse().setContent(asJson(100          ImmutableMap.of("value", ImmutableMap.of(101            "ready", false,102            "message", "Unable to read distributor status."))));103      } catch (InterruptedException e) {104        span.setAttribute("error", true);105        span.setStatus(Status.ABORTED);106        EXCEPTION.accept(attributeMap, e);107        attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),108                         EventAttribute.setValue("Interruption while getting distributor status: " + e.getMessage()));109        Thread.currentThread().interrupt();110        return new HttpResponse().setContent(asJson(111          ImmutableMap.of("value", ImmutableMap.of(112            "ready", false,113            "message", "Reading distributor status was interrupted."))));114      }115      boolean ready = status.hasCapacity();116      long remaining = System.currentTimeMillis() + 2000 - start;117      List<Future<Map<String, Object>>> nodeResults = status.getNodes().stream()118        .map(node -> {119          ImmutableMap<String, Object> defaultResponse = ImmutableMap.of(120            "id", node.getId(),121            "uri", node.getUri(),122            "maxSessions", node.getMaxSessionCount(),123            "slots", node.getSlots(),124            "warning", "Unable to read data from node.");125          CompletableFuture<Map<String, Object>> toReturn = new CompletableFuture<>();126          Future<?> future = EXECUTOR_SERVICE.submit(127            () -> {128              try {129                HttpClient client = clientFactory.createClient(node.getUri().toURL());130                HttpRequest nodeStatusReq = new HttpRequest(GET, "/se/grid/node/status");131                HttpTracing.inject(tracer, span, nodeStatusReq);132                HttpResponse res = client.execute(nodeStatusReq);133                toReturn.complete(res.getStatus() == 200134                                  ? json.toType(string(res), MAP_TYPE)135                                  : defaultResponse);136              } catch (IOException e) {137                toReturn.complete(defaultResponse);138              }139            });140          SCHEDULED_SERVICE.schedule(141            () -> {142              if (!toReturn.isDone()) {143                toReturn.complete(defaultResponse);144                future.cancel(true);145              }...Source:ProtocolConverter.java  
...39import org.openqa.selenium.remote.http.HttpResponse;40import org.openqa.selenium.remote.tracing.AttributeKey;41import org.openqa.selenium.remote.tracing.EventAttribute;42import org.openqa.selenium.remote.tracing.EventAttributeValue;43import org.openqa.selenium.remote.tracing.HttpTracing;44import org.openqa.selenium.remote.tracing.Span;45import org.openqa.selenium.remote.tracing.Status;46import org.openqa.selenium.remote.tracing.Tracer;47import java.io.UncheckedIOException;48import java.util.HashMap;49import java.util.Map;50import java.util.Objects;51import java.util.function.Function;52import static java.net.HttpURLConnection.HTTP_OK;53import static java.nio.charset.StandardCharsets.UTF_8;54import static org.openqa.selenium.json.Json.MAP_TYPE;55import static org.openqa.selenium.remote.Dialect.W3C;56import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;57import static org.openqa.selenium.remote.RemoteTags.SESSION_ID_EVENT;58import static org.openqa.selenium.remote.http.Contents.bytes;59import static org.openqa.selenium.remote.http.Contents.string;60import static org.openqa.selenium.remote.tracing.HttpTracing.newSpanAsChildOf;61import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST;62import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST_EVENT;63import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;64import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE_EVENT;65import static org.openqa.selenium.remote.tracing.Tags.KIND;66public class ProtocolConverter implements HttpHandler {67  private static final Json JSON = new Json();68  private static final ImmutableSet<String> IGNORED_REQ_HEADERS = ImmutableSet.<String>builder()69    .add("connection")70    .add("content-length")71    .add("content-type")72    .add("keep-alive")73    .add("proxy-authorization")74    .add("proxy-authenticate")75    .add("proxy-connection")76    .add("te")77    .add("trailer")78    .add("transfer-encoding")79    .add("upgrade")80    .build();81  private final Tracer tracer;82  private final HttpClient client;83  private final CommandCodec<HttpRequest> downstream;84  private final CommandCodec<HttpRequest> upstream;85  private final ResponseCodec<HttpResponse> downstreamResponse;86  private final ResponseCodec<HttpResponse> upstreamResponse;87  private final JsonToWebElementConverter converter;88  private final Function<HttpResponse, HttpResponse> newSessionConverter;89  public ProtocolConverter(90    Tracer tracer,91    HttpClient client,92    Dialect downstream,93    Dialect upstream) {94    this.tracer = Require.nonNull("Tracer", tracer);95    this.client = Require.nonNull("HTTP client", client);96    this.downstream = getCommandCodec(Require.nonNull("Downstream dialect", downstream));97    this.downstreamResponse = getResponseCodec(downstream);98    this.upstream = getCommandCodec(Require.nonNull("Upstream dialect", upstream));99    this.upstreamResponse = getResponseCodec(upstream);100    converter = new JsonToWebElementConverter(null);101    newSessionConverter = downstream == W3C ? this::createW3CNewSessionResponse : this::createJwpNewSessionResponse;102  }103  @Override104  public HttpResponse execute(HttpRequest req) throws UncheckedIOException {105    try (Span span = newSpanAsChildOf(tracer, req, "protocol_converter")) {106      Map<String, EventAttributeValue> attributeMap = new HashMap<>();107      attributeMap.put(AttributeKey.HTTP_HANDLER_CLASS.getKey(),108                       EventAttribute.setValue(getClass().getName()));109      Command command = downstream.decode(req);110      KIND.accept(span, Span.Kind.SERVER);111      HTTP_REQUEST.accept(span, req);112      HTTP_REQUEST_EVENT.accept(attributeMap, req);113      SessionId sessionId = command.getSessionId();114      SESSION_ID.accept(span, sessionId);115      SESSION_ID_EVENT.accept(attributeMap, sessionId);116      String commandName = command.getName();117      span.setAttribute("command.name", commandName);118      attributeMap.put("command.name", EventAttribute.setValue(commandName));119      attributeMap.put("downstream.command.parameters", EventAttribute.setValue(command.getParameters().toString()));120      // Massage the webelements121      @SuppressWarnings("unchecked")122      Map<String, ?> parameters = (Map<String, ?>) converter.apply(command.getParameters());123      command = new Command(124        command.getSessionId(),125        command.getName(),126        parameters);127      attributeMap.put("upstream.command.parameters", EventAttribute.setValue(command.getParameters().toString()));128      HttpRequest request = upstream.encode(command);129      HttpTracing.inject(tracer, span, request);130      HttpResponse res = makeRequest(request);131      if(!res.isSuccessful()) {132        span.setAttribute("error", true);133        span.setStatus(Status.UNKNOWN);134      }135      HTTP_RESPONSE.accept(span, res);136      HTTP_RESPONSE_EVENT.accept(attributeMap, res);137      HttpResponse toReturn;138      if (DriverCommand.NEW_SESSION.equals(command.getName()) && res.getStatus() == HTTP_OK) {139        toReturn = newSessionConverter.apply(res);140      } else {141        Response decoded = upstreamResponse.decode(res);142        toReturn = downstreamResponse.encode(HttpResponse::new, decoded);143      }...Source:HandleSession.java  
...30import org.openqa.selenium.remote.http.HttpResponse;31import org.openqa.selenium.remote.tracing.AttributeKey;32import org.openqa.selenium.remote.tracing.EventAttribute;33import org.openqa.selenium.remote.tracing.EventAttributeValue;34import org.openqa.selenium.remote.tracing.HttpTracing;35import org.openqa.selenium.remote.tracing.Span;36import org.openqa.selenium.remote.tracing.Status;37import org.openqa.selenium.remote.tracing.Tracer;38import java.time.Duration;39import java.util.HashMap;40import java.util.Map;41import java.util.concurrent.Callable;42import java.util.concurrent.ExecutionException;43import static org.openqa.selenium.remote.HttpSessionId.getSessionId;44import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;45import static org.openqa.selenium.remote.RemoteTags.SESSION_ID_EVENT;46import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;47import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST;48import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST_EVENT;49import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;50import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE_EVENT;51class HandleSession implements HttpHandler {52  private final Tracer tracer;53  private final HttpClient.Factory httpClientFactory;54  private final SessionMap sessions;55  private final Cache<SessionId, HttpHandler> knownSessions;56  HandleSession(57    Tracer tracer,58    HttpClient.Factory httpClientFactory,59    SessionMap sessions) {60    this.tracer = Require.nonNull("Tracer", tracer);61    this.httpClientFactory = Require.nonNull("HTTP client factory", httpClientFactory);62    this.sessions = Require.nonNull("Sessions", sessions);63    this.knownSessions = CacheBuilder.newBuilder()64      .expireAfterAccess(Duration.ofMinutes(1))65      .build();66  }67  @Override68  public HttpResponse execute(HttpRequest req) {69    try (Span span = HttpTracing.newSpanAsChildOf(tracer, req, "router.handle_session")) {70      Map<String, EventAttributeValue> attributeMap = new HashMap<>();71      attributeMap.put(AttributeKey.HTTP_HANDLER_CLASS.getKey(),72                       EventAttribute.setValue(getClass().getName()));73      HTTP_REQUEST.accept(span, req);74      HTTP_REQUEST_EVENT.accept(attributeMap, req);75      SessionId id = getSessionId(req.getUri()).map(SessionId::new)76          .orElseThrow(() -> {77            NoSuchSessionException exception = new NoSuchSessionException("Cannot find session: " + req);78            EXCEPTION.accept(attributeMap, exception);79            attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),80                             EventAttribute.setValue(81                                 "Unable to execute request for an existing session: " + exception.getMessage()));82            span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);83            return exception;84          });85      SESSION_ID.accept(span, id);86      SESSION_ID_EVENT.accept(attributeMap, id);87      try {88        HttpTracing.inject(tracer, span, req);89        HttpResponse res = knownSessions.get(id, loadSessionId(tracer, span, id)).execute(req);90        HTTP_RESPONSE.accept(span, res);91        return res;92      } catch (ExecutionException e) {93        span.setAttribute("error", true);94        span.setStatus(Status.CANCELLED);95        EXCEPTION.accept(attributeMap, e);96        attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),97                         EventAttribute.setValue("Unable to execute request for an existing session: " + e.getMessage()));98        span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);99        Throwable cause = e.getCause();100        if (cause instanceof RuntimeException) {101          throw (RuntimeException) cause;102        }...Source:SpanWrappedHttpHandler.java  
...25import java.util.UUID;26import java.util.function.Function;27import java.util.logging.Level;28import java.util.logging.Logger;29import static org.openqa.selenium.remote.tracing.HttpTracing.newSpanAsChildOf;30import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;31import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST_EVENT;32import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE_EVENT;33import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST;34import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;35import static org.openqa.selenium.remote.tracing.Tags.KIND;36public class SpanWrappedHttpHandler implements HttpHandler {37  private static final Logger LOG = Logger.getLogger(SpanWrappedHttpHandler.class.getName());38  private final Tracer tracer;39  private final Function<HttpRequest, String> namer;40  private final HttpHandler delegate;41  public SpanWrappedHttpHandler(Tracer tracer, Function<HttpRequest, String> namer, HttpHandler delegate) {42    this.tracer = Require.nonNull("Tracer", tracer);43    this.namer = Require.nonNull("Naming function", namer);44    this.delegate = Require.nonNull("Actual handler", delegate);45  }46  @Override47  public HttpResponse execute(HttpRequest req) throws UncheckedIOException {48    // If there is already a span attached to this request, then do nothing.49    Object possibleSpan = req.getAttribute("selenium.tracing.span");50    Map<String, EventAttributeValue> attributeMap = new HashMap<>();51    attributeMap.put(AttributeKey.HTTP_HANDLER_CLASS.getKey(),52                     EventAttribute.setValue(delegate.getClass().getName()));53    if (possibleSpan instanceof Span) {54      return delegate.execute(req);55    }56    String name = Require.state("Operation name", namer.apply(req)).nonNull("must be set for %s", req);57    TraceContext before = tracer.getCurrentContext();58    Span span = newSpanAsChildOf(tracer, req, name);59    try {60      TraceContext after = tracer.getCurrentContext();61      span.setAttribute("random.key", UUID.randomUUID().toString());62      req.setAttribute("selenium.tracing.span", span);63      if (!(after.getClass().getName().equals("org.openqa.selenium.remote.tracing.empty.NullContext"))) {64        LOG.fine(String.format("Wrapping request. Before %s and after %s", before, after));65      }66      KIND.accept(span, Span.Kind.SERVER);67      HTTP_REQUEST.accept(span, req);68      HTTP_REQUEST_EVENT.accept(attributeMap, req);69      HttpTracing.inject(tracer, span, req);70      HttpResponse res = delegate.execute(req);71      HTTP_RESPONSE.accept(span, res);72      HTTP_RESPONSE_EVENT.accept(attributeMap, res);73      span.addEvent("HTTP request execution complete", attributeMap);74      return res;75    } catch (Throwable t) {76      span.setAttribute("error", true);77      span.setStatus(Status.UNKNOWN);78      EXCEPTION.accept(attributeMap, t);79      attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),80                       EventAttribute.setValue("Unable to execute request: " + t.getMessage()));81      span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);82      LOG.log(Level.WARNING, "Unable to execute request: " + t.getMessage(), t);83      throw t;...Source:ReverseProxyHandler.java  
...29import java.io.UncheckedIOException;30import java.util.HashMap;31import java.util.Map;32import java.util.logging.Logger;33import static org.openqa.selenium.remote.tracing.HttpTracing.newSpanAsChildOf;34import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST;35import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST_EVENT;36import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;37import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE_EVENT;38import static org.openqa.selenium.remote.tracing.Tags.KIND;39public class ReverseProxyHandler implements HttpHandler {40  private static final Logger LOG = Logger.getLogger(ReverseProxyHandler.class.getName());41  private static final ImmutableSet<String> IGNORED_REQ_HEADERS = ImmutableSet.<String>builder()42      .add("connection")43      .add("keep-alive")44      .add("proxy-authorization")45      .add("proxy-authenticate")46      .add("proxy-connection")47      .add("te")...Source:AddToSessionQueue.java  
...14// KIND, either express or implied.  See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.grid.sessionqueue;18import static org.openqa.selenium.remote.tracing.HttpTracing.newSpanAsChildOf;19import static org.openqa.selenium.remote.tracing.Tags.HTTP_REQUEST;20import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;21import org.openqa.selenium.internal.Require;22import org.openqa.selenium.remote.http.HttpHandler;23import org.openqa.selenium.remote.http.HttpRequest;24import org.openqa.selenium.remote.http.HttpResponse;25import org.openqa.selenium.remote.tracing.Span;26import org.openqa.selenium.remote.tracing.Tracer;27class AddToSessionQueue implements HttpHandler {28  private final Tracer tracer;29  private final NewSessionQueuer newSessionQueuer;30  AddToSessionQueue(Tracer tracer, NewSessionQueuer newSessionQueuer) {31    this.tracer = Require.nonNull("Tracer", tracer);32    this.newSessionQueuer = Require.nonNull("New Session Queuer", newSessionQueuer);...1webdriver.gecko.driver="/lib/geckodriver-v0.26.0-win64/geckodriver.exe"21System.setProperty("webdriver.gecko.driver", "C:\\gecko\\geckodriver.exe");2System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");3WebDriver driver = new FirefoxDriver();41FirefoxOptions options = new FirefoxOptions();2options.setPreference("browser.download.folderList", 2);3options.setPreference("browser.download.dir", "C:\\Windows\\temp");4options.setPreference("browser.download.useDownloadDir", true);5options.setPreference("browser.download.viewableInternally.enabledTypes", "");6options.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;text/plain;application/text;text/xml;application/xml");7options.setPreference("pdfjs.disabled", true);  // disable the built-in PDF viewer89WebDriver driver = new FirefoxDriver(options);10driver.get("https://www.mozilla.org/en-US/foundation/documents");11driver.findElement(By.linkText("IRS Form 872-C")).click();12LambdaTest’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.
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.
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.
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.
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.
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.
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.
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.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!
