With Capybara, how do I switch to the new window for links with "_blank" targets?

Ruby on-RailsTestingSeleniumWebkitCapybara

Ruby on-Rails Problem Overview


Perhaps this isn't actually the issue I'm experiencing, but it seems that when I "click_link" a link with target="_blank", the session keeps the focus on the current window.

So I either want to be able to switch to the new window, or to ignore the _blank attribute - essentially, I just want it to actually go to the page indicated by the link so I can make sure it's the right page.

I use the webkit and selenium drivers.


I submitted my findings thus far below. A more thorough answer is much appreciated.

Also, this only works with selenium - the equivalent for the webkit driver (or pointing out where I could discover it myself) would be much appreciated.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Capybara >= 2.3 includes the new window management API. It can be used like:

new_window = window_opened_by { click_link 'Something' }
within_window new_window do
  # code
end

Solution 2 - Ruby on-Rails

This solution only works for the Selenium driver

All open windows are stores in Selenium's

response.driver.browser.window_handles

Which seems to be an array. The last item is always the window that was most recently opened, meaning you can do the following to switch to it.

Within a block:

new_window=page.driver.browser.window_handles.last 
page.within_window new_window do
  #code
end

Simply refocus for current session:

session.driver.browser.switch_to.window(page.driver.browser.window_handles.last)

Referenced on the capybara issues page: https://github.com/jnicklas/capybara/issues/173

More details on Selenium's window switching capabilities: http://qastuffs.blogspot.com/2010/10/testing-pop-up-windows-using-selenium.html

Solution 3 - Ruby on-Rails

This is now working with Poltergeist. Although window_handles is still not implemented (you need a window name, i.e. via a JavaScript popup):

within_window 'other_window' do
  current_url.should match /example.com/
end

Edit: Per comment below, Poltergeist now implements window_handles since version 1.4.0.

Solution 4 - Ruby on-Rails

Capybara provides some methods to ease finding and switching windows:

facebook_window = window_opened_by do
  click_button 'Like'
end
within_window facebook_window do
  find('#login_email').set('[email protected]')
  find('#login_password').set('qwerty')
  click_button 'Submit'
end

More details here: Capybara documentation

Solution 5 - Ruby on-Rails

I know this is old post, but for what its worth in capybara 2.4.4

within_window(switch_to_window(windows.last)) do 
    # in my case assert redirected url from a prior click action
    expect(current_url).to eq(redirect['url'])
end

Solution 6 - Ruby on-Rails

Seems like it is not possible with capybara-webkit right now: https://github.com/thoughtbot/capybara-webkit/issues/271

:-(

At the same time https://github.com/thoughtbot/capybara-webkit/issues/129 claims it is possible to switch windows with within_window.

Also https://github.com/thoughtbot/capybara-webkit/issues/47 suggests that page.driver.browser.switch_to().window(page.driver.browser.window_handles.last) works. Ah well, on to code reading.

The code at https://github.com/thoughtbot/capybara-webkit/blob/master/lib/capybara/webkit/browser.rb at least has some references that suggest that the API that works for webdriver / firefox is also working for webkit.

Solution 7 - Ruby on-Rails

Now within_window implemented for capybara-webkit http://github.com/thoughtbot/capybara-webkit/pull/314 and here you can see how to use it http://github.com/mhoran/capybara-webkit-demo

Solution 8 - Ruby on-Rails

As of May 2014 the following code works on capybara-webkit

 within_window(page.driver.browser.window_handles.last) do
   expect(current_url).to eq('http://www.example.com/')
 end

Solution 9 - Ruby on-Rails

To explicitly change window, you use switch_to_window

  def terms_of_use
    terms_window = window_opened_by do
      click_link(@terms_link)
    end
    switch_to_window(terms_window)
  end

An after that method browser will work in the new page, instead of wrap everything in a within_window block

Solution 10 - Ruby on-Rails

This works for me in capybara-webkit:

within_window(windows.last) do
  # code here
end

(I'm using capybara 2.4.1 and capybara-webkit 1.3.0)

Solution 11 - Ruby on-Rails

You can pass a name, url or title of the window also (But now its dipricated)

  let(:product) { create :product }

  it 'tests' do
    visit products_path
    click_link(product.id)

    within_window(product_path(product)) do
      expect(page).to have_content(product.title)
    end
  end

You can pass a labda or a proc also

within_window(->{ page.title == 'Page title' }) do
  click_button 'Submit'
end

wish it stretches the method usage to more clearly understaing

Solution 12 - Ruby on-Rails

I had this issue when opening links in an gmail window: I fixed it like this:

Given /^(?:|I )click the "([^"]*)" link in email message$/ do |field|

  # var alllinks = document.getElementsByTagName("a");
  # for (alllinksi=0; alllinksi<alllinks.length; alllinksi++) {
  #   alllinks[alllinksi].removeAttribute("target");
  # }

  page.execute_script('var alllinks = document.getElementsByTagName("a"); for (alllinksi=0; alllinksi<alllinks.length; alllinksi++) { alllinks[alllinksi].removeAttribute("target"); }')

  within(:css, "div.msg") do
    click_link link_text
  end

end

Solution 13 - Ruby on-Rails

The best idea is to update capybara to the latests version (2.4.1) and just use windows.last because page.driver.browser.window_handles is deprecated.

Solution 14 - Ruby on-Rails

The main implementation (window_opened_by) raises an error for me:

*** Capybara::WindowError Exception: block passed to #window_opened_by opened 0 windows instead of 1

So, I resolve it by this solution:

new_window = open_new_window

within_window new_window do
  visit(click_link 'Something')
end

page.driver.browser.window_handles
# => ["CDwindow-F7EF6D3C12B68D6B6A3DFC69C2790718", "CDwindow-9A026DEC65C3C031AF7D2BA12F28ADC7"]

Solution 15 - Ruby on-Rails

I personally like the following approach since it works correctly regardless of JS being enabled or not.

My Spec:

  it "shows as expected" do
    visit my_path

    # ...test my non-JS stuff in the current tab

    switch_to_new_tab

    # ...test my non-JS stuff in the new tab
    
    # ...keep switching to new tabs as much as necessary
  end 

  # OR

  it "shows as expected", js: true do
    visit my_path

    # ...test my non-JS stuff in the current tab
    # ...also test my JS stuff in the current tab

    switch_to_new_tab

    # ...test my non-JS stuff in the new tab
    # ...also test my JS stuff in the new tab

    # ...keep switching to new tabs as much as necessary
  end 

Test helpers:

  def switch_to_new_tab
    current_browser = page.driver.browser

    if js_enabled?
      current_browser.switch_to.window(current_browser.window_handles.last)
    else
      visit current_browser.last_request.fullpath
    end
  end

  def js_enabled?
    Capybara.current_driver == Capybara.javascript_driver
  end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlyphGryphView Question on Stackoverflow
Solution 1 - Ruby on-RailsAndrei BotalovView Answer on Stackoverflow
Solution 2 - Ruby on-RailsGlyphGryphView Answer on Stackoverflow
Solution 3 - Ruby on-RailsKridsada ThanabulpongView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAravinView Answer on Stackoverflow
Solution 5 - Ruby on-RailsbcarView Answer on Stackoverflow
Solution 6 - Ruby on-Railsuser132837View Answer on Stackoverflow
Solution 7 - Ruby on-RailsE-HaulerView Answer on Stackoverflow
Solution 8 - Ruby on-RailsthekindofmeView Answer on Stackoverflow
Solution 9 - Ruby on-RailsG. I. JoeView Answer on Stackoverflow
Solution 10 - Ruby on-RailslavaturtleView Answer on Stackoverflow
Solution 11 - Ruby on-RailsitsnikolayView Answer on Stackoverflow
Solution 12 - Ruby on-RailscomplisticView Answer on Stackoverflow
Solution 13 - Ruby on-RailsAdam PiotrowskiView Answer on Stackoverflow
Solution 14 - Ruby on-RailsAleksei StrizhakView Answer on Stackoverflow
Solution 15 - Ruby on-RailsAndres LeonView Answer on Stackoverflow