开发者

Simulate closing/reopening the browser in cucumber/capybara?

I'm writing cucumber tests to test user 'Remember me' type functionality, and in order to do that in real life the user would close their browser开发者_开发问答, reopen their browser, and come back to the site.

My test so far looks like this:

Scenario: 'Remember me' checked
  Given I have checked "Remember me"
  And I am logged in as "test@test.com"
  When I close and re-open my browser
  And I come back to the dashboard
  Then I should be on the dashboard

However I don't know what to fill in for the 'When I close and re-open the browser' step definition.

Does anyone know how I would do this (or if this isn't what I should be doing, how I should be testing it?)


I use Show me the cookies.

Add to bundle with gem 'show_me_the_cookies' and then add World(ShowMeTheCookies) in your features/support/env.rb

Then just define a step:

When /^I reopen the browser$/ do
  expire_cookies
  visit [ current_path, page.driver.request.env['QUERY_STRING'] ].reject(&:blank?).join('?')
end


Perhaps create a second Capybara session? http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session


You can just clear the cookies.

When /^I clear cookies$/ do
  browser = Capybara.current_session.driver.browser
  browser.manage.delete_all_cookies
end


This gem makes it easy: https://github.com/nruth/show_me_the_cookies


I was trying to test the same thing, got it work like this:

When(/^I close and reopen the browser$/) do
  # Get cookies we want to keep
  remember_me_cookie = page.driver.browser.manage.cookie_named('remember_user_token')

  # Close the window and delete the cookies
  page.driver.quit

  # Reopen the window
  page.driver.switch_to_window(page.driver.current_window_handle)

  # Go to our domain and add our cookies back in
  visit('/') 
  remember_me_cookie.nil? ? @current_user = nil : page.driver.browser.manage.add_cookie(remember_me_cookie) 

  # Refresh the domain to activate the cookies
  visit('/') 
end

A few things:

  • page.driver.quit removes the cookies, thats why I am doing the dance with the cookies
  • I tried closing the window using page.driver.close_window(page.driver.current_window_handle), then switching back to it with page.driver.switch_to_window(handle) but that didn't work
  • I tried closing the window using page.driver.close_window(page.driver.current_window_handle), then switching to a new window page.driver.switch_to_window(page.driver.open_new_window) but that didn't work

I feel like I am not really understanding how the browser / window relationship is working in Capybara. Also it seems like copying over the cookies is kind of cheating my integration test. Hopefully there is a better way to achieve all of this in future versions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜