Best Capybara code snippet using Capybara.port_key
server.rb
Source:server.rb
...26 @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 = Checker.new(@host, @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 = Thread.new do61 Capybara.server.call(middleware, 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 def base_url72 "http#{'s' if using_ssl?}://#{host}:#{port}"73 end74 private75 def middleware76 @middleware ||= Middleware.new(app, @reportable_errors, @extra_middleware)77 end78 def port_key79 Capybara.reuse_server ? app.object_id : middleware.object_id80 end81 def pending_requests?82 middleware.pending_requests?83 end84 def find_available_port(host)85 server = TCPServer.new(host, 0)86 server.addr[1]87 ensure88 server&.close89 end90 end91end...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!