Method: Capybara::Session#window_opened_by

Defined in:
lib/capybara/session.rb

#window_opened_by(**options, &block) ⇒ Capybara::Window

Get the window that has been opened by the passed block. It will wait for it to be opened (in the same way as other Capybara methods wait). It's better to use this method than windows.last as order of windows isn't defined in some drivers.

Returns the window that has been opened within a block.

Parameters:

  • options (Hash)

Options Hash (**options):

Returns:

Raises:

  • (Capybara::WindowError)

    if block passed to window hasn't opened window or opened more than one window



581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/capybara/session.rb', line 581

def window_opened_by(**options)
  old_handles = driver.window_handles
  yield

  synchronize_windows(options) do
    opened_handles = (driver.window_handles - old_handles)
    if opened_handles.size != 1
      raise Capybara::WindowError, 'block passed to #window_opened_by ' \
                                   "opened #{opened_handles.size} windows instead of 1"
    end
    Window.new(self, opened_handles.first)
  end
end