简体   繁体   中英

Optionally using capybara-webkit with RSpec

I prefer to use capybara-webkit as the driver used with capybara, but don't expect other developers on my team to jump through the hoop of having QT installed in order to do the same. I'd like to remove capybara-webkit from the gemfile and only use it as the test driver when it can be required. I tried to do this with the following code in my spec_helper.rb file:

begin
  require 'capybara-webkit'
  driver = :webkit
  puts "capybara-webkit has been detected."
rescue LoadError
  driver = :selenium
  puts "capybara-webkit unavailable. Falling back to Selenium"
  puts "For faster, headless integration tests, install capybara-webkit"
end

Capybara.javascript_driver = driver

The problem is that even with capybara-webkit installed, the require fails. I can only assume this is because it's not in my application's bundle. How can I get around this? Is there a way to get around this that doesn't require capybara-webkit to be added to the Gemfile?

Rather than performing a require check, how about checking for another component that should be there instead? Something outside the scope of Ruby. You'd need to do this because of the gem being unavailable in the bundle.

I think you'd just want to add a custom group to the Gemfile and put capybara-webkit there. Other developers can then install the bundle, but exclude that group.

I like to use both as well, in general, webkit is faster, but sometimes I like to see the browser doing stuff(like when I have no idea what is happening, or to show to a client) my resolution is to have both installed via the gemfile, and set this in your spec_helper.rb

Capybara.javascript_driver = :webkit unless ENV['SHOWJS']

This leaves my default to the headless driver, and when I want to see it in selenium I can run this command:

SHOWJS=1 rspec spec/features/user/new_spec.rb

If you use guard or something, you might have to export the environment variable because I don't think you can just pass it in.

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