### Install Ferrum Gem Source: https://github.com/rubycdp/ferrum/blob/main/README.md Add the Ferrum gem to your Gemfile to include it in your project. Run `bundle install` afterwards. ```ruby gem "ferrum" ``` -------------------------------- ### Get All Headers Source: https://github.com/rubycdp/ferrum/blob/main/docs/12-headers.md Retrieves all currently set headers. ```APIDOC ## GET page.headers ### Description Get all headers. ### Method GET ### Endpoint page.headers ``` -------------------------------- ### Start Screencast and Save Frames Source: https://github.com/rubycdp/ferrum/blob/main/docs/6-screencast.md Starts sending frames to record screencast. Frames are Base64 encoded images that are decoded and saved to files. Chrome only sends new frames when page content changes. ```ruby require "base64" page.go_to("https://apple.com/ipad") page.start_screencast(format: :jpeg, quality: 75) do |data, metadata| timestamp = (metadata["timestamp"] * 1000).to_i File.binwrite("image_#{timestamp}.jpg", Base64.decode64(data)) end sleep 10 page.stop_screencast ``` -------------------------------- ### Example: Interacting with Elements within a Frame Source: https://github.com/rubycdp/ferrum/blob/main/docs/15-frames.md Demonstrates how to access a frame and then use finders to locate elements specifically within that frame's context. ```APIDOC ## Example: Interacting with Elements within a Frame ### Description You can access a frame and then use finders to locate elements within that frame. ### Request Example ```ruby page.go_to("https://example.com/page-with-iframe") frame = page.at_xpath("//iframe").frame # => Frame frame.at_css("//a[text() = 'Log in']") # => Node ``` ``` -------------------------------- ### start_screencast Source: https://github.com/rubycdp/ferrum/blob/main/docs/6-screencast.md Starts sending frames to record screencast to the given block. The block receives base64-encoded image data, metadata, and the session ID for each frame. ```APIDOC ## start_screencast ### Description Starts sending frames to record screencast to the given block. ### Method `start_screencast` ### Parameters #### Options - **:format** (Symbol) - Optional - `:jpeg` | `:png` The format the image should be returned in. - **:quality** (Integer) - Optional - The image quality. Note: 0-100 works for JPEG only. - **:max_width** (Integer) - Optional - Maximum screencast frame width. - **:max_height** (Integer) - Optional - Maximum screencast frame height. - **:every_nth_frame** (Integer) - Optional - Send every n-th frame. #### Block Inputs - **data** (String) - Base64-encoded compressed image. - **metadata** (Hash) - Screencast frame metadata. - **"offsetTop"** (Integer) - Top offset in DIP. - **"pageScaleFactor"** (Integer) - Page scale factor. - **"deviceWidth"** (Integer) - Device screen width in DIP. - **"deviceHeight"** (Integer) - Device screen height in DIP. - **"scrollOffsetX"** (Integer) - Position of horizontal scroll in CSS pixels. - **"scrollOffsetY"** (Integer) - Position of vertical scroll in CSS pixels. - **"timestamp"** (Float) - Optional - Frame swap timestamp in seconds since Unix epoch. - **session_id** (Integer) - Frame number. ### Request Example ```ruby page.start_screencast(format: :jpeg, quality: 75) do |data, metadata| timestamp = (metadata["timestamp"] * 1000).to_i File.binwrite("image_#{timestamp}.jpg", Base64.decode64(data)) end ``` ### Response No explicit response defined, but the block receives frame data. ``` -------------------------------- ### Evaluate JavaScript for Page Dimensions Source: https://github.com/rubycdp/ferrum/blob/main/README.md Use the `evaluate` method to execute JavaScript within the page context and retrieve results. This example gets the full width and height of the document. ```ruby browser = Ferrum::Browser.new page = browser.create_page page.go_to("https://www.google.com/search?q=Ruby+headless+driver+for+Capybara") width, height = page.evaluate <<~JS [document.documentElement.offsetWidth, document.documentElement.offsetHeight] JS # => [1024, 1931] browser.quit ``` -------------------------------- ### Get Downloaded Files Information - Ruby Source: https://github.com/rubycdp/ferrum/blob/main/docs/8-downloads.md Retrieves information about all downloaded files. This is useful for verifying download status and details. ```ruby page.go_to("http://localhost/attachment.pdf") page.downloads.files # => [{"frameId"=>"E3316DF1B5383D38F8ADF7485005FDE3", "guid"=>"11a68745-98ac-4d54-9b57-9f9016c268b3", "url"=>"http://localhost/attachment.pdf", "suggestedFilename"=>"attachment.pdf", "totalBytes"=>4911, "receivedBytes"=>4911, "state"=>"completed"}] ``` -------------------------------- ### position Source: https://github.com/rubycdp/ferrum/blob/main/docs/3-navigation.md Gets the current position of the browser window on the screen. ```APIDOC ## position ### Description Gets the current position of the browser window. ### Response #### Success Response (200) - **position** (Array) - An array containing the [left, top] coordinates of the window. ``` -------------------------------- ### Perform Mouse Movements Source: https://github.com/rubycdp/ferrum/blob/main/README.md Utilize the `mouse` API to simulate mouse actions like moving, clicking (down/up), and tracing paths. This example traces a square. ```ruby # Trace a 100x100 square browser = Ferrum::Browser.new page = browser.create_page page.go_to("https://google.com") page.mouse .move(x: 0, y: 0) .down .move(x: 0, y: 100) .move(x: 100, y: 100) .move(x: 100, y: 0) .move(x: 0, y: 0) .up browser.quit ``` -------------------------------- ### Get Window Bounds - Ruby Source: https://github.com/rubycdp/ferrum/blob/main/docs/3-navigation.md Retrieve the current window bounds as a hash, including `left`, `top`, `width`, `height`, and `windowState`. ```ruby browser.window_bounds # => { "left": 0, "top": 1286, "width": 10, "height": 10, "windowState": "normal" } ``` -------------------------------- ### Simulate Mouse Movements Source: https://github.com/rubycdp/ferrum/blob/main/docs/1-introduction.md Use the `mouse` API to perform complex mouse interactions, such as tracing a square by moving the mouse, pressing/releasing the button, and returning to the start. ```ruby # Trace a 100x100 square browser = Ferrum::Browser.new page = browser.create_page page.go_to("https://google.com") page.mouse .move(x: 0, y: 0) .down .move(x: 0, y: 100) .move(x: 100, y: 100) .move(x: 100, y: 0) .move(x: 0, y: 0) .up browser.quit ``` -------------------------------- ### Main Frame Response Source: https://github.com/rubycdp/ferrum/blob/main/docs/7-network.md Gets the response object for the main frame of the page. ```APIDOC ## Main Frame Response ### Description Provides the response object for the main frame of the current page. ### Method `page.network.response` ### Response #### Success Response - `Network::Response`: The response object for the main frame. ### Request Example ```ruby page.go_to("https://github.com/") page.network.response # => # ``` ``` -------------------------------- ### Get Frame Title Source: https://github.com/rubycdp/ferrum/blob/main/docs/15-frames.md Retrieves the title of a specific frame. This can be helpful for identifying the content of a frame. ```ruby page.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe") frame = page.frames[1] frame.title # => HTML Demo: