Home > Back-end >  Can webdriver intercept network requests?
Can webdriver intercept network requests?

Time:10-26

When using the webdriver to start a web page, can you intercept the relevant requests of the web page to obtain the request payload?

CodePudding user response:

Selenium 4 have network intercept capablity Manipulating network traffic is extremely useful for testing web applications because it gives us endless possibilities to configure the environment where the web application will be running. Blocking network traffic, mocking network connections, simulating the network speed, adding headers, etc. The following code shows how to add a header to the HTTP request, which is helpful when our application under test exposes filters requests or exposes specific features depending on the received headers.

@Test
public void addExtraHeaders() {
    ChromeDriver driver = new ChromeDriver();
    DevTools devTools = driver.getDevTools();
    devTools.createSession();
    driver.get("https://manytools.org/http-html-text/http-request-headers/");

        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));

        Headers headers = new Headers(Collections.singletonMap("testing", "selenium"));
    devTools.send(Network.setExtraHTTPHeaders(headers));

        driver.get("https://manytools.org/http-html-text/http-request-headers/");
    driver.quit();
}

Reference : https://saucelabs.com/resources/articles/bidirectional-apis

  • Related