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
use crate::webdrivercommands::WebDriverCommands;
use crate::WebDriverSession;
use crate::{
common::{command::Command, connection_common::convert_json, keys::TypingData},
error::WebDriverResult,
};
/// Struct for managing alerts.
pub struct Alert<'a> {
session: &'a WebDriverSession,
}
impl<'a> Alert<'a> {
/// Create a new Alert struct. This is typically created internally
/// via a call to `WebDriver::switch_to().alert()`.
pub fn new(session: &'a WebDriverSession) -> Self {
Alert {
session,
}
}
///Convenience wrapper for executing a WebDriver command.
fn cmd(&self, command: Command) -> WebDriverResult<serde_json::Value> {
self.session.cmd(command)
}
/// Get the active alert text.
///
/// # Example:
/// ```rust
/// # use thirtyfour_sync::prelude::*;
/// # fn main() -> WebDriverResult<()> {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps)?;
/// # driver.get("http://webappdemo")?;
/// # driver.find_element(By::Id("pagealerts"))?.click()?;
/// # driver.find_element(By::Id("alertbutton1"))?.click()?;
/// let alert = driver.switch_to().alert();
/// let text = alert.text()?;
/// # assert_eq!(text, "Alert 1 showing");
/// # alert.dismiss()?;
/// # driver.quit()?;
/// # Ok(())
/// # }
/// ```
pub fn text(&self) -> WebDriverResult<String> {
let v = self.cmd(Command::GetAlertText)?;
convert_json::<String>(&v["value"])
}
/// Dismiss the active alert.
///
/// # Example:
/// ```rust
/// # use thirtyfour_sync::prelude::*;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps)?;
/// # driver.get("http://webappdemo")?;
/// # driver.find_element(By::Id("pagealerts"))?.click()?;
/// # driver.find_element(By::Id("alertbutton2"))?.click()?;
/// driver.switch_to().alert().dismiss()?;
/// # let elem = driver.find_element(By::Id("alert-result"))?;
/// # assert_eq!(elem.text()?, "Alert 2 clicked false");
/// # driver.quit()?;
/// # Ok(())
/// # }
/// ```
pub fn dismiss(&self) -> WebDriverResult<()> {
self.cmd(Command::DismissAlert).map(|_| ())
}
/// Accept the active alert.
///
/// # Example:
/// ```rust
/// # use thirtyfour_sync::prelude::*;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps)?;
/// # driver.get("http://webappdemo")?;
/// # driver.find_element(By::Id("pagealerts"))?.click()?;
/// # driver.find_element(By::Id("alertbutton2"))?.click()?;
/// driver.switch_to().alert().accept()?;
/// # let elem = driver.find_element(By::Id("alert-result"))?;
/// # assert_eq!(elem.text()?, "Alert 2 clicked true");
/// # driver.quit()?;
/// # Ok(())
/// # }
/// ```
pub fn accept(&self) -> WebDriverResult<()> {
self.cmd(Command::AcceptAlert).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_sync::prelude::*;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps)?;
/// # driver.get("http://webappdemo")?;
/// # driver.find_element(By::Id("pagealerts"))?.click()?;
/// # driver.find_element(By::Id("alertbutton3"))?.click()?;
/// let alert = driver.switch_to().alert();
/// alert.send_keys("selenium")?;
/// alert.accept()?;
/// # let elem = driver.find_element(By::Id("alert-result"))?;
/// # assert_eq!(elem.text()?, "selenium");
/// # driver.quit()?;
/// # Ok(())
/// # }
/// ```
///
/// You can also send special key combinations like this:
/// ```rust
/// # use thirtyfour_sync::prelude::*;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps)?;
/// # driver.get("http://webappdemo")?;
/// # driver.find_element(By::Id("pagealerts"))?.click()?;
/// # driver.find_element(By::Id("alertbutton3"))?.click()?;
/// let alert = driver.switch_to().alert();
/// alert.send_keys("selenium")?;
/// alert.send_keys(Keys::Control + "a")?;
/// alert.send_keys("thirtyfour")?;
/// # alert.accept()?;
/// # let elem = driver.find_element(By::Id("alert-result"))?;
/// # assert_eq!(elem.text()?, "thirtyfour");
/// # driver.quit()?;
/// # Ok(())
/// # }
/// ```
pub fn send_keys<S>(&self, keys: S) -> WebDriverResult<()>
where
S: Into<TypingData>,
{
self.cmd(Command::SendAlertText(keys.into())).map(|_| ())
}
}