How to set up performance logging in SeleniumWebdriver with Chrome

梦想与她 提交于 2020-01-04 06:11:34

问题


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


回答1:


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.




回答2:


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");


来源:https://stackoverflow.com/questions/50986959/how-to-set-up-performance-logging-in-seleniumwebdriver-with-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!