How to use find_available_port method of Capybara Package

Best Capybara code snippet using Capybara.find_available_port

server.rb

Source:server.rb Github

copy

Full Screen

...29 @host = deprecated_options[1] || host30 @reportable_errors = deprecated_options[2] || reportable_errors31 @port = deprecated_options[0] || port32 @port ||= Capybara::Server.ports[port_key]33 @port ||= find_available_port(host)34 @checker = Checker.new(@host, @port)35 end36 def reset_error!37 middleware.clear_error38 end39 def error40 middleware.error41 end42 def using_ssl?43 @checker.ssl?44 end45 def responsive?46 return false if @server_thread&.join(0)47 res = @checker.request { |http| http.get('/__identify__') }48 return res.body == app.object_id.to_s if res.is_a?(Net::HTTPSuccess) || res.is_a?(Net::HTTPRedirection)49 rescue SystemCallError, Net::ReadTimeout, OpenSSL::SSL::SSLError50 false51 end52 def wait_for_pending_requests53 timer = Capybara::Helpers.timer(expire_in: 60)54 while pending_requests?55 raise "Requests did not finish in 60 seconds: #{middleware.pending_requests}" if timer.expired?56 sleep 0.0157 end58 end59 def boot60 unless responsive?61 Capybara::Server.ports[port_key] = port62 @server_thread = Thread.new do63 Capybara.server.call(middleware, port, host)64 end65 timer = Capybara::Helpers.timer(expire_in: 60)66 until responsive?67 raise 'Rack application timed out during boot' if timer.expired?68 @server_thread.join(0.1)69 end70 end71 self72 end73 def base_url74 "http#{'s' if using_ssl?}://#{host}:#{port}"75 end76 private77 def middleware78 @middleware ||= Middleware.new(app, @reportable_errors, @extra_middleware)79 end80 def port_key81 Capybara.reuse_server ? app.object_id : middleware.object_id82 end83 def pending_requests?84 middleware.pending_requests?85 end86 def find_available_port(host)87 server = TCPServer.new(host, 0)88 port = server.addr[1]89 server.close90 # Workaround issue where some platforms (mac, ???) when passed a host91 # of '0.0.0.0' will return a port that is only available on one of the92 # ip addresses that resolves to, but the next binding to that port requires93 # that port to be available on all ips94 server = TCPServer.new(host, port)95 port96 rescue Errno::EADDRINUSE97 retry98 ensure99 server&.close100 end...

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:

YouTube

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?

Helpful

NotHelpful