Class: Ferrum::Console

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ferrum/console.rb,
lib/ferrum/console/message.rb,
lib/ferrum/console/location.rb,
sig/ferrum/console.rbs,
sig/ferrum/console/message.rbs,
sig/ferrum/console/location.rbs

Overview

Collects what the DevTools console would show for a page or worker: console.* calls, browser log entries and uncaught exceptions, as Messages. Keeps the last LIMIT messages and is Enumerable over them, oldest first.

Examples:

browser.go_to("https://example.com")
browser.console.select(&:error?) # => [#<Ferrum::Console::Message type=:error ...>]
browser.console.clear

Defined Under Namespace

Classes: Location, Message

Constant Summary collapse

LIMIT =

Returns:

  • (::Integer)
1000

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Console

Returns a new instance of Console.

Parameters:



22
23
24
25
26
27
28
# File 'lib/ferrum/console.rb', line 22

def initialize(page)
  @page = page
  @messages = []
  @listeners = Concurrent::Map.new
  @listener_id = Concurrent::AtomicFixnum.new
  @mutex = Mutex.new
end

Instance Method Details

#clear ⇒ nil

Forgets the collected messages.

Returns:

  • (nil)


85
86
87
88
# File 'lib/ferrum/console.rb', line 85

def clear
  @mutex.synchronize { @messages.clear }
  nil
end

#each ⇒ self #each ⇒ ::Enumerator[Console::Message, self]

Yields each collected message, oldest first.

Overloads:

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (Enumerator, self)


37
38
39
40
41
42
# File 'lib/ferrum/console.rb', line 37

def each(&)
  return to_enum(:each) { size } unless block_given?

  to_a.each(&)
  self
end

#empty? ⇒ Boolean

Whether no messages were collected.

Returns:

  • (Boolean)


76
77
78
# File 'lib/ferrum/console.rb', line 76

def empty?
  size.zero?
end

#inspect ⇒ String

Debug representation of the console.

Returns:

  • (String)


139
140
141
# File 'lib/ferrum/console.rb', line 139

def inspect
  "#<#{self.class.name} size=#{size}>"
end

#last ⇒ Message?

The most recent message.

Returns:



58
59
60
# File 'lib/ferrum/console.rb', line 58

def last
  @mutex.synchronize { @messages.last }
end

#off(id) ⇒ void

This method returns an undefined value.

Removes a listener added via #on.

Parameters:

  • id (Integer)


109
110
111
# File 'lib/ferrum/console.rb', line 109

def off(id)
  @listeners.delete(id)
end

#on {|message, arg0| ... } ⇒ Integer

Calls the block for every new message. Prefer page.on(:console).

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (Integer) —

    The listener id, used to remove it via #off.



98
99
100
# File 'lib/ferrum/console.rb', line 98

def on(&block)
  @listener_id.increment.tap { |id| @listeners[id] = block }
end

#size ⇒ Integer

Number of collected messages.

Returns:

  • (Integer)


67
68
69
# File 'lib/ferrum/console.rb', line 67

def size
  @mutex.synchronize { @messages.size }
end

#subscribe ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Subscribes to the CDP events messages are built from.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ferrum/console.rb', line 119

def subscribe
  @page.on("Runtime.consoleAPICalled") do |params|
    add(Message.from_console_api(params)) unless replayed?(params)
  end

  @page.on("Log.entryAdded") do |params|
    entry = params["entry"]
    add(Message.from_log_entry(entry)) unless entry["source"] == "worker"
  end

  @page.on("Runtime.exceptionThrown") do |params|
    add(Message.from_exception(params))
  end
end

#to_a ⇒ Array<Message>

The collected messages, oldest first.

Returns:



49
50
51
# File 'lib/ferrum/console.rb', line 49

def to_a
  @mutex.synchronize { @messages.dup }
end