[−][src]Crate thirtyfour
Thirtyfour is a Selenium / WebDriver library for Rust, for automated website UI testing.
It supports the full W3C WebDriver spec. Tested with Chrome and Firefox although any W3C-compatible WebDriver should work.
Both sync and async APIs are provided (see examples below).
Features
- All W3C WebDriver and WebElement methods supported
- Async / await support (both tokio and async-std runtimes supported via feature flags)
- Synchronous support (use the
blocking
feature flag) - Create new browser session directly via WebDriver (e.g. chromedriver)
- Create new browser session via Selenium Standalone or Grid
- Automatically close browser session on drop
- Find elements (via all common selectors e.g. Id, Class, CSS, Tag, XPath)
- Send keys to elements, including key-combinations
- Execute Javascript
- Action Chains
- Get and set cookies
- Switch to frame/window/element/alert
- Shadow DOM support
- Alert support
- Capture / Save screenshot of browser or individual element as PNG
- Easy to add support for more HTTP clients using generics
Feature Flags
Support for tokio and async-std async runtimes, and support for synchronous http, are provided via feature flags.
-
tokio-runtime
: (Default) Use the tokio runtime with the reqwest http client. -
async-std-runtime
: Use the async-std runtime with the surf http client.Make sure you specify
default-features = false
to avoid conflicts with the tokio runtime support. -
blocking
: Enables the synchronous reqwest http client viathirtyfour::sync::prelude::*
.The
blocking
flag also enablestokio-runtime
because the synchronous reqwest client uses tokio internally.
NOTE: You cannot specify async-std-runtime
with other feature flags.
Examples
The following examples assume you have a selenium server running at localhost:4444, and a demo web app running at http://webappdemo
You can set these up using docker-compose, as follows:
docker-compose up -d --build
The included web app demo is purely for demonstration / unit testing purposes and is not required in order to use this library in other projects.
Async example:
use thirtyfour::prelude::*; use tokio; #[tokio::main] async fn main() -> WebDriverResult<()> { let caps = DesiredCapabilities::chrome(); let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; // Navigate to URL. driver.get("http://webappdemo").await?; // Navigate to page, by chaining futures together and awaiting the result. driver.find_element(By::Id("pagetextinput")).await?.click().await?; // Find element. let elem_div = driver.find_element(By::Css("div[data-section='section-input']")).await?; // Find element from element. let elem_text = elem_div.find_element(By::Name("input1")).await?; // Type in the search terms. elem_text.send_keys("selenium").await?; // Click the button. let elem_button = elem_div.find_element(By::Tag("button")).await?; elem_button.click().await?; // Get text value of element. let elem_result = driver.find_element(By::Name("input-result")).await?; assert_eq!(elem_result.text().await?, "selenium"); Ok(()) }
Sync example:
use thirtyfour::sync::prelude::*; fn main() -> WebDriverResult<()> { let caps = DesiredCapabilities::chrome(); let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps)?; // Navigate to URL. driver.get("http://webappdemo")?; // Navigate to page. driver.find_element(By::Id("pagetextinput"))?.click()?; // Find element. let elem_div = driver.find_element(By::Css("div[data-section='section-input']"))?; // Find element from element. let elem_text = elem_div.find_element(By::Name("input1"))?; // Type in the search terms. elem_text.send_keys("selenium")?; // Click the button. let elem_button = elem_div.find_element(By::Tag("button"))?; elem_button.click()?; // Get text value of element. let elem_result = driver.find_element(By::Name("input-result"))?; assert_eq!(elem_result.text()?, "selenium"); Ok(()) }
Re-exports
pub use common::capabilities::chrome::ChromeCapabilities; |
pub use common::capabilities::desiredcapabilities::*; |
pub use common::capabilities::edge::EdgeCapabilities; |
pub use common::capabilities::firefox::FirefoxCapabilities; |
pub use common::capabilities::ie::InternetExplorerCapabilities; |
pub use common::capabilities::opera::OperaCapabilities; |
pub use common::capabilities::safari::SafariCapabilities; |
pub use common::command::By; |
pub use common::cookie::Cookie; |
pub use common::keys::Keys; |
pub use common::keys::TypingData; |
pub use common::scriptargs::ScriptArgs; |
pub use common::types::*; |
Modules
action_chain | |
common | |
error | |
http_async | |
prelude | |
support | |
sync |
Structs
Alert | Struct for managing alerts. |
GenericWebDriver | The GenericWebDriver struct encapsulates an async Selenium WebDriver browser session. For the sync driver, see sync::GenericWebDriver. |
SwitchTo | Struct for switching between frames/windows/alerts. |
WebElement | The WebElement struct encapsulates a single element on a page. |
Traits
WebDriverCommands | All browser-level W3C WebDriver commands are implemented under this trait. |
Type Definitions
WebDriver |