thirtyfour/alert.rs
1use crate::common::command::Command;
2use crate::error::WebDriverResult;
3use crate::session::handle::SessionHandle;
4use crate::TypingData;
5use std::sync::Arc;
6
7/// Struct for managing alerts.
8#[derive(Debug)]
9pub struct Alert {
10 handle: Arc<SessionHandle>,
11}
12
13impl Alert {
14 /// Create a new Alert struct. This is typically created internally
15 /// via a call to `WebDriver::switch_to().alert()`.
16 pub fn new(handle: Arc<SessionHandle>) -> Self {
17 Self {
18 handle,
19 }
20 }
21
22 /// Get the text of the active alert if there is one.
23 #[deprecated(
24 since = "0.30.0",
25 note = "This method has been moved to WebDriver::get_alert_text()"
26 )]
27 pub async fn text(&self) -> WebDriverResult<String> {
28 self.handle.get_alert_text().await
29 }
30
31 /// Dismiss the active alert if there is one.
32 #[deprecated(
33 since = "0.30.0",
34 note = "This method has been moved to WebDriver::dismiss_alert()"
35 )]
36 pub async fn dismiss(&self) -> WebDriverResult<()> {
37 self.handle.dismiss_alert().await
38 }
39
40 /// Accept the active alert if there is one.
41 #[deprecated(
42 since = "0.30.0",
43 note = "This method has been moved to WebDriver::accept_alert()"
44 )]
45 pub async fn accept(&self) -> WebDriverResult<()> {
46 self.handle.accept_alert().await
47 }
48
49 /// Send the specified text to the active alert if there is one.
50 #[deprecated(
51 since = "0.30.0",
52 note = "This method has been moved to WebDriver::send_alert_text()"
53 )]
54 pub async fn send_keys(&self, keys: impl Into<TypingData>) -> WebDriverResult<()> {
55 self.handle.send_alert_text(keys.into()).await
56 }
57}
58
59impl SessionHandle {
60 /// Get the active alert text.
61 ///
62 /// # Example:
63 /// ```no_run
64 /// # use thirtyfour::prelude::*;
65 /// # use thirtyfour::support::block_on;
66 /// #
67 /// # fn main() -> WebDriverResult<()> {
68 /// # block_on(async {
69 /// # let caps = DesiredCapabilities::chrome();
70 /// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
71 /// let text = driver.get_alert_text().await?;
72 /// # driver.quit().await?;
73 /// # Ok(())
74 /// # })
75 /// # }
76 /// ```
77 pub async fn get_alert_text(&self) -> WebDriverResult<String> {
78 self.cmd(Command::GetAlertText).await?.value::<String>()
79 }
80
81 /// Dismiss the active alert.
82 ///
83 /// # Example:
84 /// ```no_run
85 /// # use thirtyfour::prelude::*;
86 /// # use thirtyfour::support::block_on;
87 /// #
88 /// # fn main() -> WebDriverResult<()> {
89 /// # block_on(async {
90 /// # let caps = DesiredCapabilities::chrome();
91 /// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
92 /// driver.dismiss_alert().await?;
93 /// # driver.quit().await?;
94 /// # Ok(())
95 /// # })
96 /// # }
97 /// ```
98 pub async fn dismiss_alert(&self) -> WebDriverResult<()> {
99 self.cmd(Command::DismissAlert).await?;
100 Ok(())
101 }
102
103 /// Accept the active alert.
104 ///
105 /// # Example:
106 /// ```no_run
107 /// # use thirtyfour::prelude::*;
108 /// # use thirtyfour::support::block_on;
109 /// #
110 /// # fn main() -> WebDriverResult<()> {
111 /// # block_on(async {
112 /// # let caps = DesiredCapabilities::chrome();
113 /// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
114 /// driver.accept_alert().await?;
115 /// # driver.quit().await?;
116 /// # Ok(())
117 /// # })
118 /// # }
119 /// ```
120 pub async fn accept_alert(&self) -> WebDriverResult<()> {
121 self.cmd(Command::AcceptAlert).await?;
122 Ok(())
123 }
124
125 /// Send the specified keys to the active alert.
126 ///
127 /// # Example:
128 /// You can specify anything that implements `Into<TypingData>`. This
129 /// includes &str and String.
130 /// ```no_run
131 /// # use thirtyfour::prelude::*;
132 /// # use thirtyfour::support::block_on;
133 /// #
134 /// # fn main() -> WebDriverResult<()> {
135 /// # block_on(async {
136 /// # let caps = DesiredCapabilities::chrome();
137 /// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
138 /// driver.send_alert_text("selenium").await?;
139 /// driver.accept_alert().await?;
140 /// # driver.quit().await?;
141 /// # Ok(())
142 /// # })
143 /// # }
144 /// ```
145 ///
146 /// You can also send special key combinations like this:
147 /// ```no_run
148 /// # use thirtyfour::prelude::*;
149 /// # use thirtyfour::support::block_on;
150 /// #
151 /// # fn main() -> WebDriverResult<()> {
152 /// # block_on(async {
153 /// # let caps = DesiredCapabilities::chrome();
154 /// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
155 /// driver.send_alert_text(Key::Control + "a").await?;
156 /// driver.send_alert_text("thirtyfour").await?;
157 /// # driver.quit().await?;
158 /// # Ok(())
159 /// # })
160 /// # }
161 /// ```
162 pub async fn send_alert_text(&self, keys: impl Into<TypingData>) -> WebDriverResult<()> {
163 self.cmd(Command::SendAlertText(keys.into())).await?;
164 Ok(())
165 }
166}