Class: Lightpanda::Browser
- Inherits:
-
Object
- Object
- Lightpanda::Browser
- Extended by:
- Forwardable
- Defined in:
- lib/lightpanda/browser.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#process ⇒ Object
readonly
Returns the value of attribute process.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#target_id ⇒ Object
readonly
Returns the value of attribute target_id.
Instance Method Summary collapse
- #at_css(selector) ⇒ Object
- #back ⇒ Object
- #body ⇒ Object (also: #html)
- #command(method, **params) ⇒ Object
- #cookies ⇒ Object
- #create_page ⇒ Object
- #css(selector) ⇒ Object
- #current_url ⇒ Object
- #enable_page_events ⇒ Object
- #evaluate(expression) ⇒ Object
- #execute(expression) ⇒ Object
- #forward ⇒ Object
- #go_to(url, wait: true) ⇒ Object (also: #goto)
-
#initialize(options = {}) ⇒ Browser
constructor
A new instance of Browser.
- #network ⇒ Object
- #page_command(method, **params) ⇒ Object
- #quit ⇒ Object
- #refresh ⇒ Object (also: #reload)
- #restart ⇒ Object
- #screenshot(path: nil, format: :png, quality: nil, full_page: false, encoding: :binary) ⇒ Object
- #start ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Browser
Returns a new instance of Browser.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lightpanda/browser.rb', line 13 def initialize( = {}) @options = Options.new() @process = nil @client = nil @target_id = nil @session_id = nil @started = false @page_events_enabled = false start end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/lightpanda/browser.rb', line 9 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/lightpanda/browser.rb', line 9 def @options end |
#process ⇒ Object (readonly)
Returns the value of attribute process.
9 10 11 |
# File 'lib/lightpanda/browser.rb', line 9 def process @process end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
9 10 11 |
# File 'lib/lightpanda/browser.rb', line 9 def session_id @session_id end |
#target_id ⇒ Object (readonly)
Returns the value of attribute target_id.
9 10 11 |
# File 'lib/lightpanda/browser.rb', line 9 def target_id @target_id end |
Instance Method Details
#at_css(selector) ⇒ Object
141 142 143 144 145 |
# File 'lib/lightpanda/browser.rb', line 141 def at_css(selector) result = page_command("DOM.querySelector", nodeId: document_node_id, selector: selector) result["nodeId"] end |
#back ⇒ Object
99 100 101 |
# File 'lib/lightpanda/browser.rb', line 99 def back page_command("Page.navigateToHistoryEntry", entryId: current_entry_id - 1) end |
#body ⇒ Object Also known as: html
120 121 122 |
# File 'lib/lightpanda/browser.rb', line 120 def body evaluate("document.documentElement.outerHTML") end |
#command(method, **params) ⇒ Object
62 63 64 |
# File 'lib/lightpanda/browser.rb', line 62 def command(method, **params) @client.command(method, params) end |
#cookies ⇒ Object
185 186 187 |
# File 'lib/lightpanda/browser.rb', line 185 def @cookies ||= Cookies.new(self) end |
#create_page ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/lightpanda/browser.rb', line 41 def create_page result = @client.command("Target.createTarget", { url: "about:blank" }) @target_id = result["targetId"] attach_result = @client.command("Target.attachToTarget", { targetId: @target_id, flatten: true }) @session_id = attach_result["sessionId"] end |
#css(selector) ⇒ Object
136 137 138 139 |
# File 'lib/lightpanda/browser.rb', line 136 def css(selector) node_ids = page_command("DOM.querySelectorAll", nodeId: document_node_id, selector: selector) node_ids["nodeIds"] || [] end |
#current_url ⇒ Object
112 113 114 |
# File 'lib/lightpanda/browser.rb', line 112 def current_url evaluate("window.location.href") end |
#enable_page_events ⇒ Object
92 93 94 95 96 97 |
# File 'lib/lightpanda/browser.rb', line 92 def enable_page_events return if @page_events_enabled page_command("Page.enable") @page_events_enabled = true end |
#evaluate(expression) ⇒ Object
125 126 127 128 129 |
# File 'lib/lightpanda/browser.rb', line 125 def evaluate(expression) response = page_command("Runtime.evaluate", expression: expression, returnByValue: true, awaitPromise: true) handle_evaluate_response(response) end |
#execute(expression) ⇒ Object
131 132 133 134 |
# File 'lib/lightpanda/browser.rb', line 131 def execute(expression) page_command("Runtime.evaluate", expression: expression, returnByValue: false, awaitPromise: false) nil end |
#forward ⇒ Object
103 104 105 |
# File 'lib/lightpanda/browser.rb', line 103 def forward page_command("Page.navigateToHistoryEntry", entryId: current_entry_id + 1) end |
#go_to(url, wait: true) ⇒ Object Also known as: goto
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/lightpanda/browser.rb', line 70 def go_to(url, wait: true) enable_page_events if wait loaded = Concurrent::Event.new handler = proc { loaded.set } @client.on("Page.loadEventFired", &handler) result = page_command("Page.navigate", url: url) loaded.wait(@options.timeout) @client.off("Page.loadEventFired", handler) result else page_command("Page.navigate", url: url) end end |
#network ⇒ Object
181 182 183 |
# File 'lib/lightpanda/browser.rb', line 181 def network @network ||= Network.new(self) end |
#page_command(method, **params) ⇒ Object
66 67 68 |
# File 'lib/lightpanda/browser.rb', line 66 def page_command(method, **params) @client.command(method, params, session_id: @session_id) end |
#quit ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/lightpanda/browser.rb', line 54 def quit @client&.close @process&.stop @client = nil @process = nil @started = false end |
#refresh ⇒ Object Also known as: reload
107 108 109 |
# File 'lib/lightpanda/browser.rb', line 107 def refresh page_command("Page.reload") end |
#restart ⇒ Object
49 50 51 52 |
# File 'lib/lightpanda/browser.rb', line 49 def restart quit start end |
#screenshot(path: nil, format: :png, quality: nil, full_page: false, encoding: :binary) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/lightpanda/browser.rb', line 147 def screenshot(path: nil, format: :png, quality: nil, full_page: false, encoding: :binary) params = { format: format.to_s } params[:quality] = quality if quality && format == :jpeg if full_page metrics = page_command("Page.getLayoutMetrics") content_size = metrics["contentSize"] params[:clip] = { x: 0, y: 0, width: content_size["width"], height: content_size["height"], scale: 1 } end result = page_command("Page.captureScreenshot", **params) data = result["data"] if encoding == :base64 data else decoded = Base64.decode64(data) if path File.binwrite(path, decoded) path else decoded end end end |
#start ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/lightpanda/browser.rb', line 25 def start return if @started if @options.ws_url? @client = Client.new(@options.ws_url, @options) else @process = Process.new(@options) @process.start @client = Client.new(@process.ws_url, @options) end create_page @started = true end |
#title ⇒ Object
116 117 118 |
# File 'lib/lightpanda/browser.rb', line 116 def title evaluate("document.title") end |