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
Share:
10,957

Related videos on Youtube

Brand
Author by

Brand

Updated on June 11, 2022

Comments

  • Brand
    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 is register_user which looks like this

    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
    

    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
    cman77 over 11 years
    When do you use spec/support vs. spec/helpers?
  • just__matt
    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
    Admin almost 11 years
    Aren't all the files inside spec/support loaded automatically?
  • Nathan Bertram
    Nathan Bertram over 10 years
    Same here ... that's if you have your test in spec/features vs. spec/requests
  • Andre Schweighofer
    Andre Schweighofer over 9 years
    @Gerep spec/support is loaded automatically. But that doesn't make the helper available to RSpec automatically.
  • Nakilon
    Nakilon over 5 years
    That's an adding a helper to RSpec, not Capybara.
  • Nakilon
    Nakilon over 5 years
    Alright, 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 as find.
  • itsnikolay
    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
    Nakilon over 5 years
    RSpec's include and exclude -- 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
    Nakilon over 5 years
    Oh, I'm wrong here. Inside the describe the include 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
    Connor Shea about 5 years
    spec/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 }