简体   繁体   中英

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

好吧,覆盖 env var 是有效的,所以这就是事情。

HEADLESS=true rspec

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM