问题
I have tried to register Selenium chrome driver in ruby Capybara. 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. Does anyone know how can I enable logging for ChromeDriver in Ruby/Capybara? 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
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
I have read that ChromeDriver logging could be enabled by LoggingPreferences but I couldn`t find any mention of it for Ruby.
Does anyone know how to enable logging for ChromeDriver in Ruby/Capybara while registering Selenium driver?
回答1:
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
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
来源:https://stackoverflow.com/questions/58123519/how-to-enable-chromedriver-logging-in-ruby-capybara-with-selenium