selenium
selenium copied to clipboard
[🐛 Bug]: CDP - Emulate network condition not applied for more than one browser tab
What happened?
Hi, I wanted use CDP methods in my tests and I faced with command like Network.emulateNetworkConditions. In my tests Im using one chrome/edge instance for two different users - Client/Admin. For example, test opens first tab for client and second tab for admin. I wanted emulate network conditions, but this works only for second(last browser tab).
My expectations was: client(first tab) should has applied Network.emulateNetworkConditions(offline), but it was applied only for second tab(Admin). I tried get all targets and get first tab target and after that use method for AttachToTarget(firstTabTargetId), but this didnt work also.
Its bug or I dont understand how it works?
Thanks
How can we reproduce the issue?
Code for reproduce this issue.
[Test]
public async Task SeleniumCdpTestForEmulateNetwork()
{
_driver.Navigate().GoToUrl("http://www.google.com");
Thread.Sleep(2000);
_driver = _driver.SwitchTo().NewWindow(WindowType.Tab);
_driver.Navigate().GoToUrl("http://stackoverflow.com");
Thread.Sleep(2000);
var session = (_driver as IDevTools).GetDevToolsSession();
var commandParams = new JObject(
new JProperty("offline", true),
new JProperty("latency", 0),
new JProperty("downloadThroughput", -1),
new JProperty("uploadThroughput", -1));
await session.Domains.Network.EnableNetwork();
await session.SendCommand("Network.emulateNetworkConditions", commandParams);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
}
Relevant log output
No logs for this behaviour.
Operating System
Windows 10
Selenium version
Selenium.WebDriver 4.4.0 / netcoreapp3.1
What are the browser(s) and version(s) where you see this issue?
Chrome 105.0.5195.102
What are the browser driver(s) and version(s) where you see this issue?
ChromeDriver 105.0.5195.1900
Are you using Selenium Grid?
No
@mcichonqa, thank you for creating this issue. We will troubleshoot it as soon as we can.
Info for maintainers
Triage this issue by using labels.
If information is missing, add a helpful comment and then I-issue-template
label.
If the issue is a question, add the I-question
label.
If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted
label.
If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C),
add the applicable G-*
label, and it will provide the correct link and auto-close the
issue.
After troubleshooting the issue, please add the R-awaiting answer
label.
Thank you!
I would like to work on this issue.
Go for it! 🎉
Firstly, I think the code to reproduce this issue is for DotNet. @titusfortner Please correct me if I'm wrong.
Secondly, I wrote a similar code in NodeJS and things are working as you expect. That is, client tab is offline and admin is not. Below is the code and the order in which I made CDP connections and opened my tabs. Please refer to comments and try to write your code in the same order.
Solution : After opening a new tab/window, execute the CDP command and then go to the URL.
// Setting up driver
let options = new chrome.Options();
let driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
//Storing the window handle of client
var client = await driver.getWindowHandle()
console.log(client)
// Try block will raise net::ERR_INTERNET_DISCONNECTED
try{
const cdpConnection = await driver.createCDPConnection('page');
var commandParams = {
"offline": true,
"latency": 0,
"downloadThroughput": -1,
"uploadThroughput": -1}
await cdpConnection.execute(
"Network.emulateNetworkConditions",
commandParams
);
// Get the client URL "after" executing the CDP command. This will raise error.
await driver.get("http://google.com")
}
catch{
// Switching to new tab for 'admin'. We can reach the URL stackoverflow.com because we have not
// executed the CDP command for this tab.
await driver.sleep(2000)
await driver.switchTo().newWindow('tab')
await driver.get("http://stackoverflow.com")
var admin = await driver.getWindowHandle()
// Switching back to client tab and getting a URL. This will raise error.
await driver.switchTo().window(client)
await driver.sleep(1000)
// net::ERR_INTERNET_DISCONNECTED raised here
await driver.get("http://amazon.com")
}
finally{
// Switching back to admin and getting a URL. This works again.
await driver.sleep(1000)
await driver.switchTo().window(admin)
// URL available
await driver.get("http://fb.com")
}
await driver.quit()
Let me know your results. If you want me to write the same code in DotNot, I will do that too.
You are right, @TamsilAmani, the code is .NET.
If you have time, any help triaging issues is appreciated 🙇
Oops, I mis-tagged this, sorry! Thanks for looking @TamsilAmani
Thanks for answers. @TamsilAmani could you execute cdpCommand for first tab and second tab with the same result = page offline?
CDP command works only for tab where before I write "var session = (_driver as IDevTools).GetDevToolsSession()".
Command is sended after this line in below example - _driver = _driver.SwitchTo().NewWindow(WindowType.Tab), when second tab exists and is switched to second, but CDP is applied only for first window.
public async Task SeleniumCdpTestForEmulateNetwork()
{
var session = (_driver as IDevTools).GetDevToolsSession();
var commandParams = new JObject(
new JProperty("offline", true),
new JProperty("latency", 0),
new JProperty("downloadThroughput", -1),
new JProperty("uploadThroughput", -1));
_driver.Navigate().GoToUrl("http://www.google.com");
Thread.Sleep(2000);
_driver = _driver.SwitchTo().NewWindow(WindowType.Tab);
_driver.Navigate().GoToUrl("http://stackoverflow.com");
await session.Domains.Network.EnableNetwork();
await session.SendCommand("Network.emulateNetworkConditions", commandParams);
Thread.Sleep(5000);
_driver = _driver.SwitchTo().Window(_driver.WindowHandles[0]);
Thread.Sleep(2000);
}
public async Task SeleniumCdpTestForEmulateNetwork()
{
var session = (_driver as IDevTools).GetDevToolsSession();
var commandParams = new JObject(
new JProperty("offline", true),
new JProperty("latency", 0),
new JProperty("downloadThroughput", -1),
new JProperty("uploadThroughput", -1));
_driver.Navigate().GoToUrl("http://www.google.com");
Thread.Sleep(2000);
_driver = _driver.SwitchTo().NewWindow(WindowType.Tab);
_driver.Navigate().GoToUrl("http://stackoverflow.com");
session = (_driver as IDevTools).GetDevToolsSession(); <- new session here
await session.Domains.Network.EnableNetwork();
await session.SendCommand("Network.emulateNetworkConditions", commandParams);
Thread.Sleep(10000);
_driver = _driver.SwitchTo().Window(_driver.WindowHandles[0]);
Thread.Sleep(5000);
}
Second example also applied CDP command only for first window.
@mcichonqa I have tried the following code and it works for both client and admin tabs.
try{
const cdpConnection_client = await driver.createCDPConnection('page');
await cdpConnection_client.execute(
"Network.emulateNetworkConditions",
commandParams
);
await driver.get("http://google.com") // Error : ERR_INTERNET_DISCONNECTED
}
catch{
await driver.sleep(2000)
await driver.switchTo().newWindow('tab')
const cdpConnection_admin = await driver.createCDPConnection('page'); // New CDPConnection
await cdpConnection_admin.execute(
"Network.emulateNetworkConditions",
commandParams
);
await driver.get("http://stackoverflow.com") // Error : ERR_INTERNET_DISCONNECTED
}
I suggest to create a new connection (with a new variable) and execute the command BEFORE going to the url using _driver.Navigate().GoToUrl("http://stackoverflow.com");
Tell me if things don't work this time. I'll check in DOTNET then.
@TamsilAmani I tried execute the same case, but in .NET we have only GetDevToolsSession method for create WS connection(CDP connection), maybe Im wrong.
Ok, looks like GetDevToolsSession method is a problem. My colleague tried to create workaround and invoke StartSession method in DevToolsSession class. This resolved the problem. So StartSession method allowed me to create several connection for all my required tabs, but I'm not sure about this solution, it only works :)
One invocation GetDevToolsSession method will create one DevToolsSession object and next invocation GetDevToolsSession will return previous created object(this doesn't create next one new), so the new WS connection is unable in this case.
I believe this issue is solved for the specific use case. The .NET way of doing things is similar to Java CDP. So I recommend to close this issue.
Hi, everyone The issue still exists in Selenium 4.6 for .net.
Avoiding calling 'StartSession' method does not help
Is there any workaround for it?
This issue is stale because it has been open 280 days with no activity. Remove stale label or comment or this will be closed in 14 days.
Network Conditions was just completely reworked in .NET for Selenium 4.12 and should address this, if not we can open a new ticket.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.