playwright-ruby-client Documentation

website·Indexed 29 days ago

https://playwright-ruby-client.vercel.app

A Ruby client library for Playwright that enables browser automation for Chromium, Firefox, and WebKit. Includes guides for RSpec and Ruby on Rails integration via Capybara, as well as API references for BrowserContext, Page, Locator, and APIRequestContext.

Tokens
41.6K
Snippets
310
Records
400
Agent score
74%

What's inside playwright-ruby-client

  1. Overview of Playwright Ruby Locators

    Locators are the central mechanism for auto-waiting and retry-ability in Playwright. They represent a way to find one or more elements on a page at any given moment. Locators are typically created using the Page#locator method.
  2. Simulate mouse interactions using the Mouse class

    The Mouse class allows for precise control of the mouse cursor using main-frame CSS pixels relative to the top-left corner of the viewport. Every page object provides access to its own mouse instance via page.mouse. To debug mouse movements, use the Trace viewer or Playwright Inspector, which display a red dot at the location of each mouse action.
    # Example: Trace a 100x100 square
    page.mouse.move(0, 0)
    page.mouse.down
    page.mouse.move(0, 100)
    page.mouse.move(100, 100)
    page.mouse.move(100, 0)
    page.mouse.move(0, 0)
    page.mouse.up
  3. Understand and create JSHandle instances

    A JSHandle represents an in-page JavaScript object. It prevents the referenced object from being garbage collected until the handle is explicitly disposed of or the origin frame/parent context is destroyed. JSHandle instances can be passed as arguments to Page#eval_on_selector, Page#evaluate, and Page#evaluate_handle.
    window_handle = page.evaluate_handle("window")
  4. Expose Ruby functions to the browser JavaScript environment

    You can add functions to the window object of every frame in every page within a context.

    • expose_binding: The callback receives information about the caller ({ browserContext: BrowserContext, page: Page, frame: Frame }).
    • expose_function: A standard function exposure that returns a Promise to the browser.
    # Example: expose_binding to get page URL
    browser_context.expose_binding("pageURL", ->(source) { source[:page].url })
    
    # Example: expose_function to provide SHA256 hashing
    require 'digest'
    def sha256(text)
      Digest::SHA256.hexdigest(text)
    end
    browser_context.expose_function("sha256", method(:sha256))
  5. Manage browser local and session storage using WebStorage

    The WebStorage class provides an asynchronous, browser-consistent API to interact with localStorage and sessionStorage for the current origin. You can access these storage instances via Page#local_storage and Page#session_storage.
    page.goto("https://example.com")
    # Set a value in local storage
    page.local_storage.set_item("token", "abc")
    
    # Retrieve a value
    token = page.local_storage.get_item("token")
    
    # Get all storage items
    all = page.local_storage.items
    
    # Remove a specific item
    page.local_storage.remove_item("token")
    
    # Clear all storage
    page.local_storage.clear
  6. Simulate WebAuthn passkeys using Credentials

    The Credentials class is a virtual WebAuthn authenticator scoped to a BrowserContext. It intercepts navigator.credentials.create() and navigator.credentials.get() calls in the browser, allowing tests to register and use passkeys without a real hardware security key.
  7. Inspect FileChooser properties

    The FileChooser class provides methods to retrieve metadata about the file upload input:

    • element: Returns the input element associated with the file chooser.
    • multiple?: Returns a boolean indicating if the file chooser accepts multiple files.
    • page: Returns the page object that the file chooser belongs to.
  8. Run Playwright Ruby client on Alpine Linux using a remote server

    Because Playwright requires specific shell permissions and runtime dependencies that are difficult to install on Alpine Linux, Docker, or Serverless environments, you can use a client-server architecture. The Ruby client runs on Alpine Linux and communicates via WebSockets with a Playwright server running in the official Microsoft Playwright Docker image.
  9. Understand ElementHandle vs Locator

    An ElementHandle represents a specific DOM element. It is created using Page#query_selector. Unlike a Locator, which captures the logic to find an element every time it is used, an ElementHandle points to a particular instance. If the DOM element is replaced (e.g., by a React render), the ElementHandle remains pointed at the old element, which can lead to unexpected behavior. Use of ElementHandle is generally discouraged in favor of Locator objects and web-first assertions.
    # ElementHandle (points to a specific element)
    handle = page.query_selector("text=Submit")
    handle.click
    
    # Locator (re-locates the element every time it is used)
    locator = page.get_by_text("Submit")
    locator.click
  10. Handle API responses using the APIResponse class

    The APIResponse class represents the response returned by APIRequestContext#get and similar request methods. It provides a comprehensive set of methods to inspect the HTTP response, including status codes, headers, and the response body in various formats.
    playwright.chromium.launch do |browser|
      context = browser.new_context
      response = context.request.get("https://example.com/user/repos")
    
      response.ok? # => true
      response.status # => 200
      response.headers["content-type"] # => "application/json; charset=utf-8"
      response.json # => { "name" => "Foo" }
      response.body # => "{ \"name\" => \"Foo\" }"
    end
  11. Inspect and manage WebSocket connections with the WebSocket class

    The WebSocket class represents WebSocket connections within a page. It is used to inspect and manipulate transmitted and received data. For intercepting or modifying WebSocket frames, use WebSocketRoute instead.
  12. Interact with a browser page using the Page class

    The Page class provides methods to interact with a single tab in a Browser or an extension background page in Chromium. A single Browser instance can manage multiple Page instances.
    playwright.webkit.launch do |browser|
      page = browser.new_page
      page.goto('https://example.com/')
      page.screenshot(path: 'screenshot.png')
    end