In our ongoing journey through Selenium Chrome DevTools integration, we’ve explored mobile browsing simulation, real-time insights with Selenium CDP Listeners, intercepting and mocking network/API responses, and testing failed network requests. Our exploration now delves into enhancing test efficiency by addressing unwanted network requests. This blog will guide you through the process of necessary network calls, ensuring focused and efficient web testing.
Catch up on our previous blogs:
- Simulating Mobile Browsing
- Selenium CDP Listeners
- Intercepting and Mocking Network/API Responses
- Detecting and Responding to Failed Network Requests
Blocking Unwanted Network Requests
Efficient web testing requires a focused environment, and unnecessary network requests can introduce noise and affect test reliability. Selenium and Chrome DevTools Protocol (CDP) can block unwanted network calls, optimizing test scenarios.
Blocking Network Requests
Let’s take a scenario where we just want to focus on the functionality of an application. Suppose we have a sample e-commerce page website “https://EcomDemoApp.com”, we just want to focus on basic functionality like going to the product page >select a product > add to cart > verifying a message “THIS PRODUCT IS ALREADY ADDED TO CART”. If we go through the usual flow, each time a page loads the images and CSS components on the pages also load, which takes time and makes execution slower. But if we just want to validate the functional logic, we don’t require the images and CSS to load. Hence we can block the loading by blocking the network request that is responsible for these, this makes execution extremely fast.
Note: For example, we are blocking images and CSS components, but as per our requirements, we can block any unwanted network request that seems to be insignificant and causes any kind of issue in testing.
Step-by-Step Guide to Block Network Requests
Let’s dive into a step-by-step guide on how to block unwanted network requests that will block the loading of images and CSS components. Below is the step-by-step code with an explanation for the action:
public class NetworkBlocking { public static void main(String[] args) throws InterruptedException { //initialize driver WebDriverManager.chromedriver().setup(); ChromeDriver driver = new ChromeDriver(); //Create and start DevTool session DevTools devTools = driver.getDevTools(); devTools.createSession(); //send command to CDP methods> cdp method will invoke and get access to chrome devtools //enable network devTools.send(Network.enable(Optional.empty(),Optional.empty(),Optional.empty())); // Block network request by passing list of request URLs as argument in Network.setBlockedURLs() method // We have used regular expression instead if complete URLs, as we know the request URL for the desired network requests ends with .css and .jpg devTools.send(Network.setBlockedURLs(ImmutableList.of("*.jpg","*.css"))); //perform the test //open application driver.get("https://EcomDemoApp.com"); //click link navigating to product page > select product > add to cart > get the text driver.findElement(By.linkText("Browse Products")).click(); driver.findElement(By.linkText("Selenium")).click(); driver.findElement(By.cssSelector(".add-to-cart")).click(); System.out.println(driver.findElement(By.cssSelector("p")).getText()); Thread.sleep(3000); //quit driver.quit(); } }
Explanation:
- Initialize Driver:
- This line initializes the WebDriver for Chrome using WebDriverManager and sets up the driver.
- Create and Start DevTool Session:
- We create a DevTools instance and start a session, allowing us to interact with Chrome DevTools programmatically.
- Enable Network Domain:
- We send a command to enable the network domain in Chrome DevTools, allowing interception and manipulation of network requests.
- Block Network Requests:
- We block specific network requests by passing a list of request URLs to the
Network.setBlockedURLs()
method. Regular expressions (*.jpg, *.css) are used to match the URLs of requests we want to block.
- We block specific network requests by passing a list of request URLs to the
- Perform Test:
- We navigate to a web page, perform actions like clicking links and buttons, and print out some text from the page.
- Quit Driver:
- Finally, we quit the WebDriver instance, closing the browser window.
You can refer to the official documentation for details on various methods and events: Chrome DevTools Protocol.
Conclusion
Blocking unwanted network requests with Selenium CDP commands elevates test efficiency by creating a focused testing environment. This strategy ensures that your tests remain free from interference, providing reliable and precise results, and making execution faster. Stay tuned for more insights in our Selenium Chrome DevTools integration series as we continue to explore advanced web testing techniques. Happy testing!