Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure puma when running Capybara?

I'd like to adjust the puma configuration when running Capybara tests. Changing settings in .env, .env.test (I use dotenv), or config/puma.rb has no effect.

Where can I change the configuration?

Rails 5.1, poltergeist 1.15.0, capybara 2.14.0, puma 2.8.2

like image 753
John Bachir Avatar asked May 11 '17 23:05

John Bachir


2 Answers

In ApplicationSystemTestCase, you can configure by passing options to the default :puma server used by Rails in setup.

AFAIK, this will work for any options, but I've only used

  • Silent: true to silence the puma startup output
  • Thread: '1:1' to configure the puma process to only use one thread

Here's how I've setup up rails systems tests to run inside docker containers:

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: { url: "http://selenium:4444/wd/hub" }

  def setup
    Capybara.server_host = '0.0.0.0' # bind to all interfaces
    Capybara.server = :puma, { Silent: true, Threads: '1:1' }
    host! "http://#{IPSocket.getaddress(Socket.gethostname)}"
    super
  end
end
like image 120
erroric Avatar answered Nov 02 '22 09:11

erroric


Generally with Capybara you configure the server in a register_server block. The :puma server definition Capybara provides is

Capybara.register_server :puma do |app, port, host|
  require 'rack/handler/puma'
  Rack::Handler::Puma.run(app, Host: host, Port: port, Threads: "0:4")
end

If you're using Rails 5.1 system testing it has added a layer of abstraction on top of that with server configuration being done in ActionDispatch::SystemTesting::Server#register which is defined as

def register
  Capybara.register_server :rails_puma do |app, port, host|
    Rack::Handler::Puma.run(app, Port: port, Threads: "0:1")
  end
end

Either way you should be able to either overwrite one of the current server registrations

Capybara.register_server :rails_puma do |app, port,host|
  Rack::Handler::Puma.run(app, ...custom settings...)
end

or configure your own

Capybara.register_server :my_custom_puma do |app, port, host|
  Rack::Handler::Puma.run(app, ...custom settings...)
end

Capybara.server = :my_custom_puma
like image 12
Thomas Walpole Avatar answered Nov 02 '22 08:11

Thomas Walpole