t56k
t56k

Reputation: 7011

Rspec and Chrome/headless flag

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

Upvotes: 2

Views: 3474

Answers (3)

Unkas
Unkas

Reputation: 3708

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

Upvotes: 3

Matthew Jaeger
Matthew Jaeger

Reputation: 79

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

Upvotes: 1

t56k
t56k

Reputation: 7011

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

HEADLESS=true rspec

Upvotes: 1

Related Questions