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.AutoCloseableProvides 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 theRoutewill 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 org.openqa.selenium.remote.http.HttpResponsePROCEED_WITH_REQUESTReturn this from aRoutablein order to have the browser continue the request unmodified.
-
Constructor Summary
Constructors Constructor Description NetworkInterceptor(org.openqa.selenium.WebDriver driver, org.openqa.selenium.remote.http.Filter filter)NetworkInterceptor(org.openqa.selenium.WebDriver driver, org.openqa.selenium.remote.http.HttpHandler handler)NetworkInterceptor(org.openqa.selenium.WebDriver driver, org.openqa.selenium.remote.http.Routable routable)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()protected org.openqa.selenium.remote.http.HttpMethodconvertFromCdpHttpMethod(java.lang.String method)protected org.openqa.selenium.remote.http.HttpRequestcreateHttpRequest(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)
-
-
-
Constructor Detail
-
NetworkInterceptor
public NetworkInterceptor(org.openqa.selenium.WebDriver driver, org.openqa.selenium.remote.http.HttpHandler handler)
-
NetworkInterceptor
public NetworkInterceptor(org.openqa.selenium.WebDriver driver, org.openqa.selenium.remote.http.Routable routable)
-
NetworkInterceptor
public NetworkInterceptor(org.openqa.selenium.WebDriver driver, org.openqa.selenium.remote.http.Filter filter)
-
-
Method Detail
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
convertFromCdpHttpMethod
protected org.openqa.selenium.remote.http.HttpMethod convertFromCdpHttpMethod(java.lang.String method)
-
createHttpRequest
protected org.openqa.selenium.remote.http.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)
-
-