{"id":4196,"date":"2024-12-12T11:38:39","date_gmt":"2024-12-12T11:38:39","guid":{"rendered":"https:\/\/www.rcvtechnologies.com\/blog\/?p=4196"},"modified":"2024-12-13T11:31:27","modified_gmt":"2024-12-13T11:31:27","slug":"how-to-capture-network-log-in-selenium-4-using-cdpchrome-devtools-protocol","status":"publish","type":"post","link":"https:\/\/www.rcvtechnologies.com\/blog\/how-to-capture-network-log-in-selenium-4-using-cdpchrome-devtools-protocol\/","title":{"rendered":"How to capture Network log in Selenium 4 using CDP(Chrome DevTools Protocol)"},"content":{"rendered":"<div class=\"dm-code-snippet dark default  dm-normal-version\" style=\"background-color:#abb8c3;\" snippet-height=\"\">\n\t\t\t<div class=\"control-language\">\n                <div class=\"dm-buttons\">\n                    <div class=\"dm-buttons-left\">\n                        <div class=\"dm-button-snippet red-button\"><\/div>\n                        <div class=\"dm-button-snippet orange-button\"><\/div>\n                        <div class=\"dm-button-snippet green-button\"><\/div>\n                    <\/div>\n                    <div class=\"dm-buttons-right\">\n                        <a id=\"dm-copy-raw-code\">\n                        <span class=\"dm-copy-text\">Copy Code<\/span>\n                        <span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span>\n                        <span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a>\n                    <\/div>\n                <\/div>\n                <pre class=\"no-line-numbers\"><code id=\"dm-code-raw\" class=\"no-wrap language-php\"><\/p>\n<pre class=\"dm-pre-admin-side\">package testing;\r\nimport java.util.Optional;\r\nimport org.openqa.selenium.chrome.ChromeDriver;\r\nimport org.openqa.selenium.devtools.DevTools;\r\nimport org.openqa.selenium.devtools.v131.network.Network;\r\nimport org.openqa.selenium.devtools.v131.network.model.Request;\r\nimport org.testng.annotations.Test;\r\npublic class Networklog extends BaseTest {\r\n   @Test\r\n   public void captureNetworkRequestLogs() {\r\n      \r\n    DevTools devTools = ((ChromeDriver)driver).getDevTools();\r\n    devTools.createSession();\r\n   \r\n    devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));\r\n   \r\n    devTools.addListener(Network.requestWillBeSent(), requestActivity -&gt;{\r\n   \t   Request request = requestActivity.getRequest();\r\n   \t   System.out.println(request.getUrl());\r\n   \t         \r\n    });\r\n    driver.get(\"https:\/\/weatherstack.com\/\");\r\n   }\r\n}\r\n<\/pre>\n<p><\/code><\/pre>\n\t\t\t<\/div>\n        <\/div>\n<p><span style=\"font-weight: 400;\">Above Java code is using Selenium and Chrome DevTools Protocol (CDP) to capture network request details while visiting the specified webpage (<\/span><span style=\"font-weight: 400;\">https:\/\/weatherstack.com\/<\/span><span style=\"font-weight: 400;\">).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In our Networklog\u00a0 class, the output result will be a list of URLs corresponding to the network requests made by the page <\/span><span style=\"font-weight: 400;\">https:\/\/weatherstack.com\/<\/span><span style=\"font-weight: 400;\"> during the test execution.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here&#8217;s a breakdown of what happens:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>DevTools Session<\/b><span style=\"font-weight: 400;\">: A session is created using <\/span><span style=\"font-weight: 400;\">devTools.createSession()<\/span><span style=\"font-weight: 400;\">, which connects the Selenium WebDriver (ChromeDriver) to the Chrome DevTools Protocol. This allows you to interact with the browser&#8217;s internal mechanisms.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Network Monitoring<\/b><span style=\"font-weight: 400;\">: The line <\/span><span style=\"font-weight: 400;\">devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()))<\/span><span style=\"font-weight: 400;\"> enables the capture of network events, like requests and responses.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Listener for Requests<\/b><span style=\"font-weight: 400;\">: The <\/span><span style=\"font-weight: 400;\">devTools.addListener(Network.requestWillBeSent(), &#8230;)<\/span><span style=\"font-weight: 400;\"> sets up a listener for network requests that are sent during the page load. Every time a request is made, the listener is triggered.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Printing URLs<\/b><span style=\"font-weight: 400;\">: The listener extracts the URL of the network request (<\/span><span style=\"font-weight: 400;\">request.getUrl()<\/span><span style=\"font-weight: 400;\">) and prints it to the console using <\/span><span style=\"font-weight: 400;\">System.out.println(request.getUrl())<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Webpage Load<\/b><span style=\"font-weight: 400;\">: The line <\/span><span style=\"font-weight: 400;\">driver.get(&#8220;https:\/\/weatherstack.com\/&#8221;);<\/span><span style=\"font-weight: 400;\"> causes Selenium to load the page <\/span><span style=\"font-weight: 400;\">https:\/\/weatherstack.com\/<\/span><span style=\"font-weight: 400;\">, and as the page loads, various network requests will be made (e.g., requests for images, scripts, stylesheets, API calls, etc.).<\/span><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>Output :-\u00a0<\/strong><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-4198 size-full\" src=\"https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/12\/snapedit_1734002833627.png\" alt=\"\" width=\"1600\" height=\"892\" srcset=\"https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/12\/snapedit_1734002833627.png 1600w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/12\/snapedit_1734002833627-300x167.png 300w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/12\/snapedit_1734002833627-1024x571.png 1024w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/12\/snapedit_1734002833627-768x428.png 768w, https:\/\/www.rcvtechnologies.com\/blog\/wp-content\/uploads\/2024\/12\/snapedit_1734002833627-1536x856.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\" \/><\/p>\n<p><b>Output Description:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Request URLs<\/b><span style=\"font-weight: 400;\">: The console will output the URLs of all the network requests made by the browser when loading the <\/span><span style=\"font-weight: 400;\">https:\/\/weatherstack.com\/<\/span><span style=\"font-weight: 400;\"> website.<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">You will see URLs corresponding to:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"3\"><span style=\"font-weight: 400;\">The main HTML page (e.g., <\/span><span style=\"font-weight: 400;\">https:\/\/weatherstack.com\/<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"3\"><span style=\"font-weight: 400;\">API calls made to fetch data (e.g., JSON endpoints).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"3\"><span style=\"font-weight: 400;\">Static resources like CSS files, JavaScript files, images, or fonts requested by the page.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">Each URL will be printed on a new line as <\/span><span style=\"font-weight: 400;\">System.out.println(request.getUrl());<\/span><span style=\"font-weight: 400;\"> is executed within the listener.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Timing<\/b><span style=\"font-weight: 400;\">: <\/span>The requests will be logged in the order they are sent by the browser. The network listener captures the request <b>before<\/b> it is sent, so the URLs will appear in real time as they are initiated by the page load.<\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Capturing Chrome DevTools (CDT) results, such as network requests, using Selenium offers several benefits for web automation, debugging, and performance analysis. Here are some key advantages:<\/span><\/p>\n<p><b>\u00a0\u00a0\u00a0Network Traffic Monitoring:<\/b><span style=\"font-weight: 400;\">&#8211;<\/span><\/p>\n<p><b>\u00a0 \u00a01. Detailed Network Insights<\/b><span style=\"font-weight: 400;\">: By capturing network requests, you can track all API calls, resources loaded (e.g., images, scripts), and responses. This is particularly useful for debugging issues related to resource loading or identifying failures in API requests.<\/span><\/p>\n<p><b>\u00a0 \u00a02. Latency and Timing Analysis<\/b><span style=\"font-weight: 400;\">: You can measure the time taken for different requests to complete, identify slow-loading resources, and optimize performance by analyzing the request and response times.<\/span><\/p>\n<p><b>Monitoring External Services and API Interactions:-<\/b><\/p>\n<p><b>API Testing<\/b><span style=\"font-weight: 400;\">: You can monitor interactions with APIs, checking whether correct requests are made and ensuring that the expected responses are returned. This is especially important for applications that rely heavily on external services.<\/span><\/p>\n<p><b>Validating API Responses<\/b><span style=\"font-weight: 400;\">: You can also verify the data returned by APIs and ensure that it is correctly handled by the application (e.g., checking if the correct JSON data is parsed).<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Above Java code is using Selenium and Chrome DevTools Protocol (CDP) to capture network request details while visiting the specified webpage (https:\/\/weatherstack.com\/). In our Networklog\u00a0 class, the output result will be a list of URLs corresponding to the network requests made by the page https:\/\/weatherstack.com\/ during the test execution. Here&#8217;s a breakdown of what happens: [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":4202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[331,173],"tags":[],"class_list":["post-4196","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium","category-tech"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/4196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=4196"}],"version-history":[{"count":5,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/4196\/revisions"}],"predecessor-version":[{"id":4204,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/4196\/revisions\/4204"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/media\/4202"}],"wp:attachment":[{"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=4196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=4196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rcvtechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=4196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}