Otake
Otake

Reputation: 31

Rspec - How can I close the current browser on system spec?

I use rspec 3.9, capybara 3.3 and selenium-webdriver 3.142 with chrome for test.
I'd like to ensure that browser retains "remember me" cookies after closing/reopening the browser.
But I cannot close the current browser.

current_window
=> #<Window @handle="CDwindow-EE538F0EA985E268B76B91E21375BBD2">

current_window.close
ArgumentError: Not allowed to close the primary window
from /Users/otake/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/capybara-3.30.0/lib/capybara/selenium/driver.rb:200:in `close_window'

Same error happens after input command "switch_to_window(open_new_window)" then "windows.first.close"

I'd like to know how to:
1. close the current browser
2. open new browser

Thank you

Upvotes: 2

Views: 685

Answers (1)

Nick M
Nick M

Reputation: 2532

You can have Capybara close the browser:

Capybara.current_session.quit

A new one will open automatically with the next feature spec. Not sure if your cookies will persist though.

But there are other ways to test cookies

https://relishapp.com/rspec/rspec-rails/docs/controller-specs/cookies

describe do
  specify do
    get "/"

    Timecop.travel(35.minutes.from_now) do
      get "/"

      cookie = get_cookie(cookies, "foo")
      expect(cookie.value).to eq("some value!")
      expect(cookie.expires).to be_present
    end
  end

  # That will be built-in in rack-test > 0.6.3
  def get_cookie(cookies, name)
    cookies.send(:hash_for, nil).fetch(name, nil)
  end
end

Upvotes: 1

Related Questions