Class: Puppeteer::LifecycleWatcher

Inherits:
Object
  • Object
show all
Includes:
IfPresent
Defined in:
lib/puppeteer/lifecycle_watcher.rb

Overview

Defined Under Namespace

Classes: ExpectedLifecycle, FrameDetachedError, TerminatedError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Constructor Details

#initialize(frame_manager, frame, wait_until, timeout) ⇒ LifecycleWatcher

  • @param Puppeteer::LifecycleWatcher.!Puppeteer!Puppeteer.FrameManager frameManager

  • @param Puppeteer::LifecycleWatcher.!Puppeteer!Puppeteer.Frame frame

  • @param string|!Array<string> waitUntil

  • @param number timeout



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppeteer/lifecycle_watcher.rb', line 64

def initialize(frame_manager, frame, wait_until, timeout)
  @expected_lifecycle = ExpectedLifecycle.new(wait_until)
  @frame_manager = frame_manager
  @frame = frame
  @initial_loader_id = frame.loader_id
  @timeout = timeout

  @listener_ids = {}
  @listener_ids['client'] = @frame_manager.client.add_event_listener(CDPSessionEmittedEvents::Disconnected) do
    terminate(TerminatedError.new('Navigation failed because browser has disconnected!'))
  end
  @listener_ids['frame_manager'] = [
    @frame_manager.add_event_listener(FrameManagerEmittedEvents::LifecycleEvent) do |_|
      check_lifecycle_complete
    end,
    @frame_manager.add_event_listener(FrameManagerEmittedEvents::FrameNavigatedWithinDocument, &method(:navigated_within_document)),
    @frame_manager.add_event_listener(FrameManagerEmittedEvents::FrameDetached, &method(:handle_frame_detached)),
  ]
  @listener_ids['network_manager'] = @frame_manager.network_manager.add_event_listener(NetworkManagerEmittedEvents::Request, &method(:handle_request))

  @same_document_navigation_promise = resolvable_future
  @lifecycle_promise = resolvable_future
  @new_document_navigation_promise = resolvable_future
  @termination_promise = resolvable_future
  check_lifecycle_complete
end

Instance Attribute Details

#new_document_navigation_promiseObject (readonly)

Returns the value of attribute new_document_navigation_promise.



118
119
120
# File 'lib/puppeteer/lifecycle_watcher.rb', line 118

def new_document_navigation_promise
  @new_document_navigation_promise
end

#same_document_navigation_promiseObject (readonly)

Returns the value of attribute same_document_navigation_promise.



118
119
120
# File 'lib/puppeteer/lifecycle_watcher.rb', line 118

def same_document_navigation_promise
  @same_document_navigation_promise
end

Instance Method Details

#disposeObject



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/puppeteer/lifecycle_watcher.rb', line 160

def dispose
  if_present(@listener_ids['client']) do |id|
    @frame_manager.client.remove_event_listener(id)
  end
  if_present(@listener_ids['frame_manager']) do |ids|
    @frame_manager.remove_event_listener(*ids)
  end
  if_present(@listener_ids['network_manager']) do |id|
    @frame_manager.network_manager.remove_event_listener(id)
  end
end

#handle_frame_detached(frame) ⇒ Object

Parameters:



98
99
100
101
102
103
104
# File 'lib/puppeteer/lifecycle_watcher.rb', line 98

def handle_frame_detached(frame)
  if @frame == frame
    @termination_promise.reject(FrameDetachedError.new)
    return
  end
  check_lifecycle_complete
end

#handle_request(request) ⇒ Object

Parameters:



92
93
94
95
# File 'lib/puppeteer/lifecycle_watcher.rb', line 92

def handle_request(request)
  return if request.frame != @frame || !request.navigation_request?
  @navigation_request = request
end

Returns:



107
108
109
110
111
# File 'lib/puppeteer/lifecycle_watcher.rb', line 107

def navigation_response
  if_present(@navigation_request) do |request|
    request.response
  end
end

#timeout_or_termination_promiseObject



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/puppeteer/lifecycle_watcher.rb', line 124

def timeout_or_termination_promise
  if @timeout > 0
    future do
      Timeout.timeout(@timeout / 1000.0) do
        @termination_promise.value!
      end
    rescue Timeout::Error
      raise Puppeteer::TimeoutError.new("Navigation timeout of #{@timeout}ms exceeded")
    end
  else
    @termination_promise
  end
end