module Capybara::Playwright::PageExtension

Public Class Methods

new(*args, **kwargs) click to toggle source
Calls superclass method
# File lib/capybara/playwright/page.rb, line 6
def initialize(*args, **kwargs)
  if kwargs.empty?
    super(*args)
  else
    super(*args, **kwargs)
  end
  capybara_initialize
end

Public Instance Methods

capybara_accept_modal(dialog_type, **options, &block) click to toggle source
# File lib/capybara/playwright/page.rb, line 89
def capybara_accept_modal(dialog_type, **options, &block)
  timeout_sec = options[:wait]
  acceptor = DialogAcceptor.new(dialog_type, options)
  matcher = DialogMessageMatcher.new(options[:text])
  message_promise = Concurrent::Promises.resolvable_future
  handler = -> (dialog) {
    message = dialog.message
    if matcher.matches?(message)
      message_promise.fulfill(message)
      acceptor.handle(dialog)
    else
      message_promise.reject(Capybara::ModalNotFound.new("Dialog message=\"#{message}\" dowsn't match"))
      dialog.dismiss
    end
  }
  capybara_dialog_event_handler.with_handler(handler) do
    block.call

    message = message_promise.value!(timeout_sec)
    if message_promise.fulfilled?
      message
    else
      # timed out
      raise Capybara::ModalNotFound
    end
  end
end
capybara_current_frame() click to toggle source
# File lib/capybara/playwright/page.rb, line 178
def capybara_current_frame
  @capybara_frames.last || main_frame
end
capybara_dismiss_modal(dialog_type, **options, &block) click to toggle source
# File lib/capybara/playwright/page.rb, line 117
def capybara_dismiss_modal(dialog_type, **options, &block)
  timeout_sec = options[:wait]
  matcher = DialogMessageMatcher.new(options[:text])
  message_promise = Concurrent::Promises.resolvable_future
  handler = -> (dialog) {
    message = dialog.message
    if matcher.matches?(message)
      message_promise.fulfill(message)
    else
      message_promise.reject(Capybara::ModalNotFound.new("Dialog message=\"#{message}\" dowsn't match"))
    end
    dialog.dismiss
  }
  capybara_dialog_event_handler.with_handler(handler) do
    block.call

    message = message_promise.value!(timeout_sec)
    if message_promise.fulfilled?
      message
    else
      # timed out
      raise Capybara::ModalNotFound
    end
  end
end
capybara_pop_frame() click to toggle source
# File lib/capybara/playwright/page.rb, line 174
def capybara_pop_frame
  @capybara_frames.pop
end
capybara_push_frame(frame) click to toggle source

@param frame [Playwright::Frame]

# File lib/capybara/playwright/page.rb, line 170
def capybara_push_frame(frame)
  @capybara_frames << frame
end
capybara_reset_frames() click to toggle source
# File lib/capybara/playwright/page.rb, line 165
def capybara_reset_frames
  @capybara_frames.clear
end
capybara_response_headers() click to toggle source
# File lib/capybara/playwright/page.rb, line 151
def capybara_response_headers
  headers = @capybara_last_response&.headers || {}

  Headers.new.tap do |h|
    headers.each do |key, value|
      h[key] = value
    end
  end
end
capybara_status_code() click to toggle source
# File lib/capybara/playwright/page.rb, line 161
def capybara_status_code
  @capybara_last_response&.status.to_i
end

Private Instance Methods

capybara_dialog_event_handler() click to toggle source
# File lib/capybara/playwright/page.rb, line 38
        def capybara_dialog_event_handler
  @capybara_dialog_event_handler ||= DialogEventHandler.new.tap do |h|
    h.default_handler = method(:capybara_on_unexpected_modal)
  end
end
capybara_initialize() click to toggle source
# File lib/capybara/playwright/page.rb, line 15
        def capybara_initialize
  @capybara_all_responses = {}
  @capybara_last_response = nil
  @capybara_frames = []

  on('dialog', -> (dialog) {
    capybara_dialog_event_handler.handle_dialog(dialog)
  })
  on('download', -> (download) {
    FileUtils.mkdir_p(Capybara.save_path)
    dest = File.join(Capybara.save_path, download.suggested_filename)
    # download.save_as blocks main thread until download completes.
    Thread.new(dest) { |_dest| download.save_as(_dest) }
  })
  on('response', -> (response) {
    @capybara_all_responses[response.url] = response
  })
  on('framenavigated', -> (frame) {
    @capybara_last_response = @capybara_all_responses[frame.url]
    @capybara_all_responses.clear
  })
end
capybara_on_unexpected_modal(dialog) click to toggle source
# File lib/capybara/playwright/page.rb, line 44
        def capybara_on_unexpected_modal(dialog)
  puts "[WARNING] Unexpected modal - \"#{dialog.message}\""
  if dialog.type == 'beforeunload'
    dialog.accept_async
  else
    dialog.dismiss
  end
end