Class: Ferrum::Dialog

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/dialog.rb

Overview

Represents a JavaScript dialog (alert, confirm, prompt or beforeunload) shown by the page. Instances are yielded to blocks registered with page.on(:dialog), and can be accepted (optionally with a prompt's text) or dismissed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, params) ⇒ Dialog

Returns a new instance of Dialog.



13
14
15
16
17
# File 'lib/ferrum/dialog.rb', line 13

def initialize(page, params)
  @page = page
  @message = params["message"]
  @default_prompt = params["defaultPrompt"]
end

Instance Attribute Details

#default_promptObject (readonly)

Returns the value of attribute default_prompt.



11
12
13
# File 'lib/ferrum/dialog.rb', line 11

def default_prompt
  @default_prompt
end

#messageObject (readonly)

Returns the value of attribute message.



11
12
13
# File 'lib/ferrum/dialog.rb', line 11

def message
  @message
end

Instance Method Details

#accept(prompt_text = nil) ⇒ Object

Accept dialog with given text or default prompt if applicable

Examples:

browser = Ferrum::Browser.new
browser.on(:dialog) do |dialog|
  if dialog.match?(/bla-bla/)
    dialog.accept
  else
    dialog.dismiss
  end
end
browser.go_to("https://google.com")

Parameters:

  • prompt_text (String, nil) (defaults to: nil)


35
36
37
38
39
40
# File 'lib/ferrum/dialog.rb', line 35

def accept(prompt_text = nil)
  options = { accept: true }
  response = prompt_text || default_prompt
  options.merge!(promptText: response) if response
  @page.command("Page.handleJavaScriptDialog", slowmoable: true, **options)
end

#dismissObject

Dismiss dialog.

Examples:

browser = Ferrum::Browser.new
browser.on(:dialog) do |dialog|
  if dialog.match?(/bla-bla/)
    dialog.accept
  else
    dialog.dismiss
  end
end
browser.go_to("https://google.com")


56
57
58
# File 'lib/ferrum/dialog.rb', line 56

def dismiss
  @page.command("Page.handleJavaScriptDialog", slowmoable: true, accept: false)
end

#match?(pattern) ⇒ Boolean

Whether the dialog's message matches the given pattern.

Parameters:

  • pattern (Regexp, String)

    A regexp matched against the message, or a string checked for inclusion in it.

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/ferrum/dialog.rb', line 68

def match?(pattern)
  return message.match?(pattern) if pattern.is_a?(Regexp)

  message.include?(pattern.to_s)
end