Package org.openqa.selenium.devtools
Class NetworkInterceptor
- java.lang.Object
-
- org.openqa.selenium.devtools.NetworkInterceptor
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class NetworkInterceptor extends java.lang.Object implements java.lang.AutoCloseable
Provides a mechanism for stubbing out responses to requests in drivers which implementHasDevTools
. Usage is done by specifying aRoute
, which will be checked for every request to see if that request should be handled or not. Note that the URLs given to theRoute
will be fully qualified.Example usage:
Route route = Route.matching(req -> GET == req.getMethod() && req.getUri().endsWith("/example")) .to(() -> req -> new HttpResponse().setContent(Contents.utf8String("Hello, World!"))); try (NetworkInterceptor interceptor = new NetworkInterceptor(driver, route)) { // Your code here. }
It is also possible to intercept and modify responses that the browser will receive. Do this by calling
NetworkInterceptor(WebDriver, Filter)
.
-
-
Field Summary
Fields Modifier and Type Field Description static HttpResponse
PROCEED_WITH_REQUEST
Return this from aRoutable
in order to have the browser continue the request unmodified.
-
Constructor Summary
Constructors Constructor Description NetworkInterceptor(WebDriver driver, Filter filter)
NetworkInterceptor(WebDriver driver, HttpHandler handler)
NetworkInterceptor(WebDriver driver, Routable routable)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
protected HttpMethod
convertFromCdpHttpMethod(java.lang.String method)
protected HttpRequest
createHttpRequest(java.lang.String cdpMethod, java.lang.String url, java.util.Map<java.lang.String,java.lang.Object> headers, java.util.Optional<java.lang.String> postData)
-
-
-
Field Detail
-
PROCEED_WITH_REQUEST
public static final HttpResponse PROCEED_WITH_REQUEST
Return this from aRoutable
in order to have the browser continue the request unmodified.
-
-
Method Detail
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
convertFromCdpHttpMethod
protected HttpMethod convertFromCdpHttpMethod(java.lang.String method)
-
createHttpRequest
protected HttpRequest createHttpRequest(java.lang.String cdpMethod, java.lang.String url, java.util.Map<java.lang.String,java.lang.Object> headers, java.util.Optional<java.lang.String> postData)
-
-