Skip to content

Commit

Permalink
Make system tests take failed screenshots in before_teardown hook
Browse files Browse the repository at this point in the history
Previously we were calling the `take_failed_screenshot` method in an
`after_teardown` hook. However, this means that other teardown hooks
have to be executed before we take the screenshot. Since there can be
dynamic updates to the page after the assertion fails and before we
take a screenshot, it seems desirable to minimize that gap as much as
possible. Taking the screenshot in a `before_teardown` rather than an
`after_teardown` helps with that, and has a side benefit of allowing
us to remove the nested `ensure` commented on here:
#34411 (comment)
  • Loading branch information
rmacklin committed Apr 21, 2019
1 parent 80b7d58 commit ef12ccf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
9 changes: 9 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
* Make system tests take a failed screenshot in a `before_teardown` hook
rather than an `after_teardown` hook.

This helps minimize the time gap between when an assertion fails and when
the screenshot is taken (reducing the time in which the page could have
been dynamically updated after the assertion failed).

*Richard Macklin*

* Introduce `ActionDispatch::ActionableExceptions`.

The `ActionDispatch::ActionableExceptions` middleware dispatches actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ def before_setup
super
end

def before_teardown
take_failed_screenshot
ensure
super
end

def after_teardown
begin
take_failed_screenshot
ensure
Capybara.reset_sessions!
end
Capybara.reset_sessions!
ensure
super
end
Expand Down
26 changes: 26 additions & 0 deletions railties/test/application/test_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,32 @@ def reset_sessions!
assert_match "Capybara.reset_sessions! called", output
end

def test_failed_system_test_screenshot_should_be_taken_before_other_teardown
app_file "test/system/failed_system_test_screenshot_should_be_taken_before_other_teardown_test.rb", <<~RUBY
require "application_system_test_case"
require "selenium/webdriver"
class FailedSystemTestScreenshotShouldBeTakenBeforeOtherTeardownTest < ApplicationSystemTestCase
ActionDispatch::SystemTestCase.class_eval do
def take_failed_screenshot
puts "take_failed_screenshot called"
super
end
end
def teardown
puts "test teardown called"
super
end
test "dummy" do
end
end
RUBY
output = run_test_command("test/system/failed_system_test_screenshot_should_be_taken_before_other_teardown_test.rb")
assert_match(/take_failed_screenshot called\n.*test teardown called/, output)
end

def test_system_tests_are_not_run_with_the_default_test_command
app_file "test/system/dummy_test.rb", <<-RUBY
require "application_system_test_case"
Expand Down

0 comments on commit ef12ccf

Please sign in to comment.