SlightlyKosumi
SlightlyKosumi

Reputation: 775

How to set up performance logging in SeleniumWebdriver with Chrome

Background - trying to get ChromeDriver to log so that I can start using Lighthouse.

I'm trying to get a C# Selenium.Webdriver equivalent of this

DesiredCapabilities cap = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);

Map<String, Object> perfLogPrefs = new HashMap<String, Object>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools"); // comma-separated trace categories
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);

RemoteWebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), cap);

I can't figure out the right combination. Anyone know what the correct recipe is, or have an example, or both?

My attempt

var chromeOptions = new Dictionary<string, object>();
List<string> args = new List<string>();
foreach (string ma in myArgs)   //yes, myArgs is created beforehand
                    args.Add(ma);
chromeOptions.Add("args", args);

var options = new Dictionary<string, object>();
var loggingPrefs = new Dictionary<string, object>();
loggingPrefs.Add("PERFORMANCE", "ALL" );

var perfLoggingPrefs = new Dictionary<string, object>();
perfLoggingPrefs.Add("enableNetwork", true);
perfLoggingPrefs.Add("enablePage", true);
perfLoggingPrefs.Add("traceCategories", "toplevel,disabled-by-default-devtools.timeline.frame,blink.console,disabled-by-default-devtools.timeline,benchmark");

options.Add("chromeOptions", chromeOptions);
options.Add("loggingPrefs", loggingPrefs);
options.Add("perfLoggingPrefs", perfLoggingPrefs);

var capabilities = new DesiredCapabilities(options);
Driver = new RemoteWebDriver(new Uri("http://localhost:9515"), capabilities);

Result: No logging

Upvotes: 2

Views: 9682

Answers (2)

Felipe Faria
Felipe Faria

Reputation: 594

Edit 2022: ChromePerformanceLoggingPreferences was removed so this solution will not work for recent versions.

Deprecated answer:

Not sure what is wrong with your example, but this worked for me:

var options = new ChromeOptions();
var perfLogPrefs = new ChromePerformanceLoggingPreferences();
var tracingCategories = "toplevel,disabled-by-default-devtools.timeline.frame,blink.console,disabled-by-default-devtools.timeline,benchmark";
perfLogPrefs.AddTracingCategories(new string[] { tracingCategories });
options.PerformanceLoggingPreferences = perfLogPrefs;
options.SetLoggingPreference("performance", LogLevel.All);

var Driver = new ChromeDriver(options);

This is how you get the logs afterwards:

var logs = Driver.Manage().Logs.GetLog("performance");

There was a bug in selenium that was fixed in version 3.14 that wouldn't allow this to work. If you're getting a "unrecognized performance logging option: enableTimeline" error try updating selenium.

Upvotes: 5

Joy Salonga
Joy Salonga

Reputation: 51

This worked on my end for local

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.SetLoggingPreference("performance", LogLevel.All);
Instance = new ChromeDriver(chromeOptions);

and for remote

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.SetLoggingPreference("performance", LogLevel.All);
desiredCapabilities = chromeOptions.ToCapabilities() as DesiredCapabilities;
desiredCapabilities?.SetCapability(CapabilityType.BrowserName, settings.Name.ToLower());
desiredCapabilities?.SetCapability(CapabilityType.Version, settings.Version);
desiredCapabilities?.SetCapability(CapabilityType.Platform, GetPlatform(settings.Platform));

to get the logs

var logs = Instance.Manage().Logs.GetLog("performance");

Upvotes: 5

Related Questions