Where/how to include helper methods for capybara integration tests
10,957
Solution 1
Put your helper to the spec/support folder and do something like this:
spec/support/:
module YourHelper
def register_user(user)
visit home_page
fill_in 'user_name', :with => user.username
fill_in 'password', :with => user.password
click_button 'sign_up_button'
end
end
RSpec.configure do |config|
config.include YourHelper, :type => :request
end
Solution 2
I used the given solution by @VasiliyErmolovich, but I changed the type to make it work:
config.include YourHelper, :type => :feature
Related videos on Youtube
Author by
Brand
Updated on June 11, 2022Comments
-
Brand almost 2 years
I"m using capybara for my integration/acceptance tests. They're in
/spec/requests/
folder. Now I have a few helper methods that I use during acceptance tests. One example isregister_user
which looks like thisdef register_user(user) visit home_page fill_in 'user_name', :with => user.username fill_in 'password', :with => user.password click_button 'sign_up_button' end
I want to use this method in several different acceptance tests (they're in different files). What's the best way to include this? I've tried putting it in
spec/support/
but it hasn't been working for me. After spending some time on it I realized I don't even know if it's a good way of doing it so I figured I'd ask here.Note: I am using rails 3, spork and rspec.
-
cman77 over 11 yearsWhen do you use spec/support vs. spec/helpers?
-
just__matt almost 11 years@cman77 spec/helpers is for testing app/helpers, spec/support files are for modules and configuration that you want to use in you specs
-
Admin almost 11 yearsAren't all the files inside spec/support loaded automatically?
-
Nathan Bertram over 10 yearsSame here ... that's if you have your test in spec/features vs. spec/requests
-
Andre Schweighofer over 9 years@Gerep
spec/support
is loaded automatically. But that doesn't make the helper available to RSpec automatically. -
Nakilon over 5 yearsThat's an adding a helper to RSpec, not Capybara.
-
Nakilon over 5 yearsAlright, this isolates the helper from other tests. But what if it's a Capybara/Selenium-specific helper that is commonly needed? Like some wrapper around JS that I'll need in all my test files. And that I don't want to call it via
MyHelpers::my_js_helper
but I want it to be as easily accessible asfind
. -
itsnikolay over 5 years@Nakilon it's plain ruby, you can refactor your specs/code/DSL/capybara/selenium/whatever with it. Because everywhere
include
works like mixin.Find
method already exists in capybara. But it doesn't prevent to create another method e.g.content_find
using mixin approach. -
Nakilon over 5 yearsRSpec's
include
andexclude
-- while they look like plain Ruby they are in fact customly defined for a filtering ability and maybe even something more. While there is no "def include" in Capybara repo, maybe there is something else I don't know. -
Nakilon over 5 yearsOh, I'm wrong here. Inside the
describe
theinclude
method is plain Ruby. But the problem still remains that I'll need to put this line in every context I need, in dozens of files. -
Connor Shea about 5 yearsspec/support is no longer loaded automatically, there's a commented-out line in the
spec_helper.rb
that can be used to include the files in the support directory:Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }