Class: Ferrum::Dialog

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, params) ⇒ Dialog

Returns a new instance of Dialog.



7
8
9
10
11
# File 'lib/ferrum/dialog.rb', line 7

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.



5
6
7
# File 'lib/ferrum/dialog.rb', line 5

def default_prompt
  @default_prompt
end

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/ferrum/dialog.rb', line 5

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)


29
30
31
32
33
34
# File 'lib/ferrum/dialog.rb', line 29

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")


50
51
52
# File 'lib/ferrum/dialog.rb', line 50

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

#match?(regexp) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ferrum/dialog.rb', line 54

def match?(regexp)
  !!message.match(regexp)
end