#web-driver #session #browser #command #bi-di #send

webdriverbidi

WebDriver BiDi client implementation in Rust

14 releases

0.1.13 Jan 17, 2025
0.1.12 Jan 11, 2025
0.1.5 Dec 29, 2024

#19 in WebSocket

Download history 63/week @ 2024-12-18 399/week @ 2024-12-25 675/week @ 2025-01-01 334/week @ 2025-01-08 140/week @ 2025-01-15

1,611 downloads per month
Used in 2 crates

MIT license

1MB
6.5K SLoC

webdriverbidi

Overview

The webdriverbidi library provides an interface for interacting with web browsers through the WebDriver BiDi (Bidirectional) protocol. This library allows you to create and manage WebDriver sessions, send commands, and handle responses asynchronously through WebSockets.

Features

  • Create and manage WebDriver BiDi sessions
  • Send commands
  • Handle events asynchronously

Getting Started

Prerequisites

  • Rust and Cargo installed
  • A WebDriver server that supports the BiDi protocol

Installation

Add the following to your Cargo.toml (the example below will also require tokio with full features):

[dependencies]
webdriverbidi = "0.1.13"

Usage

Start a WebDriver BiDi compliant server

$ geckodriver --host=localhost --port=4444

Create a new Rust project and add the following code to src/main.rs:

use tokio;
use tokio::time;

// --------------------------------------------------

use webdriverbidi::remote::browsing_context::{
    GetTreeParameters, NavigateParameters, ReadinessState,
};
use webdriverbidi::session::WebDriverBiDiSession;
use webdriverbidi::webdriver::capabilities::CapabilitiesRequest;

// --------------------------------------------------

async fn sleep(secs: u64) {
    time::sleep(time::Duration::from_secs(secs)).await
}

#[tokio::main]
async fn main() {
    // Initialize a new WebDriver BiDi session and start it
    let host = String::from("localhost");
    let port = 4444;
    let capabilities = CapabilitiesRequest::default();
    let mut session = WebDriverBiDiSession::new(host, port, capabilities);
    session.start().await.expect("Failed to start session");

    // Get the browsing context tree
    let get_tree_params = GetTreeParameters::new(None, None);
    let get_tree_rslt = session
        .browsing_context_get_tree(get_tree_params)
        .await
        .expect("Failed to get browsing context tree");

    // Navigate to rust-lang.org
    let navigate_params = NavigateParameters::new(
        get_tree_rslt.contexts[0].context.clone(),
        "https://www.rust-lang.org/".to_string(),
        Some(ReadinessState::Complete),
    );
    session
        .browsing_context_navigate(navigate_params)
        .await
        .expect("Failed to navigate");

    sleep(2).await;

    // Close the session
    session.close().await.expect("Failed to close session");
}

Module Coverage

session

Types

  • session.CapabilitiesRequest
  • session.CapabilityRequest
  • session.ProxyConfiguration
  • session.UserPromptHandler
  • session.UserPromptHandlerType
  • session.Subscription
  • session.SubscriptionRequest
  • session.UnsubscribeByIDRequest
  • session.UnsubscribeByAttributesRequest

Commands

  • session.status
  • session.new
  • session.end
  • session.subscribe
  • session.unsubscribe

browser

Types

  • browser.ClientWindow
  • browser.ClientWindowInfo
  • browser.UserContext
  • browser.UserContextInfo

Commands

  • browser.close
  • browser.createUserContext
  • browser.getClientWindows
  • browser.getUserContexts
  • browser.removeUserContext
  • browser.setClientWindowState

browsingContext

Types

  • browsingContext.BrowsingContext
  • browsingContext.Info
  • browsingContext.Locator
  • browsingContext.Navigation
  • browsingContext.NavigationInfo
  • browsingContext.ReadinessState
  • browsingContext.UserPromptType

Commands

  • browsingContext.activate
  • browsingContext.captureScreenshot
  • browsingContext.close
  • browsingContext.create
  • browsingContext.getTree
  • browsingContext.handleUserPrompt
  • browsingContext.locateNodes
  • browsingContext.navigate
  • browsingContext.print
  • browsingContext.reload
  • browsingContext.setViewport
  • browsingContext.traverseHistory

Events

  • browsingContext.contextCreated
  • browsingContext.contextDestroyed
  • browsingContext.navigationStarted
  • browsingContext.fragmentNavigated
  • browsingContext.historyUpdated
  • browsingContext.domContentLoaded
  • browsingContext.load
  • browsingContext.downloadWillBegin
  • browsingContext.navigationAborted
  • browsingContext.navigationFailed
  • browsingContext.userPromptClosed
  • browsingContext.userPromptOpened

network

Types

  • network.AuthChallenge
  • network.AuthCredentials
  • network.BaseParameters
  • network.BytesValue
  • network.Cookie
  • network.CookieHeader
  • network.FetchTimingInfo
  • network.Header
  • network.Initiator
  • network.Intercept
  • network.Request
  • network.RequestData
  • network.ResponseContent
  • network.ResponseData
  • network.SetCookieHeader
  • network.UrlPattern

Commands

  • network.addIntercept
  • network.continueRequest
  • network.continueResponse
  • network.continueWithAuth
  • network.failRequest
  • network.provideResponse
  • network.removeIntercept
  • network.setCacheBehavior

Events

  • network.authRequired
  • network.beforeRequestSent
  • network.fetchError
  • network.responseCompleted
  • network.responseStarted

script

Types

  • script.Channel
  • script.ChannelValue
  • script.EvaluateResult
  • script.ExceptionDetails
  • script.Handle
  • script.InternalId
  • script.LocalValue
  • script.PreloadScript
  • script.Realm
  • script.PrimitiveProtocolValue
  • script.RealmInfo
  • script.RealmType
  • script.RemoteReference
  • script.RemoteValue
  • script.ResultOwnership
  • script.SerializationOptions
  • script.SharedId
  • script.StackFrame
  • script.StackTrace
  • script.Source
  • script.Target

Commands

  • script.addPreloadScript
  • script.disown
  • script.callFunction
  • script.evaluate
  • script.getRealms
  • script.removePreloadScript

Events

  • script.message
  • script.realmCreated
  • script.realmDestroyed

storage

Types

  • storage.PartitionKey

Commands

  • storage.getCookies
  • storage.setCookie
  • storage.deleteCookies

log

Types

  • log.LogEntry

Events

  • log.entryAdded

input

Types

  • input.ElementOrigin

Commands

  • input.performActions
  • input.releaseActions
  • input.setFiles

webExtension

Types

  • webExtension.Extension

Commands

  • webExtension.install
  • webExtension.uninstall

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Dependencies

~7–19MB
~251K SLoC