Skip to content

Commit

Permalink
Fixes #38246 - Show console logs in rails tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga committed Feb 27, 2025
1 parent 84a54dc commit b46fff8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions developer_docs/foreman_dev_setup.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ Foreman's integration tests use the https://github.com/teamcapybara/capybara[Cap

Adding `DEBUG_JS_TEST=1` to the test run, will open a web browser and run the tests in chrome.

To print the warning and error javascript logs within your test, include `puts page.driver.browser.logs.get(:browser)` in the test run.
Adding `SHOW_JS_LOG=1` to the test run will enable all levels of javascript logs for viewing.


To run Foreman's integration tests:
[source, shell]
....
Expand Down
23 changes: 22 additions & 1 deletion test/integration_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@

Selenium::WebDriver::Chrome::Service.driver_path = ENV['TESTDRIVER_PATH'] || Foreman::Util.which('chromedriver', Rails.root.join('node_modules', '.bin'))

javascript_driver = ENV.fetch("JS_TEST_DRIVER") { ENV['DEBUG_JS_TEST'] ? :selenium_chrome : :selenium_chrome_headless }.to_sym
javascript_driver = ENV.fetch("JS_TEST_DRIVER") do
if ENV['SHOW_JS_LOG']
:logging_selenium_chrome
elsif ENV['DEBUG_JS_TEST']
:selenium_chrome
else
:selenium_chrome_headless
end
end.to_sym

def chrome_options
options = Selenium::WebDriver::Chrome::Options.new
Expand Down Expand Up @@ -51,6 +59,19 @@ def chrome_options
browser: :chrome,
options: options)
end
elsif javascript_driver == :logging_selenium_chrome
ShowMeTheCookies.register_adapter(javascript_driver, ShowMeTheCookies::SeleniumChrome)
options = chrome_options
options.add_option("goog:loggingPrefs", {browser: 'ALL'})
unless ENV['DEBUG_JS_TEST']
options.args << '--headless'
end
Capybara.register_driver javascript_driver do |app|
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
options: options)
end
else
Capybara.register_driver javascript_driver do |app|
Capybara::Selenium::Driver.new(
Expand Down

0 comments on commit b46fff8

Please sign in to comment.