Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can webdriver intercept network requests?

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

like image 756
Riddle Avatar asked Jul 31 '26 08:07

Riddle


1 Answers

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

like image 83
Zakaria Shahed Avatar answered Aug 03 '26 01:08

Zakaria Shahed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!