简体   繁体   English

如何在 Rails 日志文件中从 Selenium(使用 Chrome)获取“console.log”?

[英]How to get "console.log" from Selenium (with Chrome) in the Rails log files?

In Rails project, I'm using Cucumber, with Capybara, in order to run my tests in a Chrome web browser through Selenium.在 Rails 项目中,我使用 Cucumber 和 Capybara,以便通过 Selenium 在 Chrome 网络浏览器中运行我的测试。

My tests are running fine but I would like to get the console.log , console.error and so on in the log files of my Rails application.我的测试运行良好,但我想在 Rails 应用程序的日志文件中获取console.logconsole.error等。

I have registered a :chrome capybara driver like this (based on different articles and SO answers):我已经注册了这样的:chrome capybara 驱动程序(基于不同的文章和 SO 答案):

Capybara.register_driver :chrome do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {
      args: chrome_switches,
    },
    loggingPrefs: {
      browser: 'ALL',
      client: 'ALL',
      driver: 'ALL',
      server: 'ALL'
    }
  )
  Capybara::Selenium::Driver.new(
    app,
    browser: :remote,
    url: "http://selenium:#{ENV['SELENIUM_PORT']}/wd/hub",
    desired_capabilities: capabilities
  )
end

But I have nothing, from the Chrome browser, in the log files.但是我在 Chrome 浏览器的日志文件中没有任何内容。

How to get "console.log" from Selenium (with Chrome) in the Rails log files?如何在 Rails 日志文件中从 Selenium(使用 Chrome)获取“console.log”?

The logs don't show up automatically, you would need to request them from Selenium and then add then write them out to whatever log you like.日志不会自动显示,您需要从 Selenium 请求它们,然后添加然后将它们写到您喜欢的任何日志中。 To request the logs you need to do something like要请求日志,您需要执行以下操作

page.driver.browser.manage.logs.get(:browser) # :driver, etc.

Generally you'd do something like that in an after step and then write it to the log file you want it to be in.通常,您会在之后的步骤中执行类似操作,然后将其写入您希望它所在的日志文件中。

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

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