3

I found a code: page.driver.browser.switch_to.window, which is apparently switching to already opened window. How do we open a new window using page.driver.browser object?

Thanks

user_1357
  • 7,766
  • 13
  • 63
  • 106

2 Answers2

9

I ended up here, even if is a old thread, might be useful for someone, you can create and use windows like this:

new_window = open_new_window
switch_to_window new_window

or:

within_window new_window do
end

you can also find new windows through windows object.

open_new_window
new_window = windows.last

References:

http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Session:open_new_window

http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FSession%3Aswitch_to_window

https://github.com/teamcapybara/capybara#working-with-windows

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

Juliano
  • 346
  • 3
  • 4
2

I ended up using JavaScript to do this:

When /^I open a new window for "([^"]*)"$/ do |url|
str =
<<END_TAG
     window.open("#{url}", "window_name", "height=800,width=1000");
END_TAG
  page.execute_script(str)
end
user_1357
  • 7,766
  • 13
  • 63
  • 106