Leslie Chong
Leslie Chong

Reputation: 1345

Starting a new capybara session for every test

I am trying to start a new Capybara session for every one of our rspec tests and am unable to figure out how to properly quit/close the session on completion.

Here is my spec_helper.rb file.

RSpec.configure do |config|
  config.include Capybara::DSL

  config.before :each do
      @session = Capybara::Session.new(:selenium)
  end

 config.after :each do
     @session.driver.browser.quit
 end

end

The @session.driver.browser.quit statement properly closes the browser but for the last test that gets run I get an error message:

/Users/lpc/.rvm/gems/ruby-1.9.3-p448@capybara/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/common/file_reaper.rb:32:in `reap': file not added for reaping: "/var/folders/5l/kw4vv8bj7rvc4xv6yfyspkwh0000gn/T/webdriver-profile20131107-96496-cx4x5r" (Selenium::WebDriver::Error::WebDriverError)
from /Users/lpc/.rvm/gems/ruby-1.9.3-p448@capybara/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/firefox/launcher.rb:45:in `quit'
from /Users/lpc/.rvm/gems/ruby-1.9.3-p448@capybara/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/firefox/bridge.rb:58:in `ensure in quit'
from /Users/lpc/.rvm/gems/ruby-1.9.3-p448@capybara/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/firefox/bridge.rb:58:in `quit'
from /Users/lpc/.rvm/gems/ruby-1.9.3-p448@capybara/gems/selenium-webdriver-2.37.0/lib/selenium/webdriver/common/driver.rb:168:in `quit'
from /Users/lpc/.rvm/gems/ruby-1.9.3-p448@capybara/gems/capybara-2.1.0/lib/capybara/selenium/driver.rb:140:in `quit'
from /Users/lpc/.rvm/gems/ruby-1.9.3-p448@capybara/gems/capybara-2.1.0/lib/capybara/selenium/driver.rb:17:in `block in browser'

I believe the problem is because Capybara is also quitting the browser at the conclusion of all the tests. Does anyone know the proper way to do this and how I can silence this error message? Thanks for any help.

====UPDATE=====

To close the loop on this, I posted to Capybara google group with my issue was fixed and merged into master.

https://groups.google.com/forum/#!topic/ruby-capybara/tZi2F306Fvo

Upvotes: 8

Views: 14923

Answers (3)

ilgam
ilgam

Reputation: 4420

Try it with Capybara.reset_session!.

feature "my test" do    
  after { Capybara.reset_sessions! }
  
  scenario "my scenario" do
    # your assertions
  end
end

Upvotes: 8

ibaralf
ibaralf

Reputation: 12518

Old question, but I just encountered this problem. If you have a Capybara::Session, the browser stays open after each SPEC resulting in multiple browsers when you are running several SPEC files. There is no Capybara:Session quit method, so you need to get the driver object first then quit. I typically add this at the end of each SPEC file:

after :all do
  @capybara_session.driver.quit()
end

where @capybara_session is a Capybara::Session object.

Upvotes: 1

Shepmaster
Shepmaster

Reputation: 430466

Maybe there is a sauce method that you can use to clip the recording, and call that in your after instead?

Instead of quitting after each test, perhaps you could quit before each one? If you get an error trying to exit before one is running, perhaps there is a way to see if one is already running? If not, you could do a horrible, nasty hack: set a global variable in an after that lets you know there is a driver that you can quit.

Upvotes: 0

Related Questions