I am using selenium in order to intercept network requests , now I wrote a code which listen and do intercept requests but I am struggling in writing all the requests into a json file, this is my code:
public class SeleniumFourFeaturesTest {
WebDriver driver;
DevTools tool;
@BeforeClass
public void startSession() {
//Set Chrome Driver
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
//Set Dev-Tools and create a session
tool = ((ChromeDriver)driver).getDevTools();
tool.createSession();
}
@Test(enabled=false)
public void test1() {
driver.findElement(By.xpath("//div[@class='list-group']/a[1]")).click();
driver.findElement(By.id("ap_email")).sendKeys("test123");
driver.findElement(By.id("ap_password")).sendKeys("12345");
driver.findElement(RelativeLocator.withTagName("input").below(By.id("ap_password"))).click();
}
@Test
public void interceptRequestAndContinue() throws InterruptedException {
//enable Network
tool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//add listener to intercept request and continue
tool.addListener(Network.requestIntercepted(),
requestIntercepted -> tool.send(
Network.continueInterceptedRequest(requestIntercepted.getInterceptionId(),
Optional.empty(),
Optional.empty(),
Optional.empty(), Optional.empty(),
Optional.empty(),
Optional.empty(), Optional.empty())));
//set request interception only for css requests
RequestPattern requestPattern = new RequestPattern("*.css", ResourceType.Stylesheet, InterceptionStage.HeadersReceived);
tool.send(Network.setRequestInterception(ImmutableList.of(requestPattern)));
driver.get("https://www.google.com");
Thread.sleep(30000);
}
}
I would like at the end of the code to be able to write the requests into json file