Search code examples
c#selenium-webdriverdebuggingnunit

Selenium runs very slow with headed web driver in VS debug mode and NUnit test run


I'm working on Web automation test and I plan to use Selenium. I use Selenium to manipulate Edge browser to test. I create console application with C#.

EdgeOptions edgeOps = new EdgeOptions();
edgeOps.AddArgument("--in-process-gpu");
edgeOps.AddArgument("--ignore-certificate-errors");
edgeOps.AddArgument("--ignore-ssl-error");
edgeOps.AddArgument("--start-maximized");
edgeOps.AddUserProfilePreference("user_experience_metrics", new { personalization_data_consent_enabled = true });
driver = new EdgeDriver(edgeOps);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(2000);

I create web driver instance by above code and execute quite simple operations. It works. But I observed that the speed differs a lot when I run the code in VS by pressing F5 in debug mode, and run the compiled executable from command line.

In VS debug mode, the code finish in 22 seconds.But in command line it just use 6 seconds. I tried add:

edgeOps.AddArgument("--headless");

And when I run in VS debug mode, the time consumed also fall to 6 seconds, and the time spent by command line is nearly the same.

I noticed that when executing under headed mode in VS debug mode, the mouse cursor blinks very frequetly, I guess some inter process communication is behind the scene.

I also noticed that each operation, such as caputer an element and then send key to assign string value to the element, executes very slow than without VS debugger. I mean the interval of each step seem longer than without debugger.

Besides, I use NUnit to create test case and run the same operation in headed mode. The duration of NUnit test case run is even longer than VS debug mode by around 3 seconds. I think NUnit execute extra initialization than VS debugger so it spends more time but the Selenium part time consumption is nearly the same.

I hope to figure out why selenium runs so slow under headed mode in VS debug mode and/or in NUnit?


Solution

  • The difference in execution speed is likely due to running the code in debug mode, plus the overhead of the additional debugging and diagnostic tools running in Visual Studio. While I haven't measured the difference in performance, I can say anecdotally that I do notice that running automation code (or any code) while debugging in Visual Studio goes much slower.

    This is expected behavior.

    There probably isn't a lot you can do to speed things up when debugging through Visual Studio. Try closing out of all unnecessary diagnostics and debugging tools in Visual Studio. Otherwise, sit back and have a cup of coffee or tea. If the boss walks by, tell them you are running tests.

    And why not? Developers do the same thing.