How to use middleware method of Capybara Package

Best Capybara code snippet using Capybara.middleware


Source:server.rb Github


Full Screen

1# frozen_string_literal: true2require 'uri'3require 'net/http'4require 'rack'5require 'capybara/server/middleware'6require 'capybara/server/animation_disabler'7require 'capybara/server/checker'8module Capybara9 # @api private10 class Server11 class << self12 def ports13 @ports ||= {}14 end15 end16 attr_reader :app, :port, :host17 def initialize(app,18 *deprecated_options,19 port: Capybara.server_port,20 host: Capybara.server_host,21 reportable_errors: Capybara.server_errors,22 extra_middleware: [])23 warn 'Positional arguments, other than the application, to Server#new are deprecated, please use keyword arguments' unless deprecated_options.empty?24 @app = app25 @extra_middleware = extra_middleware26 @server_thread = nil # suppress warnings27 @host = deprecated_options[1] || host28 @reportable_errors = deprecated_options[2] || reportable_errors29 @port = deprecated_options[0] || port30 @port ||= Capybara::Server.ports[port_key]31 @port ||= find_available_port(host)32 @checker =, @port)33 end34 def reset_error!35 middleware.clear_error36 end37 def error38 middleware.error39 end40 def using_ssl?41 @checker.ssl?42 end43 def responsive?44 return false if @server_thread&.join(0)45 res = @checker.request { |http| http.get('/__identify__') }46 return res.body == app.object_id.to_s if res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)47 rescue SystemCallError, Net::ReadTimeout, OpenSSL::SSL::SSLError48 false49 end50 def wait_for_pending_requests51 timer = Capybara::Helpers.timer(expire_in: 60)52 while pending_requests?53 raise 'Requests did not finish in 60 seconds' if timer.expired?54 sleep 0.0155 end56 end57 def boot58 unless responsive?59 Capybara::Server.ports[port_key] = port60 @server_thread = do61, port, host)62 end63 timer = Capybara::Helpers.timer(expire_in: 60)64 until responsive?65 raise 'Rack application timed out during boot' if timer.expired?66 @server_thread.join(0.1)67 end68 end69 self70 end71 private72 def middleware73 @middleware ||=, @reportable_errors, @extra_middleware)74 end75 def port_key76 Capybara.reuse_server ? app.object_id : middleware.object_id77 end78 def pending_requests?79 middleware.pending_requests?80 end81 def find_available_port(host)82 server =, 0)83 server.addr[1]84 ensure85 server&.close86 end87 end88end...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Capybara automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?