Skip to content

Instantly share code, notes, and snippets.

@nandilugio
Created July 6, 2021 13:01
Show Gist options
  • Save nandilugio/31d0c96f9cc300d9cbda1993d8989390 to your computer and use it in GitHub Desktop.
Save nandilugio/31d0c96f9cc300d9cbda1993d8989390 to your computer and use it in GitHub Desktop.
# This is a debugging tool for flaky ruby/rack (rails) browser tests not properly
# waiting for potentially slow server responses (eg. with Capybara). Its a Rack
# middleware that adds a delay to every response, thus making the problem
# evident.
#
# In Rails, add the following to the end of your Application class definition in
# `config/application.rb`, AND REMOVE AFTER DEBUGGING!!!
#
# # TODO: REMOVE!!
# require("slow_requests_for_debugging")
# config.middleware.use SlowRequestsForDebugging
#
class SlowRequestsForDebugging
DELAY_SECS = 5
def initialize(app)
@app = app
end
def call(env)
@request = Rack::Request.new(env)
puts "Holding for #{DELAY_SECS} seconds the response to #{@request.url}"
sleep(DELAY_SECS)
puts "Response hold released for " + @request.url
@app.call(env)
end
end
@nandilugio
Copy link
Author

Other reasons for flaky tests:

  • Outstanding requests leaking to next test: Close the browser session on test teardown.
  • Overlapping DOM elements blocking interaction: Check browser screenshots on failing tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment