1import org.openqa.selenium.devtools.DevTools;2import org.openqa.selenium.devtools.v85.network.Network;3import org.openqa.selenium.devtools.v85.network.model.ConnectionType;4import org.openqa.selenium.devtools.v85.network.model.Request;5import org.openqa.selenium.devtools.v85.network.model.Response;6import com.galenframework.testng.GalenTestNgTestBase;7public class NetworkInterceptor extends GalenTestNgTestBase{8 public void networkInterceptor() throws MalformedURLException {9 driver = new ChromeDriver();10 DevTools devTools = ((ChromeDriver) driver).getDevTools();11 devTools.createSession();12 devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));13 devTools.addListener(Network.requestWillBeSent(), request -> {14 Request req = request.getRequest();15 System.out.println("Request URL: " + req.getUrl());16 System.out.println("Request Method: " + req.getMethod());17 System.out.println("Request Headers: " + req.getHeaders());18 System.out.println("Request Post Data: " + req.getPostData());19 });20 devTools.addListener(Network.responseReceived(), response -> {21 Response res = response.getResponse();22 System.out.println("Response URL: " + res.getUrl());23 System.out.println("Response Status: " + res.getStatus());24 System.out.println("Response Status Text: " + res.getStatusText());25 System.out.println("Response Headers: " + res.getHeaders());26 });27 devTools.send(Network.emulateNetworkConditions(false, 100, 1000, 2000, Optional.of(ConnectionType.CELLULAR4G)));28 devTools.send(Network.emulateNetworkConditions(false, 100, 1000, 2000, Optional.of(ConnectionType.CELLULAR3G)));29 devTools.send(Network.emulateNetworkConditions(false, 100, 1000, 2000, Optional.of(ConnectionType.CELLULAR2G)));30 devTools.send(Network.emulateNetworkConditions(false, 100, 1000, 2000, Optional.of(ConnectionType.WIFI)));31 devTools.send(Network.emulateNetworkConditions(false, 100, 1000, 2000, Optional.of(ConnectionType.NONE)));