简体   繁体   English

如何使用 Selenium 在 Ruby Capybara 中启用 ChromeDriver 日志记录?

[英]How to enable ChromeDriver logging in Ruby Capybara with Selenium?

I have tried to register Selenium chrome driver in ruby Capybara.我试图在 ruby Capybara 中注册 Selenium chrome 驱动程序。 But unfortunately, I didn`t find any information about enabling logging.但不幸的是,我没有找到任何有关启用日志记录的信息。 I used options with added perfLoggingPrefs: {enableNetwork: true} and faced issue that i should enable logging.我使用了添加了 perfLoggingPrefs: {enableNetwork: true} 的选项,并遇到了我应该启用日志记录的问题。 Does anyone know how can I enable logging for ChromeDriver in Ruby/Capybara?有谁知道如何在 Ruby/Capybara 中启用 ChromeDriver 的日志记录? So here is my code now:所以这是我现在的代码:

    Capybara.register_driver(:selenium_mobile) do |app|
     options = Selenium::WebDriver::Chrome::Options.new
     options.add_emulation(device_name: 'iPhone X')
     options.add_option(:perfLoggingPrefs, {enableNetwork: true})
     p "Default Selenium driver is used"
     cps = Selenium::WebDriver::Remote::Capabilities.chrome(
       loggingPrefs: {browser: 'ALL'},
       perfLoggingPrefs: {enableNetwork: true})

      Capybara::Selenium::Driver.new(app, browser: :chrome, 
    desired_capabilities: cps, options: 
       options)
      end
    end

Also, I tried to input command line arguments like另外,我尝试输入命令行 arguments 之类的

      options.add_argument('verbose')
      options.add_argument('log-path=./tmp/chromedriver.log')

Anyway i get issue:无论如何我得到问题:

Selenium::WebDriver::Error::InvalidArgumentError: invalid argument: entry 0 of 'firstMatch' is invalid from invalid argument: perfLoggingPrefs specified, but performance logging was not enabled Selenium::WebDriver::Error::InvalidArgumentError:无效参数:“firstMatch”的条目 0 因无效参数无效:指定了 perfLoggingPrefs,但未启用性能日志记录

I have read that ChromeDriver logging could be enabled by LoggingPreferences but I couldn`t find any mention of it for Ruby.我已经读过LoggingPreferences可以启用 ChromeDriver 日志记录,但我找不到 Ruby 的任何提及。

Does anyone know how to enable logging for ChromeDriver in Ruby/Capybara while registering Selenium driver?有谁知道在注册 Selenium 驱动程序时如何在 Ruby/Capybara 中启用 ChromeDriver 的日志记录?

You have already added logging prefs in capabilities.您已经在功能中添加了日志记录首选项。

Also, you can create a method to store the logs where ever you need.此外,您可以创建一种方法来将日志存储在您需要的任何位置。 Here is the method I use to write logs on a specific folder you need.这是我用来在您需要的特定文件夹上写入日志的方法。 You can call this method in after hooks.您可以在后挂钩中调用此方法。 Also, you can store as artifacts if you want to see in any CI/CD application此外,如果您想在任何 CI/CD 应用程序中查看,您可以将其存储为工件

 def capture_browser_logs
  errors = Capybara.page.driver.browser.manage.logs.get(:browser)
                   .select { |e| e.level == "SEVERE" && e.message.present? }
                   .map(&:message)
                   .to_a
  return if errors.none?
  message = errors.join("\n\n")

  # writes console errors to a log file
  log_file_path = Rails.root.join("tmp", "smoke_tests", "console_logs", "js_errors.log")
  FileUtils.mkdir_p(log_file_path.dirname) unless File.directory?(log_file_path.dirname)

  logging_destination = if ENV["RAILS_LOG_TO_STDOUT"].present? && ENV["RAILS_LOG_TO_STDOUT"].to_s == "true"
                          STDOUT
                        else
                          log_file_path
                        end

  logger = Logger.new(logging_destination)
  logger.error(message)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM