### Add ThirtyFour Crate to Cargo.toml (Rust) Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/installation.md This snippet shows how to add the thirtyfour crate as a dependency in your Rust project's Cargo.toml file. Ensure you replace THIRTYFOUR_CRATE_VERSION with the actual latest version of the crate. ```toml [dependencies] thirtyfour = "THIRTYFOUR_CRATE_VERSION" ``` -------------------------------- ### Run Selenium Example (Cargo) Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/tools/selenium.md This command executes the Selenium example integrated with the thirtyfour project. It assumes you have the Selenium server running and the necessary webdriver configured. ```bash cargo run --example selenium_example ``` -------------------------------- ### Establish WebDriver Connection and Start Session Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md Initializes a WebDriver instance by connecting to a webdriver server at a specified address and creating a new browser session. This sets up the automation environment. Dependencies include the WebDriver and DesiredCapabilities structs. ```rust let caps = DesiredCapabilities::chrome(); let driver = WebDriver::new("http://localhost:9515", caps).await?; ``` -------------------------------- ### Rust Selenium WebDriver Example Source: https://github.com/vrtgs/thirtyfour/blob/main/thirtyfour/README.md Demonstrates using the thirtyfour library in Rust to automate browser interactions. It navigates to a webpage, finds elements, types search queries, submits a form, and asserts the page title. This example requires a running chromedriver instance and uses Tokio for asynchronous operations. It includes explicit browser closing. ```rust use thirtyfour::prelude::*; #[tokio::main] async fn main() -> WebDriverResult<()> { let caps = DesiredCapabilities::chrome(); let driver = WebDriver::new("http://localhost:9515", caps).await?; // Navigate to https://wikipedia.org. driver.goto("https://wikipedia.org").await?; let elem_form = driver.find(By::Id("search-form")).await?; // Find element from element. let elem_text = elem_form.find(By::Id("searchInput")).await?; // Type in the search terms. elem_text.send_keys("selenium").await?; // Click the search button. let elem_button = elem_form.find(By::Css("button[type='submit']")).await?; elem_button.click().await?; // Look for header to implicitly wait for the page to load. driver.find(By::ClassName("firstHeading")).await?; assert_eq!(driver.title().await?, "Selenium - Wikipedia"); // Always explicitly close the browser. driver.quit().await?; Ok(()) } ``` -------------------------------- ### Start Selenium Standalone Server with Docker Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/tools/selenium.md Starts a Selenium standalone server in a Docker container. This command maps ports 4444 and 5900 from the container to the host, names the container 'selenium-server', and mounts `/dev/shm` for shared memory access, which is often required by browsers running in containers. The image used is `selenium/standalone-chrome:4.1.0-20211123`. ```bash docker run --rm -d -p 4444:4444 -p 5900:5900 --name selenium-server -v /dev/shm:/dev/shm selenium/standalone-chrome:4.1.0-20211123 ``` -------------------------------- ### Run geckodriver Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/contributing/testing.md Starts an instance of geckodriver in the background. This is a prerequisite for running Firefox-based tests. ```shell geckodriver ``` -------------------------------- ### Run chromedriver Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/contributing/testing.md Starts an instance of chromedriver in the background. This is a prerequisite for running Chrome-based tests. ```shell chromedriver ``` -------------------------------- ### Run Selenium Manually (Java) Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/tools/selenium.md This command manually starts the Selenium standalone server JAR file. It's a less recommended approach compared to Docker due to the need for manual updates of both Selenium and the webdriver. ```bash java -jar selenium.jar ``` -------------------------------- ### Basic Browser Automation with thirtyfour (Chrome) Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/first-code.md This Rust code snippet demonstrates a basic browser automation flow using the thirtyfour crate. It initializes a Chrome WebDriver, navigates to Wikipedia, searches for 'selenium', clicks the search button, and verifies the page title before closing the browser. ```rust use std::error::Error; use thirtyfour::prelude::*; #[tokio::main] async fn main() -> Result<(), Box> { let caps = DesiredCapabilities::chrome(); let driver = WebDriver::new("http://localhost:9515", caps).await?; // Navigate to https://wikipedia.org. driver.goto("https://wikipedia.org").await?; let elem_form = driver.find(By::Id("search-form")).await?; // Find element from element. let elem_text = elem_form.find(By::Id("searchInput")).await?; // Type in the search terms. elem_text.send_keys("selenium").await?; // Click the search button. let elem_button = elem_form.find(By::Css("button[type='submit']")).await?; elem_button.click().await?; // Look for header to implicitly wait for the page to load. driver.query(By::ClassName("firstHeading")).first().await?; assert_eq!(driver.title().await?, "Selenium – Wikipedia"); // Always explicitly close the browser. driver.quit().await?; Ok(()) } ``` -------------------------------- ### Add thirtyfour and tokio Dependencies to Cargo.toml Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/first-code.md This snippet shows how to add the necessary dependencies, 'thirtyfour' for browser automation and 'tokio' for asynchronous runtime, to your Rust project's Cargo.toml file. ```toml [dependencies] thirtyfour = "THIRTYFOUR_CRATE_VERSION" tokio = { version = "1", features = ["full"] } ``` -------------------------------- ### Navigate to a URL Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md Instructs the WebDriver to load a specific web page in the active browser session. This is a fundamental step for interacting with web content. Requires an active WebDriver instance. ```rust driver.goto("https://wikipedia.org").await?; ``` -------------------------------- ### Configure and Run Browser Automation with thirtyfour (Firefox) Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/first-code.md This snippet shows how to adapt the Rust code for Firefox automation by changing the DesiredCapabilities and WebDriver URL. It requires running 'geckodriver' instead of 'chromedriver'. ```rust let caps = DesiredCapabilities::firefox(); let driver = WebDriver::new("http://localhost:4444", caps).await?; ``` -------------------------------- ### Wait for Element and Assert Title (Rust) Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md This snippet demonstrates how to use `driver.query()` to find the first element with the class name 'firstHeading' and waits for it to be visible. It then asserts that the browser's title matches the expected string 'Selenium - Wikipedia'. ```rust driver.query(By::ClassName("firstHeading")).first().await?; assert_eq!(driver.title().await?, "Selenium - Wikipedia"); ``` -------------------------------- ### Instantiate and Use Components in Rust Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/features/components.md Shows the practical application of Thirtyfour Components by demonstrating how to instantiate a `CheckboxSectionComponent` from a `WebElement` and then interact with its nested components, specifically calling the `tick` method on each checkbox. ```rust let elem = driver.query(By::Id("checkbox-section")).await?; let component = CheckboxSectionComponent::from(elem); // Now you can get the checkbox components easily like this. let checkboxes = component.boxes.resolve().await?; for checkbox in checkboxes { checkbox.tick().await?; } ``` -------------------------------- ### Run thirtyfour tests with Firefox Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/contributing/testing.md Executes tests for the thirtyfour crate against Firefox by setting the THIRTYFOUR_BROWSER environment variable. ```shell THIRTYFOUR_BROWSER=firefox cargo test ``` -------------------------------- ### Run thirtyfour tests with cargo Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/contributing/testing.md Executes all tests for the thirtyfour crate using Cargo. By default, tests run against Chrome. ```shell cargo test ``` -------------------------------- ### Display Alert Message - JavaScript Source: https://github.com/vrtgs/thirtyfour/blob/main/thirtyfour/tests/test_html/sample_page.html A simple JavaScript function that displays a standard browser alert box with a predefined message. It has no external dependencies. ```javascript function showAlert() { alert("This is an alert"); } ``` -------------------------------- ### Type Text into an Element Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md Simulates keyboard input by sending a string of text to a specified web element, typically an input field. This allows for form submission or text entry. Requires a located element and the text to be sent. ```rust elem_text.send_keys("selenium").await?; ``` -------------------------------- ### Click an Element Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md Simulates a mouse click event on a specified web element. This is used to trigger actions like button presses or link navigation. Requires a located element that can be clicked. ```rust elem_button.click().await?; ``` -------------------------------- ### Close Browser Session (Rust) Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md This code snippet shows how to properly close the browser session using the `driver.quit().await?` method. This ensures that the browser window is closed after the automation script has finished executing. ```rust driver.quit().await?; ``` -------------------------------- ### Handle Prompt Dialog and Update Output - JavaScript Source: https://github.com/vrtgs/thirtyfour/blob/main/thirtyfour/tests/test_html/sample_page.html This function displays a prompt dialog, asking the user for input. The entered value is then used to update the innerHTML of an element with the ID 'alert-answer'. It requires an HTML element with the ID 'alert-answer'. ```javascript function showPrompt() { document.getElementById("alert-answer").innerHTML = prompt("What is your name?"); } ``` -------------------------------- ### Handle Button Click and Update Output - JavaScript Source: https://github.com/vrtgs/thirtyfour/blob/main/thirtyfour/tests/test_html/sample_page.html This function retrieves the value from an input element with the ID 'text-input2' and sets it as the innerHTML of an element with the ID 'text-output'. It requires the presence of these two HTML elements. ```javascript function onButtonClick() { const textElem = document.getElementById("text-input2"); const outputElem = document.getElementById("text-output"); outputElem.innerHTML = textElem.value; } ``` -------------------------------- ### Find Element by CSS Selector Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md Locates a web element using a CSS selector, allowing for more complex targeting based on attributes and element types. This is useful for elements without specific IDs or when a more general query is needed. Requires a WebDriver instance and a valid CSS selector. ```rust let elem_button = elem_form.find(By::Css("button[type='submit']")).await?; ``` -------------------------------- ### Wait for Element State Changes with thirtyfour Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/features/waiting.md Demonstrates using ElementWaiter to wait for an element to become enabled or clickable. ```rust elem.wait_until().enabled().await?; elem.wait_until().clickable().await?; ``` -------------------------------- ### Handle Confirmation Dialog - JavaScript Source: https://github.com/vrtgs/thirtyfour/blob/main/thirtyfour/tests/test_html/sample_page.html This function displays a confirmation dialog. Based on the user's choice (OK or Cancel), it updates the innerHTML of an element with the ID 'alert-answer' to reflect the selection. It requires an HTML element with the ID 'alert-answer'. ```javascript function showConfirm() { const elem = document.getElementById("alert-answer"); if (confirm("Press OK or Cancel")) { elem.innerHTML = "OK"; } else { elem.innerHTML = "Cancel"; } } ``` -------------------------------- ### Find Element by ID Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/getting-started/explaining-the-code.md Locates a web element on the current page using its unique ID attribute. This is a common method for targeting specific elements for interaction. Requires a WebDriver instance and a valid element ID. ```rust let elem_form = driver.find(By::Id("search-form")).await?; ``` ```rust let elem_text = elem_form.find(By::Id("searchInput")).await?; ``` -------------------------------- ### ElementQuery: Find first matching element using CSS or ID Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/features/queries.md Demonstrates using ElementQuery to find the first element that matches either a CSS selector or an ID. This method polls until a match is found or a timeout occurs. ```rust let elem_text = driver.query(By::Css("match.this")).or(By::Id("orThis")).first().await?; ``` -------------------------------- ### Add Selenium Manager Dependency in Cargo.toml Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/tools/selenium-manager.md This snippet shows how to add the Selenium Manager as a Git dependency in your Rust project's Cargo.toml file. It requires specifying the Git repository URL and the branch name ('trunk'). ```toml [dependencies] selenium-manager = { git = "https://github.com/SeleniumHQ/selenium", branch = "trunk" } ``` -------------------------------- ### Define and Use CheckboxComponent in Rust Source: https://github.com/vrtgs/thirtyfour/blob/main/docs/src/features/components.md Demonstrates how to define a `CheckboxComponent` to wrap a checkbox element. It includes methods to check if the checkbox is ticked and to tick it if it's clickable and not already ticked. This component uses `WebElement` and `ElementResolver` for element interaction. ```rust /// This component shows how to wrap a simple web component. #[derive(Debug, Clone, Component)] pub struct CheckboxComponent { base: WebElement, // This is the