ruby - 如何使用 Selenium 在 Ruby Capybara 中启用 ChromeDriver 日志记录?
问题描述
我试图在 ruby Capybara 中注册 Selenium chrome 驱动程序。但不幸的是,我没有找到任何有关启用日志记录的信息。我使用了添加了 perfLoggingPrefs: {enableNetwork: true} 的选项,并遇到了我应该启用日志记录的问题。有谁知道如何在 Ruby/Capybara 中启用 ChromeDriver 的日志记录?所以这是我现在的代码:
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
另外,我尝试输入命令行参数,例如
options.add_argument('verbose')
options.add_argument('log-path=./tmp/chromedriver.log')
无论如何我得到问题:
Selenium::WebDriver::Error::InvalidArgumentError:无效参数:“firstMatch”的条目 0 因无效参数无效:指定了 perfLoggingPrefs,但未启用性能日志记录
我已经读过可以启用 ChromeDriver 日志记录,LoggingPreferences
但我找不到任何关于 Ruby 的提及。
有谁知道在注册 Selenium 驱动程序时如何在 Ruby/Capybara 中启用 ChromeDriver 的日志记录?
解决方案
您已经在功能中添加了日志记录首选项。
此外,您可以创建一种方法来将日志存储在您需要的任何位置。这是我用来在您需要的特定文件夹上写入日志的方法。您可以在后挂钩中调用此方法。此外,如果您想在任何 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
推荐阅读
- ajax - ajax get方法一数据为null怎么处理
- javascript - 用单选按钮制作的计算器的问题
- excel - vba 中许多工作簿中的数据 - 限定符无效
- scala - 在 Spark 上提高 distinct + groupByKey 的性能
- python - 为什么每次重新加载页面时我的数据都没有出现
- css - React Native flexShrink 默认是 1,但好像是 0
- python - 删除字典中值为 0 的所有键
- windows-container - EvaluateAsync(),在 2 个不同的会话上序列化评估
- sql - 根据类型计算 SQL Server 用户创建的函数
- sql - postgresql数据库中的PNG图像