【问题标题】:How to set up performance logging in SeleniumWebdriver with Chrome如何使用 Chrome 在 SeleniumWebdriver 中设置性能日志记录
【发布时间】:2020-11-12 19:24:42
【问题描述】:

背景 - 试图让 ChromeDriver 进行日志记录,以便我可以开始使用 Lighthouse。

我正在尝试获取与 this 等效的 C# Selenium.Webdriver

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

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

我的尝试

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

结果:没有记录

【问题讨论】:

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


    【解决方案1】:

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

    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 中修复了 selenium 中的一个错误,该错误不允许它工作。如果您收到“无法识别的性能日志记录选项:enableTimeline”错误,请尝试更新 selenium。

    【讨论】:

      【解决方案2】:

      这对我来说是本地的

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-20
        • 2016-06-23
        • 2010-10-31
        • 2012-09-24
        • 2019-11-25
        • 1970-01-01
        • 2022-06-28
        • 2020-02-21
        相关资源
        最近更新 更多