According to capybara's readme on Github I have to require capybara/rails in my spec_helper file after installing the gem:
require 'capybara/rails'
But even if I leave out the require statement in spec_helper, I can still use the following capybara syntax:
scenario "successfully creates a new user account" do
visit "users#index"
fill_in "user_email", with: "foo#bar.com"
click_button "Create User"
expect(page).to have_content "User successfully created"
end
Does it have something to do with the rails g rspec:install command? I am confused about why the syntax works; how does the app know?
All requiring capybara/rails does is set up Capybara.app and configure the rack-test driver to respect any data-method attributes on links. Other than that it doesn't affect capybara. If you've already configured Capybara.app some other way then things will work fine. You may also have it required in your rails_helper.rb rather than spec_helper.rb
Related
I set up a simple controller with its according feature_spec:
stamps_controller.rb
class StampsController < ApplicationController
def new
#stamp = Stamp.new
end
def create
#stamp = Stamp.new(stamp_params)
if #stamp.save
redirect_to(stamp_url(#stamp.id), status: 201)
else
render 'new'
end
end
def show
#stamp = Stamp.find(params[:id])
end
private
def stamp_params
params.require(:stamp).permit(::percentage)
end
end
specs/requests/stamps_request_spec.rb
RSpec.describe 'stamp requests', type: :request do
describe 'stamp creation', js: true do
before do
FactoryBot.create_list(:domain, 2)
FactoryBot.create_list(:label, 2)
end
it 'allows users to create new stamps' do
visit new_stamp_path
expect(page).to have_content('Percentage')
find('#stamp_percentage').set('20')
click_button 'Create'
expect(current_path).to eq(stamp_path(Stamp.first.id))
end
end
end
According to the capybara docs:
Capybara automatically follows any redirects, and submits forms associated with buttons.
But this does not happen in the test, instead it throws an error:
expected: "/stamps/1
got: "/stamps"
The results are obvious: it successfully creates the stamp but fails to redirect to the new stamp. I also confirmed this by using binding.pry.
Why wouldn't capybara follow the redirect as described in the docs?
Sidenotes:
it even fails if I use the normal driver instead of js
I've looked into lots of SO questions and docs, finding nothing useful. One potential attempt I was unable to grasp was an answer with no specifics of how to implement it.
my configs:
support/capybara.rb
require 'capybara/rails'
require 'capybara/rspec'
Capybara.server = :puma
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox, marionette: true)
end
Capybara.javascript_driver = :selenium
RSpec.configure do |config|
config.include Capybara::DSL
end
spec_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
require 'rspec/rails'
require 'factory_bot_rails'
require 'pundit/matchers'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
RSpec.configure do |config|
# .
# unrelated stuff
# .
end
You have a number of issues in your test.
First, Capybara is not meant to be used in request specs - https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec - but should instead be used with feature/system tests. Once you've fixed that you should no longer need to include Capybara into every RSpec test and should remove the config.include Capybara::DSL from you config (when you require capybara/rspec it includes Capybara into the test types it should be included in - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rspec.rb#L10)
Second, click_button is not guaranteed to wait for any actions it triggers to complete. Because of that you need to wait for a visual page change before attempting to access any database objects that would be created by that action (technically you really shouldn't be doing direct DB access in feature specs at all but if you're going to ...)
click_button 'Create'
expect(page).to have_text('Stamp created!') # Whatever message is shown after creation
# Now you can safely access the DB for the created stamp
Third, as pointed out by #chumakoff you should not be using static matchers with Capybara and should instead be using the matchers provided by Capybara
click_button 'Create'
expect(page).to have_text('Stamp created!') # Whatever message is shown after creation
expect(page).to have_path(stamp_path(Stamp.first.id))
Finally, you should look at your test.log and use save_and_open_screenshot to see what your controllers actually did - It's and there's actually an error being raised on creation which is causing your app to redirect to /stamps and display the error message (would also imply your test DB isn't actually being reset between tests, or the factories you show are creating nested records, etc).
Update: After rereading your controller code I noticed the that you're passing a 201 status code to redirect_to. 201 won't actually do a redirect - From the redirect_to docs - https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to
Note that the status code must be a 3xx HTTP code, or redirection will
not occur.
The problem might be that it takes some time for current_path to change after the form is submitted. Your code would work if you put sleep(x) before expect(current_path).
Instead, you should use methods that have so-called "waiting behaviour", such as has_current_path?, have_current_path, assert_current_path:
expect(page).to have_current_path(stamp_path(Stamp.first.id))
or
expect(page.has_current_path?(stamp_path(Stamp.first.id))).to eq true
For anyone else coming here, another possible solution is to increase your wait time. Either globally or per click
# Globally
Capybara.default_max_wait_time = 5
# Per Click
find("#my-button").click(wait: 5)
I'm trying to use Capybara as a front-end test suite (Vue) and I'm getting the following error from Puma:
Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
ActionController::RoutingError:
No route matches [GET] "/login"
What is the cause of this and how can I fix this? Is it something to do with the config? Let me know if I should include more code in my question.
rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require "pundit/rspec"
require "capybara/rails"
require "capybara/rspec"
require "capybara/poltergeist"
require 'database_cleaner'
require 'factory_girl'
require 'shoulda/matchers'
require "paperclip/matchers"
require 'support/spec_helpers/warden_controller_helpers'
require 'webmock/rspec'
require 'support/spec_helpers/webmock_helper'
Capybara.javascript_driver = :poltergeist
login_spec.rb:
require 'rails_helper'
RSpec.feature "User login", type: :feature, js: true do
before :each do
#user = User.create!(
first_name: "Bob",
last_name: "Brown",
email: "bob#email.com",
password: "bob1",
has_accepted_terms: true
)
end
scenario "User logs in with correct credentials" do
visit root_path
save_screenshot
click_on 'Log In'
sleep 1
save_screenshot
end
end
So if I run the dev server and go to 'localhost:3000 (which is basically what you're telling Capybara to do) then there is a broken 'Log In' link on the page that points to app.localhost:3000/login which the app doesn't handle. This is because visiting a URL without the app subdomain uses the application.html.haml layout which includes no JS (and therefore no Vue router to handle the /login path). If however I go to app.localhost:3000 in the browser then there's a page with a working Log In link since all the JS is loaded on that page (including the Vue router) because it uses the app.html.haml layout (BaseController)
You need to fix the main homepage link to function correctly and/or configure Capybara to connect to the app subdomain by default
Capybara.app_host = 'http://app.localhost' # hostname with ‘app’ sub domain that resolves to interface the AUT is being run on
Capybara.always_include_port = true
Basically Capybaras telling you your apps main page is broken because it is broken.
I'm quite desperate since moving our test suite from Minitest to RSpec. All the controller and model tests run fine so far, but since trying to port (formerly passing/working) feature tests like the following I ran into trouble...
feature 'Create place' do
scenario 'create valid place as user' do
login_as_user
visit '/places/new'
fill_in_valid_place_information
click_button('Create Place')
visit '/places'
expect(page).to have_content('Any place', count: 1)
end
...
def fill_in_valid_place_information
fill_in('place_name', with: 'Any place')
fill_in('place_street', with: 'Magdalenenstr.')
fill_in('place_house_number', with: '19')
fill_in('place_postal_code', with: '10963')
fill_in('place_city', with: 'Berlin')
fill_in('place_email', with: 'schnipp#schnapp.com')
fill_in('place_homepage', with: 'http://schnapp.com')
fill_in('place_phone', with: '03081763253')
end
end
Unfortunately this does not lead to a DB commit which makes the test fail. It does not fail if i pry into the test and manually create the requested place. I tried different methods in order to trigger the button but nothing worked so far.
This is how my rails_helper.rb looks like:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/poltergeist'
require 'pry'
def validate_captcha
fill_in 'captcha', with: SimpleCaptcha::SimpleCaptchaData.first.value
end
def login_as_user
user = create :user, email: 'user#example.com'
visit 'login/'
fill_in 'sessions_email', with: 'user#example.com'
fill_in 'sessions_password', with: 'secret'
click_on 'Login'
end
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, phantomjs_options: ['--ignore-ssl-errors=true'])
end
Capybara.javascript_driver = :poltergeist
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, no_transaction: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.include Capybara::DSL
config.include Rails.application.routes.url_helpers
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Does anyone have a clue about a possible cause? Gem versions:
capybara 2.12.0
rspec 3.5.0
rails 4.2.7.1
best and thanks,
Andi
--- Update
I added fill_in_valid_place_information method
This is how the test fails with or without a Capybara JS driver enabled (shouldn't matter in case of this test as the feature does not use any JS). Unfortunately it doesn't give any real hints to work with...
1) Create place create valid place as user
Failure/Error: expect(page).to have_content('Any place', count: 1)
expected to find text "Any place" 1 time but found 0 times in "KIEZ KARTE Find places Here comes a list of all POIs currently available in our database. If you are looking for a specific location please enter parts of its descriptive features into the 'Search' field. Search: Name Postal code Categories No data available in table"
Timeout reached while running a *waiting* Capybara finder...perhaps you wanted to return immediately? Use a non-waiting Capybara finder. More info: http://blog.codeship.com/faster-rails-tests?utm_source=gem_exception
--- Update 2
I found the issue which is not capyara-related. Actually I forgot to transfer a stub response for an API we're calling. Thanks everybody for participating in my struggle!
There are a number of potential issues in your test that could be causing what you are seeing, it would be easier to narrow down in the future if you included the actual error message(s) your test produces.
Your scenarion/feature isn't tagged with :js metadata to activate using the Capybara driver. It's possible you've specified Capybara.default_driver somewhere, but if so then your DatabaseCleaner config is wrong
Use the recommended DatabaseCleaner configuration from https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example . The driver name detection will work if you have specified Capybara.default_driver as mentioned in #1 and also with the :js/:driver metadata usage pattern. Additionally, the append_after/after difference is important to reduce test flakiness
Your login_as_user method needs to verify the login has completed before returning. This is because click_on 'Login' can trigger asynchronously and return before the login actually occurs. This leads to the visit you call immediately following aborting the login, preventing the session cookie from being sent, and ending up with a non logged in user when you expected the user to be logged in. To fix this you need something like
def login_as_user
...
click_on 'Login'
expect(page).to have_text('You are now logged in!') #whatever message is shown on successful login, or use have_css with some element on the page that only exists when a user is logged in (user menu, etc)
end
The same issue exists between click_button('Create Place') and visit '/places' where the visit can effectively cancel the effects of the button click
I have a question with RSpec testing. I am using FactoryGirl, Capybara 2.* and trying to testing my website behavior.
Scenario of testing:
User clicking on sign_in button (devise controller), he is redirected to root_path (managed by MyController). After his redirection before_filter :setup_params should assign to #app variable (in action :find_apps in MyController) some values. I would like to ensure that #app is not nil and that values were assigned.
here is my sign_in_spec.rb
require "spec_helper.rb"
require "mymodel.rb"
describe MyContoller, :type => :feature do
before do
visit '/users/sign_in'
end
it "Shall redirect to user and ensure that #app is not nil" do
user = FactoryGirl.build(:user_monit)
fill_in "user[email]", with: user.email
fill_in "user[password]", with: user.password
#expect {click_button "Sign in"}.to change {#myapp}.from(nil)
click_button "Sign in"
get :find_apps
assigns(:myapp).should_not be_nil
end
end
require section in my spec_helper.rb
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'
require 'mocha/setup'
require 'factory_girl'
....
config.include RSpec::Rails::RequestExampleGroup, type: :feature
I have got several mistakes:
1) result should have changed, but is still nil
2) bad argument error for get :find_apps (ArgumentError: bad argument (expected URI object or URI string))
What am I doing wrong?
Thanks in advance!
You have to create User before you can login so :
user = FactoryGirl.create(:user_monit)
second issue you can resolve adding normal uri instead :find_apps the same like you make in before block
I am trying to test my rails app with rspec 2.10.0 + capybara 1.1.2. Here is my test file
require 'spec_helper'
describe AdminPanelController do
describe "index" do
it "should have return code 200" do
visit '/admin'
page.should have_content "hello"
#response.status.should be(200)
end
end
end
And here are test result
Failure/Error: page.should have_content "hello"
Capybara::ElementNotFound:
Unable to find xpath "/html"
I google about this issue but find only information that webrat can be a problem however i do not have this gem installed. Thanks for any suggestions.
Wrong type of test. This looks like a controller test, which does tests with methods like get and post and is in the spec/controllers folder. Request specs, which use capybara, reside in spec/requests. Run $ rails generate scaffold SomeModel to see how they each should look.
If you understood the above but would still like to use capybara for your controller test, modify your describe block:
describe AdminPanelController, :type => :request do
...
end