Why do my feature tests start slowly with Rails, Selenium and Spring?
Capybara needs to start up its own copy of your app in a separate thread for testing purposes. That start up time, combined with the first request probably triggering asset compilation (assets are compiled in test mode not in dev mode) is what you are seeing.
Rspec+Capybara optionally change JS driver
Never ended up finding an answer for this, so here's the hacky solution I came up with:
The only thing I found that I could reliably change was the tagging system. So I call using -t visual tag and then take it away.
In spec/spec_helper.rb
Rspec.configure do |config|
if config.filter_manager.inclusions[:visual]
Capybara.javascript_driver = :selenium
config.filter_manager.inclusions.delete(:visual)
else
Capybara.javascript_driver = :poltergeist
end
~rest of rspec config code~
Now you can run your tests with rspec (tests to run) -t visual
The main problem with this is that it will prevent you from running specific tests. You can still do a single file with rspec spec/features/signup_spec.rb -t visual
but you cannot add a :54
to run at a specific line number.
Capybara Selenium WebDriver does not see my test data
Changing my database_cleaner.rb file to this fixed the issue, they are no longer running in two different threads.
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before :each do
if Capybara.current_driver == :rack_test
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
end
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
end
Rspec+Capybara optionally change JS driver
Never ended up finding an answer for this, so here's the hacky solution I came up with:
The only thing I found that I could reliably change was the tagging system. So I call using -t visual tag and then take it away.
In spec/spec_helper.rb
Rspec.configure do |config|
if config.filter_manager.inclusions[:visual]
Capybara.javascript_driver = :selenium
config.filter_manager.inclusions.delete(:visual)
else
Capybara.javascript_driver = :poltergeist
end
~rest of rspec config code~
Now you can run your tests with rspec (tests to run) -t visual
The main problem with this is that it will prevent you from running specific tests. You can still do a single file with rspec spec/features/signup_spec.rb -t visual
but you cannot add a :54
to run at a specific line number.
Comparison of capybara-webkit vs selenium-webdriver
You're confusing a few things here. Capybara
is a testing framework/DSL, for Ruby, which can be used with any of the test runner frameworks (RSpec, Minitest, etc). It can use a number a of different drivers to communicate with the web app being tested.
The default driver is rack_test which doesn't support any JS and cannot connect to any addresses outside the app under test.
A second driver option is selenium-webdriver
which can control multiple different real browsers firefox/chrome/safari/etc. for testing, and can connect to any valid URL. The downside of using selenium-webdriver as the driver is that it opens a real browser and is therefore usually slower with a larger memory footprint.
Another driver option is capybara-webkit
which is headless and can also connect to any valid URL. It is generally faster than using selenium however as it is built on an old version of QtWebkit it doesn't support newer web standards (ES2015, etc) so at a minimum you need to make sure all JS is transpiled to ES5 maximum.
There is nothing to stop you using different drivers for different tests to get the benefits of speed for most tests and then use a real browser for tests that need things like WebRTC, etc. The Capybara README details how to do that when using different test runners (RSpec, Minitest, etc)
delete cookies using capybara webkit
You can use clear_cookies
method:
page.driver.browser.clear_cookies
Related Topics
Where to Put Common Code Found in Multiple Models
Ruby on Rails Gem for Google Map Integration
Array.Join("\N") Not the Way to Join with a Newline
Does Dependency Injection Exist in Rails
How to Remove All Non - Ascii Characters from a String in Ruby
Is a System-Wide Install of Rvm a Bad Idea
How to I Make Private Class Constants in Ruby
Does Objective-C Support Mixin Like Ruby
Purpose of & (Ampersand) in Ruby for Procs and Calling Methods
Ruby Koans: Explicit Scoping on a Class Definition Part 2
How to Manage Ruby Threads So They Finish All Their Work
Retrieving Array of Ids in Mongoid
Getting Ruby 1.8.7 Installed on Mountain Lion (10.8)