With Capybara, how can I go to a new window for links with "_blank" goals?

This may not be the problem I encountered, but it seems that when I click_link refer to target = "_ blank", the session focuses on the current window.

Thus, I either want to be able to switch to a new window, or to ignore the _blank attribute - essentially, I just want it to really go to the page indicated by the link so that I can make sure that this is the right page.

I use webkit and selenium drivers.




I have presented my findings so far below. A more thorough answer is much appreciated.

In addition, this only works with selenium - the equivalent for the webkit driver (or specifying where I could find it myself) would be very grateful.

+64
ruby-on-rails selenium testing webkit capybara
Sep 30 2018-11-21T00:
source share
13 answers

Capybara> = 2.3 includes a new window management API. It can be used as:

new_window = window_opened_by { click_link 'Something' } within_window new_window do # code end 
+56
Aug 02 '14 at 9:17
source share

This solution only works for the Selenium driver.

All open windows are shops at Selenium's

 response.driver.browser.window_handles 

It seems to be an array. The last element is always the window that was recently opened, which means that you can do the following to switch to it.

Inside the block:

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

Just refocus on the current session:

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

Link to capybara problems page: https://github.com/jnicklas/capybara/issues/173

More information about the options for switching the Selenium window: http://qastuffs.blogspot.com/2010/10/testing-pop-up-windows-using-selenium.html

+51
Oct 03 '11 at 15:30
source share

Now it works with Poltergeist. Although window_handles is still not implemented (you need a window name, i.e. through a JavaScript popup):

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



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

+8
Mar 05 '13 at
source share

Capybara provides some ways to make it easier to find and switch windows:

 facebook_window = window_opened_by do click_button 'Like' end within_window facebook_window do find('#login_email').set('a@example.com') find('#login_password').set('qwerty') click_button 'Submit' end 

Read more here: Capybara Documentation

+7
Nov 24 '15 at 11:33
source share

It seems impossible 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 that you can switch windows using within_window .

Also https://github.com/thoughtbot/capybara-webkit/issues/47 assumes page.driver.browser.switch_to().window(page.driver.browser.window_handles.last) works. Ok, read the code.

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

+6
Sep 18 '12 at 10:32
source share

Now inside_window is 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

+5
Jan 13 '13 at 12:53 on
source share

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 
+5
May 08 '14 at 5:51
source share

I know this is an old post, but why is it worth it 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 
+5
Jan 27 '15 at 19:45
source share

This works for me in capybara-webkit:

 within_window(windows.last) do # code here end 

(I am using capybara 2.4.1 and capybara-webkit 1.3.0)

+3
Jan 01 '15 at 18:17
source share

I had this problem when opening links in a gmail window: I fixed it as follows:

 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 
+2
May 03 '13 at
source share

You can pass the name , url or title of the window. (But now his immersion)

  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 labda or proc also

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

we wish him to expand the use of the method for a clearer understanding

+2
Aug 31 '15 at 14:40
source share

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

+1
Feb 06 '15 at 2:31 on
source share

To explicitly change the 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 

After this browser browser will work on a new page, instead of wrapping everything in a within_window block

0
Dec 20 '16 at 19:18
source share



All Articles