Selenium 4 beta 2 - C# - Am I using Network correctly or is this a bug only capturing 1st response

962 views
Skip to first unread message

Chris Bloom

unread,
May 14, 2021, 10:15:57 AM5/14/21
to Selenium Users
Hi,

Sample code below for a quick test turning on Network adapter and adding an event listening to ResponseReceived and navigating to 2 webpages. The event handler is only called once though.

Side note I do the same for console logs and the event listener gets called for every log as expected so I'm not sure if this is a bug or my usage is wrong as I haven't seen the proper usage documented for the Selenium 4 beta version yet.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.DevTools.V89.Network;

namespace Selenium_Network_Test
{
    [TestClass]
    public class UnitTest1
    {
        List<string> headers = new List<string>();

        [TestMethod]
        public void TestMethod1()
        {
            
            ChromeOptions options = new ChromeOptions();          

            ChromeDriver driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)), options);
                        
            DevToolsSession session = driver.GetDevToolsSession();
            
            NetworkAdapter networkAdapter = new NetworkAdapter(session);
           
            networkAdapter.Enable(new EnableCommandSettings()).GetAwaiter().GetResult();

            networkAdapter.ResponseReceived += NetworkAdapter_ResponseReceived;

            driver.Navigate().GoToUrl("https://google.com");

            System.Threading.Thread.Sleep(2000);
            
            driver.Navigate().GoToUrl("https://www.selenium.dev/documentation/en/support_packages/chrome_devtools/");
            
            System.Threading.Thread.Sleep(2000);
            
            driver.Close();

            Assert.IsTrue(headers.Count > 1);

        }

        private void NetworkAdapter_ResponseReceived(object sender, ResponseReceivedEventArgs e)
        {
            headers.Add(e.Response.HeadersText);           
        }
    }
}

Chris Bloom

unread,
May 14, 2021, 1:17:34 PM5/14/21
to Selenium Users
I was actually able to get this working making use of the Fetch ( using OpenQA.Selenium.DevTools.V89.Fetch) not Network domain. Little surprised as Fetch domain is listed as experimental in the Chrome Dev Tools protocol, guess the Selenium implementation is built around it

Reply all
Reply to author
Forward
0 new messages