Reputation: 1473
I am new to Capybara and was hoping someone could help me with a problem I am having.
As part of my testing I have a number of different test spec files. Currently at the top of each spec file I create a new Capybara session with the following: session = Capybara::Session.new(:selenium).
This works fine when there is just 1 spec file to run, but I find when I attempt to run a number of different spec file tests together (contained in a folder) it creates a new instance for each first and I end up with loads of different browser windows. Is there a way I can just have this in 1 place and not in each test spec file?
I have previous experience with cucumber and watir and what you could do with that was create your browser instance in your env file. And you could also have your teardown here too. Is there an equivalent in rspec/capybara? I have tried putting 'session = Capybara::Session.new(:selenium)' line into my spec helper file, but when I try to run the tests I get 'undefined local variable or method `session' even though I have require 'spec_helper' in each test spec.
Any help with this would be great!! If any of the above is not clear please let me know and I will do my best to clarify.
Upvotes: 1
Views: 2548
Reputation: 46846
Scott Alister described how to do this for watir and rspec on his blog. You could adapt the same for capybara and rspec.
Try:
session = Capybara::Session.new(:selenium)
RSpec.configure do |config|
config.before(:each) { @session = session }
end
Note that your tests would refer to the session using the @session
variable.
Upvotes: 3