如何使用Chrome在SeleniumWebdriver中设置性能日志记录

时间:2018-06-22 11:28:36

标签: c# logging selenium-webdriver selenium-chromedriver

背景-尝试让ChromeDriver登录,以便我可以开始使用Lighthouse。

我正在尝试获得等效于this的C#Selenium.Webdriver。

<div id="editable" contenteditable="true" class="search-input" [textContent]="query" (input)="Revisedquery=$event.target.textContent" (keyup)="searchClient($event)" (focus)="open()"></div>

我找不到正确的组合。任何人都知道正确的食谱是什么,或者有一个例子,或者两者都有?

我的尝试

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

结果:没有记录

2 个答案:

答案 0 :(得分:3)

不确定您的示例出了什么问题,但这对我有用:

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

这是以后获取日志的方式:

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

version 3.14中已修复了硒中的一个错误,该错误无法正常运行。如果收到“无法识别的性能日志记录选项:enableTimeline”错误,请尝试更新硒。

答案 1 :(得分:0)

这对我来说是本地的

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

和远程

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

获取日志

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