2

I've got headless Chrome and Chrome working for my Rspec tests. I want a flag to switch between the two so I can see the tests happen when I want and hide them when I don't. How can I implement something like:

rspec --headless 

Right now I just have this secret tied to a .env var:

Capybara.javascript_driver = Rails.application.secrets.headless ? :headless_chrome : :chrome 

3 Answers 3

3

in your rails_helper.rb you should create a statement like that:

RSpec.configure do |config| config.before(:each) do if ENV['HEADLESS'] == 'true' Capybara.current_driver = :selenium_chrome_headless else Capybara.current_driver = :selenium_chrome end end end 

then send a variable when running specs

$ HEADLESS=true rspec ./spec/features 
Sign up to request clarification or add additional context in comments.

Comments

1

Well, overriding the env var works so that's something.

HEADLESS=true rspec 

1 Comment

Just a note for anyone who finds this now. This apparently only works with Rails >= 5.1, not on Rails 5.0.
1

Hmm, I personally use this code to register driver:

Capybara.register_driver :chrome do |app| capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( chromeOptions: { args: [ ('headless' if ENV.fetch('HEADLESS', '1') == '1') ].compact } ) Capybara::Selenium::Driver.new( app, browser: :chrome, desired_capabilities: capabilities ) end 

then in .env you can set the variable to be HEADLESS or not by default and then if you want to overwrite it just type HEADLESS=0 rspec

1 Comment

Not sure how that's different to what I wrote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.