6

I'm trying to open a link by clicking on it using capybara/poltergeist which opens up in a new tab.

I can't seem to get it working.

@session.find(<link>).click

just seems to stay on the same page, as does

click_link "<link>"

@session.driver.window_handles 

Only seems to give me 1 window whatever I'm doing.

How do I get that link to open in a new tab?

Seems to be a fair amount of confusion as to what works or doesn't a la (With Capybara, how do I switch to the new window for links with "_blank" targets?).

Hoping somebody has worked it out... Can't seem to get my head around it.

Ok. By updating to the latest version of poltergeist (1.6.0) we have some progress.

Now however I have two windows but no idea how to switch between them.

@session.windows

gives me

[Window @handle="0", Window @handle="1"] (slightly modified as it was going a bit funny).

But

@session.switch_to_window(1)

results in

NoMethodError: undefined method `handle' for "1":String
Community
  • 1
  • 1
Carpela
  • 2,155
  • 1
  • 24
  • 55

3 Answers3

6

Right, as of Feb 5th 2014 and v1.6 of poltergeist, you can do this, as ...

@session.click_on "link_with_target _blank"

@session.switch_to_window(@session.windows.last)
#Do whatever you're doing
@session.current_window.close
@session.switch_to_window(@session.windows.first)

Hope that clears things up for somebody...

Carpela
  • 2,155
  • 1
  • 24
  • 55
  • 1
    This is correct. `switch_to_window` wants a Window object as argument, not an index. That's why e.g. `windows.last` works. You can also do `@session.switch_to_window(@session.windows[1])`. You should accept your answer as correct! – henrebotha Apr 14 '15 at 14:22
3

I did this with

page.switch_to_window(page.windows[0])

Maybe that's useful to someone.

3

This is what worked for me using Capybara 3.33:

new_window = window_opened_by do
  click_link 'Go to external page'
end

within_window new_window do
  assert_current_path 'http://external-page.com/?q=lala'
end 
Américo Duarte
  • 525
  • 3
  • 16