Class: Browserctl::Client
- Inherits:
-
Object
- Object
- Browserctl::Client
- Defined in:
- lib/browserctl/client.rb
Overview
Thin IPC client that wraps each browserd command as a Ruby method call.
Instance Method Summary collapse
- #call(cmd, **params) ⇒ Object
-
#click(name, selector = nil, ref: nil) ⇒ Hash
Clicks an element identified by CSS selector or snapshot ref.
-
#cookies(name) ⇒ Hash
Returns all cookies for a named page.
-
#delete_cookies(name) ⇒ Hash
Deletes all cookies for a named page.
-
#devtools(name) ⇒ Hash
Returns the Chrome DevTools URL for a named page.
-
#dialog_accept(name, text: nil) ⇒ Hash
Pre-registers a one-shot handler to accept the next JS dialog on a page.
-
#dialog_dismiss(name) ⇒ Hash
Pre-registers a one-shot handler to dismiss the next JS dialog on a page.
-
#evaluate(name, expression) ⇒ Hash
Evaluates a JavaScript expression and returns the result.
-
#export_cookies(name, path) ⇒ Hash
Exports all cookies for a named page to a JSON file.
-
#fetch(key) ⇒ Hash
Retrieves a value from the daemon-scoped key-value store.
-
#fill(name, selector = nil, value = nil, ref: nil) ⇒ Hash
Fills an input element with a value.
-
#hover(name, selector) ⇒ Hash
Moves the mouse to the centre of the element matched by selector.
-
#import_cookies(name, path) ⇒ Hash
Imports cookies from a JSON file into a named page.
-
#initialize(socket_path = nil) ⇒ Client
constructor
A new instance of Client.
-
#navigate(name, url) ⇒ Hash
Navigates a page to a URL.
-
#page_close(name) ⇒ Hash
Closes a named page and removes it from the session.
-
#page_focus(name) ⇒ Hash
Brings the named page’s tab to front.
-
#page_list ⇒ Hash
Lists all open page names.
-
#page_open(name, url: nil) ⇒ Hash
Opens or focuses a named browser page.
-
#pause(name, message: nil) ⇒ Hash
Pauses automation on a page so a human can interact directly.
-
#ping ⇒ Hash
Checks if browserd is alive.
-
#press(name, key) ⇒ Hash
Fires a keydown + keyup event for the given key name on a page.
-
#resume(name) ⇒ Hash
Resumes automation on a paused page.
-
#screenshot(name, path: nil, full: false) ⇒ Hash
Takes a screenshot of a named page.
-
#select(name, selector, value) ⇒ Hash
Sets a <select> element’s value and fires a change event.
-
#session_delete(session_name) ⇒ Hash
Permanently deletes a named session.
-
#session_list ⇒ Hash
Lists all saved sessions.
-
#session_load(session_name) ⇒ Hash
Restores a previously saved session into the running daemon.
-
#session_save(session_name) ⇒ Hash
Saves the current browser state (cookies, localStorage, open pages) to a named session.
-
#set_cookie(name, cookie_name, value, domain, path: "/") ⇒ Hash
Sets a cookie on a named page.
-
#shutdown ⇒ Hash
Shuts down browserd gracefully.
-
#snapshot(name, format: "elements", diff: false) ⇒ Hash
Takes a DOM snapshot.
-
#storage_delete(name, stores: "all") ⇒ Hash
Clears localStorage and/or sessionStorage for the page.
-
#storage_export(name, path, stores: "all") ⇒ Hash
Exports localStorage and/or sessionStorage to a JSON file.
-
#storage_get(name, key, store: "local") ⇒ Hash
Returns the value of a localStorage or sessionStorage key.
-
#storage_import(name, path) ⇒ Hash
Imports storage keys from a JSON file into the page’s localStorage.
-
#storage_set(name, key, value, store: "local") ⇒ Hash
Sets a localStorage or sessionStorage key.
-
#store(key, value) ⇒ Hash
Stores a value in the daemon-scoped key-value store.
-
#upload(name, selector, path) ⇒ Hash
Sets a file-input element to the given file path.
-
#url(name) ⇒ Hash
Returns the current URL of a named page.
-
#wait(name, selector, timeout: 30) ⇒ Hash
Waits for a CSS selector to appear within the given timeout.
Constructor Details
#initialize(socket_path = nil) ⇒ Client
Returns a new instance of Client.
12 13 14 |
# File 'lib/browserctl/client.rb', line 12 def initialize(socket_path = nil) @socket_path = socket_path || auto_discover_socket end |
Instance Method Details
#call(cmd, **params) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/browserctl/client.rb', line 16 def call(cmd, **params) result = communicate(JSON.generate({ cmd: cmd }.merge(params))) Recording.append(cmd, **params) if result[:ok] result rescue Errno::ENOENT, Errno::ECONNREFUSED raise "browserd is not running — start it with: browserd" end |
#click(name, selector = nil, ref: nil) ⇒ Hash
Clicks an element identified by CSS selector or snapshot ref.
50 51 52 53 54 |
# File 'lib/browserctl/client.rb', line 50 def click(name, selector = nil, ref: nil) raise ArgumentError, "click: provide selector or ref:" unless selector || ref call("click", name: name, selector: selector, ref: ref) end |
#cookies(name) ⇒ Hash
Returns all cookies for a named page.
145 |
# File 'lib/browserctl/client.rb', line 145 def (name) = call("cookies", name: name) |
#delete_cookies(name) ⇒ Hash
Deletes all cookies for a named page.
162 |
# File 'lib/browserctl/client.rb', line 162 def (name) = call("delete_cookies", name: name) |
#devtools(name) ⇒ Hash
Returns the Chrome DevTools URL for a named page.
124 |
# File 'lib/browserctl/client.rb', line 124 def devtools(name) = call("devtools", name: name) |
#dialog_accept(name, text: nil) ⇒ Hash
Pre-registers a one-shot handler to accept the next JS dialog on a page.
263 |
# File 'lib/browserctl/client.rb', line 263 def dialog_accept(name, text: nil) = call("dialog_accept", name: name, text: text) |
#dialog_dismiss(name) ⇒ Hash
Pre-registers a one-shot handler to dismiss the next JS dialog on a page.
268 |
# File 'lib/browserctl/client.rb', line 268 def dialog_dismiss(name) = call("dialog_dismiss", name: name) |
#evaluate(name, expression) ⇒ Hash
Evaluates a JavaScript expression and returns the result.
100 |
# File 'lib/browserctl/client.rb', line 100 def evaluate(name, expression) = call("evaluate", name: name, expression: expression) |
#export_cookies(name, path) ⇒ Hash
Exports all cookies for a named page to a JSON file. File I/O is client-side; daemon provides the cookie data.
169 170 171 172 173 174 175 176 |
# File 'lib/browserctl/client.rb', line 169 def (name, path) result = call("cookies", name: name) return result unless result[:ok] FileUtils.mkdir_p(File.dirname(path)) File.open(path, "w", 0o600) { |f| f.write(JSON.generate(result[:cookies])) } { ok: true, path: path, count: result[:cookies].length } end |
#fetch(key) ⇒ Hash
Retrieves a value from the daemon-scoped key-value store.
140 |
# File 'lib/browserctl/client.rb', line 140 def fetch(key) = call("fetch", key: key) |
#fill(name, selector = nil, value = nil, ref: nil) ⇒ Hash
Fills an input element with a value.
62 63 64 65 66 |
# File 'lib/browserctl/client.rb', line 62 def fill(name, selector = nil, value = nil, ref: nil) raise ArgumentError, "fill: provide selector or ref:" unless selector || ref call("fill", name: name, selector: selector, ref: ref, value: value) end |
#hover(name, selector) ⇒ Hash
Moves the mouse to the centre of the element matched by selector.
243 |
# File 'lib/browserctl/client.rb', line 243 def hover(name, selector) = call("hover", name: name, selector: selector) |
#import_cookies(name, path) ⇒ Hash
Imports cookies from a JSON file into a named page.
182 183 184 185 186 187 |
# File 'lib/browserctl/client.rb', line 182 def (name, path) raise "cookie file not found: #{path}" unless File.exist?(path) = JSON.parse(File.read(path), symbolize_names: true) call("import_cookies", name: name, cookies: ) end |
#navigate(name, url) ⇒ Hash
Navigates a page to a URL. Returns ‘challenge: true` when Cloudflare is detected.
43 |
# File 'lib/browserctl/client.rb', line 43 def navigate(name, url) = call("navigate", name: name, url: url) |
#page_close(name) ⇒ Hash
Closes a named page and removes it from the session.
33 |
# File 'lib/browserctl/client.rb', line 33 def page_close(name) = call("page_close", name: name) |
#page_focus(name) ⇒ Hash
Brings the named page’s tab to front. Only works when browserd was started with –headed.
129 |
# File 'lib/browserctl/client.rb', line 129 def page_focus(name) = call("page_focus", name: name) |
#page_list ⇒ Hash
Lists all open page names.
37 |
# File 'lib/browserctl/client.rb', line 37 def page_list = call("page_list") |
#page_open(name, url: nil) ⇒ Hash
Opens or focuses a named browser page.
28 |
# File 'lib/browserctl/client.rb', line 28 def page_open(name, url: nil) = call("page_open", name: name, url: url) |
#pause(name, message: nil) ⇒ Hash
Pauses automation on a page so a human can interact directly.
114 |
# File 'lib/browserctl/client.rb', line 114 def pause(name, message: nil) = call("pause", name: name, message: ) |
#ping ⇒ Hash
Checks if browserd is alive.
104 |
# File 'lib/browserctl/client.rb', line 104 def ping = call("ping") |
#press(name, key) ⇒ Hash
Fires a keydown + keyup event for the given key name on a page.
237 |
# File 'lib/browserctl/client.rb', line 237 def press(name, key) = call("press", name: name, key: key) |
#resume(name) ⇒ Hash
Resumes automation on a paused page.
119 |
# File 'lib/browserctl/client.rb', line 119 def resume(name) = call("resume", name: name) |
#screenshot(name, path: nil, full: false) ⇒ Hash
Takes a screenshot of a named page.
73 |
# File 'lib/browserctl/client.rb', line 73 def screenshot(name, path: nil, full: false) = call("screenshot", name: name, path: path, full: full) |
#select(name, selector, value) ⇒ Hash
Sets a <select> element’s value and fires a change event.
257 |
# File 'lib/browserctl/client.rb', line 257 def select(name, selector, value) = call("select", name: name, selector: selector, value: value) |
#session_delete(session_name) ⇒ Hash
Permanently deletes a named session.
293 294 295 |
# File 'lib/browserctl/client.rb', line 293 def session_delete(session_name) call("session_delete", session_name: session_name) end |
#session_list ⇒ Hash
Lists all saved sessions.
286 287 288 |
# File 'lib/browserctl/client.rb', line 286 def session_list call("session_list") end |
#session_load(session_name) ⇒ Hash
Restores a previously saved session into the running daemon.
280 281 282 |
# File 'lib/browserctl/client.rb', line 280 def session_load(session_name) call("session_load", session_name: session_name) end |
#session_save(session_name) ⇒ Hash
Saves the current browser state (cookies, localStorage, open pages) to a named session.
273 274 275 |
# File 'lib/browserctl/client.rb', line 273 def session_save(session_name) call("session_save", session_name: session_name) end |
#set_cookie(name, cookie_name, value, domain, path: "/") ⇒ Hash
Sets a cookie on a named page.
154 155 156 157 |
# File 'lib/browserctl/client.rb', line 154 def (name, , value, domain, path: "/") call("set_cookie", name: name, cookie_name: , value: value, domain: domain, path: path) end |
#shutdown ⇒ Hash
Shuts down browserd gracefully.
108 |
# File 'lib/browserctl/client.rb', line 108 def shutdown = call("shutdown") |
#snapshot(name, format: "elements", diff: false) ⇒ Hash
Takes a DOM snapshot. Returns ‘challenge: true` when Cloudflare is detected.
80 81 82 |
# File 'lib/browserctl/client.rb', line 80 def snapshot(name, format: "elements", diff: false) call("snapshot", name: name, format: format, diff: diff) end |
#storage_delete(name, stores: "all") ⇒ Hash
Clears localStorage and/or sessionStorage for the page.
229 230 231 |
# File 'lib/browserctl/client.rb', line 229 def storage_delete(name, stores: "all") call("storage_delete", name: name, stores: stores) end |
#storage_export(name, path, stores: "all") ⇒ Hash
Exports localStorage and/or sessionStorage to a JSON file.
213 214 215 |
# File 'lib/browserctl/client.rb', line 213 def storage_export(name, path, stores: "all") call("storage_export", name: name, path: path, stores: stores) end |
#storage_get(name, key, store: "local") ⇒ Hash
Returns the value of a localStorage or sessionStorage key.
194 195 196 |
# File 'lib/browserctl/client.rb', line 194 def storage_get(name, key, store: "local") call("storage_get", name: name, key: key, store: store) end |
#storage_import(name, path) ⇒ Hash
Imports storage keys from a JSON file into the page’s localStorage.
221 222 223 |
# File 'lib/browserctl/client.rb', line 221 def storage_import(name, path) call("storage_import", name: name, path: path) end |
#storage_set(name, key, value, store: "local") ⇒ Hash
Sets a localStorage or sessionStorage key.
204 205 206 |
# File 'lib/browserctl/client.rb', line 204 def storage_set(name, key, value, store: "local") call("storage_set", name: name, key: key, value: value, store: store) end |
#store(key, value) ⇒ Hash
Stores a value in the daemon-scoped key-value store.
135 |
# File 'lib/browserctl/client.rb', line 135 def store(key, value) = call("store", key: key, value: value) |
#upload(name, selector, path) ⇒ Hash
Sets a file-input element to the given file path.
250 |
# File 'lib/browserctl/client.rb', line 250 def upload(name, selector, path) = call("upload", name: name, selector: selector, path: path) |
#url(name) ⇒ Hash
Returns the current URL of a named page.
94 |
# File 'lib/browserctl/client.rb', line 94 def url(name) = call("url", name: name) |
#wait(name, selector, timeout: 30) ⇒ Hash
Waits for a CSS selector to appear within the given timeout.
89 |
# File 'lib/browserctl/client.rb', line 89 def wait(name, selector, timeout: 30) = call("wait", name: name, selector: selector, timeout: timeout) |