Skip to main content
Info

In short: the same browser test in Rust gets errors you cannot forget, real parallel browsers on one runtime, helpers that run on both engines, options the compiler checks, and one native binary to ship. This page shows each with code you can copy. Why Rust? explains the reasons; this page shows the practice.

Errors you cannot forget#

Every call that can fail returns seleniumbase_rs::Result<T>, so a failure is a value, not an exception that slips past. End a call with ? to stop the test there, or handle the error where you want a fallback:

tests/login.rs

The compiler warns about a Result you ignore, so a check you forgot to await or to propagate shows up at build time, not as a test that passed by accident.

Many browsers at once#

Tests and scrapers run on tokio, so several browsers work in parallel on one runtime with no threads of your own. BrowserPool hands out pages and shares browsers between tasks:

src/main.rs

A lease gives its browser back when you call release. The pool caps how many browsers start and reuses them, so ten tasks do not open ten Chromes. See Pure CDP.

One helper for both engines#

BrowserApi, ElementApi, AssertionApi and ScreenshotApi are traits that both BaseCase (WebDriver) and sb_cdp::Page (pure CDP) implement. Write a helper once against the traits and call it with either engine:

src/helpers.rs

The bound says exactly what the helper needs, and the compiler refuses a type that does not provide it. See Capability traits.

Options the compiler checks#

Choices are enums, not strings, so a typo does not compile:

Instead ofYou writeA wrong value
"uc", "cdp"DriverMode::Uc, DriverMode::Cdpdoes not compile
"visible"State::Visible, State::Hiddendoes not compile
by="text"SelectBy::Text("Large"), SelectBy::Index(2)does not compile
"random"Spoof::Random, Spoof::Value(..), Spoof::Offdoes not compile

Values that only make sense together are checked when you build them: LaunchOptions::builder()...build()? and Fingerprint::builder()...build()? return an error that names the problem (such as Off on a setting that cannot be turned off) before any browser starts. See Design: fingerprint API v2.

Tests are Cargo tests#

A browser test is an ordinary #[tokio::test], so cargo test runs it, filters it by name and reports it with the rest. sb_test! writes the test for you and always quits the browser, even when the test fails:

tests/example.rs
Terminal

See Macros and Writing browser tests.

One binary to ship#

sbrs and sbrs-mcp compile to single native binaries: no Python, no virtual environment, no package manager on the target machine. Copy the binary into a container or a CI image, or install it from the tag:

Terminal

See Deployment and Docker.

Next steps#