Skip to content

Commit

Permalink
Retry capybara tests on specific errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-illi committed Aug 24, 2023
1 parent 8a7e69b commit 6135ef5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/support/retry_on_flaky_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Include in `test_helper.rb` like this:
#
# class ActiveSupport::TestCase
# prepend RetryOnFlakyTests[FlakyError, AnotherFlakyError, max_tries: 3]
# end

class RetryOnFlakyTests < Module
def self.[](*error_classes, max_tries: 3)
Module.new do
define_method :run do
tries ||= 1
super()
rescue *error_classes => err
raise unless (tries +=1) <= max_tries

puts "Got #{err.class.name} Error, trying again #{tries}. time"
retry
end

define_method :capture_exceptions do |&block|
block.call
rescue => err
if error_classes.include?(err.class)
raise
else
super(&->{ raise err })
end
end
end
end
end
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
class ActiveSupport::TestCase
include CustomAssertions

prepend RetryOnFlakyTests[
# randomly happening on CI
Ferrum::PendingConnectionsError,
# race condition when trying to move mouse to element, can happen e.g. after fade-in/out of modal dialog
Ferrum::CoordinatesNotFoundError,
# race condition when trying to click element, can happen e.g. after fade-in/out of modal dialog
Capybara::Cuprite::MouseEventFailed,
max_tries: 3
]

# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
Expand Down

0 comments on commit 6135ef5

Please sign in to comment.