Class: Ferrum::Page

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Frames, Screenshot
Defined in:
lib/ferrum/page.rb,
lib/ferrum/page/frames.rb,
lib/ferrum/page/screenshot.rb

Defined Under Namespace

Modules: Frames, Screenshot Classes: Event

Constant Summary

Constants included from Screenshot

Screenshot::DEFAULT_PDF_OPTIONS, Screenshot::PAPEP_FORMATS

Instance Attribute Summary collapse

Attributes included from Frames

#main_frame

Instance Method Summary collapse

Methods included from Frames

#frame_by, #frames, #frames_subscribe

Methods included from Screenshot

#document_size, #pdf, #screenshot, #viewport_size

Constructor Details

#initialize(target_id, browser) ⇒ Page

Returns a new instance of Page.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ferrum/page.rb', line 43

def initialize(target_id, browser)
  @frames = {}
  @target_id, @browser = target_id, browser
  @event = Event.new.tap(&:set)

  host = @browser.process.host
  port = @browser.process.port
  ws_url = "ws://#{host}:#{port}/devtools/page/#{@target_id}"
  @client = Browser::Client.new(browser, ws_url, 1000)

  @mouse, @keyboard = Mouse.new(self), Keyboard.new(self)
  @headers, @cookies = Headers.new(self), Cookies.new(self)
  @network = Network.new(self)

  subscribe
  prepare_page
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def browser
  @browser
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def cookies
  @cookies
end

#document_idObject (readonly)

Returns the value of attribute document_id.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def document_id
  @document_id
end

#eventObject (readonly)

Returns the value of attribute event.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def event
  @event
end

#headersObject (readonly)

Returns the value of attribute headers.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def headers
  @headers
end

#keyboardObject (readonly)

Returns the value of attribute keyboard.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def keyboard
  @keyboard
end

#mouseObject (readonly)

Returns the value of attribute mouse.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def mouse
  @mouse
end

#networkObject (readonly)

Returns the value of attribute network.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def network
  @network
end

#referrerObject

Returns the value of attribute referrer.



38
39
40
# File 'lib/ferrum/page.rb', line 38

def referrer
  @referrer
end

#target_idObject (readonly)

Returns the value of attribute target_id.



39
40
41
# File 'lib/ferrum/page.rb', line 39

def target_id
  @target_id
end

Instance Method Details

#backObject



108
109
110
# File 'lib/ferrum/page.rb', line 108

def back
  history_navigate(delta: -1)
end

#closeObject



79
80
81
82
83
# File 'lib/ferrum/page.rb', line 79

def close
  @headers.clear
  @browser.command("Target.closeTarget", targetId: @target_id)
  @client.close
end

#command(method, wait: 0, **params) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/ferrum/page.rb', line 116

def command(method, wait: 0, **params)
  iteration = @event.reset if wait > 0
  result = @client.command(method, params)
  if wait > 0
    @event.wait(wait)
    @event.wait(@browser.timeout) if iteration != @event.iteration
  end
  result
end

#forwardObject



112
113
114
# File 'lib/ferrum/page.rb', line 112

def forward
  history_navigate(delta: 1)
end

#goto(url = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ferrum/page.rb', line 65

def goto(url = nil)
  options = { url: combine_url!(url) }
  options.merge!(referrer: referrer) if referrer
  response = command("Page.navigate", wait: timeout, **options)
  # https://cs.chromium.org/chromium/src/net/base/net_error_list.h
  if %w[net::ERR_NAME_NOT_RESOLVED
        net::ERR_NAME_RESOLUTION_FAILED
        net::ERR_INTERNET_DISCONNECTED
        net::ERR_CONNECTION_TIMED_OUT].include?(response["errorText"])
    raise StatusError, options[:url]
  end
  response["frameId"]
end

#on(name, &block) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ferrum/page.rb', line 126

def on(name, &block)
  case name
  when :dialog
    @client.on("Page.javascriptDialogOpening") do |params, index, total|
      dialog = Dialog.new(self, params)
      block.call(dialog, index, total)
    end
  when :request
    @client.on("Fetch.requestPaused") do |params, index, total|
      request = Network::InterceptedRequest.new(self, params)
      block.call(request, index, total)
    end
  when :auth
    @client.on("Fetch.authRequired") do |params, index, total|
      request = Network::AuthRequest.new(self, params)
      block.call(request, index, total)
    end
  else
    @client.on(name, &block)
  end
end

#refreshObject



104
105
106
# File 'lib/ferrum/page.rb', line 104

def refresh
  command("Page.reload", wait: timeout)
end

#resize(width: nil, height: nil, fullscreen: false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ferrum/page.rb', line 85

def resize(width: nil, height: nil, fullscreen: false)
  result = @browser.command("Browser.getWindowForTarget", targetId: @target_id)
  @window_id, @bounds = result.values_at("windowId", "bounds")

  if fullscreen
    width, height = document_size
    @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "fullscreen" })
  else
    @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { windowState: "normal" })
    @browser.command("Browser.setWindowBounds", windowId: @window_id, bounds: { width: width, height: height, windowState: "normal" })
  end

  command("Emulation.setDeviceMetricsOverride", width: width,
                                                height: height,
                                                deviceScaleFactor: 1,
                                                mobile: false,
                                                fitWindow: false)
end

#timeoutObject



61
62
63
# File 'lib/ferrum/page.rb', line 61

def timeout
  @browser.timeout
end