12 June 2014

In Capybara when you need to test a page that was opened by clicking a link with target="_blank", all you need to do is to write:

within_window(page.driver.browser.get_window_handles.last) do
    ...
    some_code_here
    ...
  end

Note! Beyond that code you’ll be at the page where you clicked that link.

update

The test will pass, but you will receive this message:

DEPRECATION WARNING: Passing string argument to #within_window is deprecated. Pass window object or lambda.

So now we should transform that code into:

within_window(windows.last) do
    ...
    some_code_here
    ...
  end

and everything should be fine :)