Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #38246 - Show console logs in rails tests #10461

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any thoughts on implementing it here?

Suggested change
options = Selenium::WebDriver::Chrome::Options.new
options = Selenium::WebDriver::Chrome::Options.new
options.add_option("goog:loggingPrefs", {browser: 'ALL'}) if ENV['SHOW_JS_LOG']

I think that gives you the same result but with less code.

Copy link
Member Author

@MariaAga MariaAga Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have to deal with ShowMeTheCookies.register_adapter(:logging_selenium_chrome, ShowMeTheCookies::SeleniumChrome) and Capybara.register_driver :logging_selenium_chrome so it seems like the same amount of code?

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
Loading