1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
use std::sync::Arc; use crate::{ common::{command::Command, connection_common::unwrap, keys::TypingData}, error::WebDriverResult, RemoteConnectionAsync, SessionId, }; /// Struct for managing alerts. pub struct Alert { session_id: SessionId, conn: Arc<RemoteConnectionAsync>, } impl Alert { /// Create a new Alert struct. This is typically created internally /// via a call to `WebDriver::switch_to().alert()`. pub fn new(session_id: SessionId, conn: Arc<RemoteConnectionAsync>) -> Self { Alert { session_id, conn } } /// Get the active alert text. /// /// # Example: /// ```rust /// # use thirtyfour::error::WebDriverResult; /// # use thirtyfour::{By, DesiredCapabilities, WebDriver}; /// # use tokio; /// # /// # #[tokio::main] /// # async fn main() -> WebDriverResult<()> { /// # let caps = DesiredCapabilities::chrome(); /// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; /// # driver.get("http://localhost:8000").await?; /// # driver.find_element(By::Id("alertbutton1")).await?.click().await?; /// let alert = driver.switch_to().alert(); /// let text = alert.text().await?; /// # assert_eq!(text, "Alert 1 showing"); /// # alert.dismiss().await?; /// # Ok(()) /// # } /// ``` pub async fn text(&self) -> WebDriverResult<String> { let v = self .conn .execute(Command::GetAlertText(&self.session_id)) .await?; unwrap::<String>(&v["value"]) } /// Dismiss the active alert. /// /// # Example: /// ```rust /// # use thirtyfour::error::WebDriverResult; /// # use thirtyfour::{By, DesiredCapabilities, WebDriver}; /// # use tokio; /// # /// # #[tokio::main] /// # async fn main() -> WebDriverResult<()> { /// # let caps = DesiredCapabilities::chrome(); /// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; /// # driver.get("http://localhost:8000").await?; /// # driver.find_element(By::Id("alertbutton2")).await?.click().await?; /// driver.switch_to().alert().dismiss().await?; /// # let elem = driver.find_element(By::Id("alert-result")).await?; /// # assert_eq!(elem.text().await?, "Alert 2 clicked false"); /// # Ok(()) /// # } /// ``` pub async fn dismiss(&self) -> WebDriverResult<()> { self.conn .execute(Command::DismissAlert(&self.session_id)) .await .map(|_| ()) } /// Accept the active alert. /// /// # Example: /// ```rust /// # use thirtyfour::error::WebDriverResult; /// # use thirtyfour::{By, DesiredCapabilities, WebDriver}; /// # use tokio; /// # /// # #[tokio::main] /// # async fn main() -> WebDriverResult<()> { /// # let caps = DesiredCapabilities::chrome(); /// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; /// # driver.get("http://localhost:8000").await?; /// # driver.find_element(By::Id("alertbutton2")).await?.click().await?; /// driver.switch_to().alert().accept().await?; /// # let elem = driver.find_element(By::Id("alert-result")).await?; /// # assert_eq!(elem.text().await?, "Alert 2 clicked true"); /// # Ok(()) /// # } /// ``` pub async fn accept(&self) -> WebDriverResult<()> { self.conn .execute(Command::AcceptAlert(&self.session_id)) .await .map(|_| ()) } /// Send the specified keys to the active alert. /// /// # Example: /// You can specify anything that implements `Into<TypingData>`. This /// includes &str and String. /// ```rust /// # use thirtyfour::error::WebDriverResult; /// # use thirtyfour::{By, DesiredCapabilities, WebDriver}; /// # use tokio; /// # /// # #[tokio::main] /// # async fn main() -> WebDriverResult<()> { /// # let caps = DesiredCapabilities::chrome(); /// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; /// # driver.get("http://localhost:8000").await?; /// # driver.find_element(By::Id("alertbutton3")).await?.click().await?; /// let alert = driver.switch_to().alert(); /// alert.send_keys("selenium").await?; /// alert.accept().await?; /// # let elem = driver.find_element(By::Id("alert-result")).await?; /// # assert_eq!(elem.text().await?, "selenium"); /// # Ok(()) /// # } /// ``` /// /// You can also send special key combinations like this: /// ```rust /// # use thirtyfour::error::WebDriverResult; /// # use thirtyfour::{By, DesiredCapabilities, Keys, WebDriver}; /// # use tokio; /// # /// # #[tokio::main] /// # async fn main() -> WebDriverResult<()> { /// # let caps = DesiredCapabilities::chrome(); /// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; /// # driver.get("http://localhost:8000").await?; /// # driver.find_element(By::Id("alertbutton3")).await?.click().await?; /// let alert = driver.switch_to().alert(); /// alert.send_keys("selenium").await?; /// alert.send_keys(Keys::Control + "a").await?; /// alert.send_keys("thirtyfour").await?; /// # alert.accept().await?; /// # let elem = driver.find_element(By::Id("alert-result")).await?; /// # assert_eq!(elem.text().await?, "thirtyfour"); /// # Ok(()) /// # } /// ``` pub async fn send_keys<S>(&self, keys: S) -> WebDriverResult<()> where S: Into<TypingData>, { self.conn .execute(Command::SendAlertText(&self.session_id, keys.into())) .await .map(|_| ()) } }