thirtyfour/switch_to.rs
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
use crate::session::handle::SessionHandle;
use crate::WindowHandle;
use crate::{
error::{WebDriverError, WebDriverResult},
Alert, WebElement,
};
/// Struct for switching between frames/windows/alerts.
pub struct SwitchTo {
handle: SessionHandle,
}
impl SwitchTo {
/// Create a new SwitchTo struct. This is typically created internally
/// via a call to `WebDriver::switch_to()`.
pub fn new(handle: SessionHandle) -> Self {
Self {
handle,
}
}
#[deprecated(
since = "0.30.0",
note = "This method has been moved to WebDriver::active_element()"
)]
pub async fn active_element(self) -> WebDriverResult<WebElement> {
let elem = self.handle.client.active_element().await?;
Ok(WebElement::new(elem, self.handle.clone()))
}
#[deprecated(
since = "0.30.0",
note = "This method has been deprecated. See the `Alert` module for new method names"
)]
pub fn alert(self) -> Alert {
Alert::new(self.handle)
}
#[deprecated(
since = "0.30.0",
note = "This method has been moved to WebDriver::enter_default_frame()"
)]
pub async fn default_content(self) -> WebDriverResult<()> {
self.handle.client.enter_frame(None).await?;
Ok(())
}
#[deprecated(since = "0.30.0", note = "This method has been moved to WebDriver::enter_frame()")]
pub async fn frame_number(self, frame_number: u16) -> WebDriverResult<()> {
self.handle.client.enter_frame(Some(frame_number)).await?;
Ok(())
}
#[deprecated(
since = "0.30.0",
note = "This method has been moved to WebElement::enter_frame()"
)]
pub async fn frame_element(self, frame_element: &WebElement) -> WebDriverResult<()> {
let frame = fantoccini::elements::Element::from_element_id(
self.handle.client,
frame_element.element_id(),
);
frame.enter_frame().await?;
Ok(())
}
#[deprecated(
since = "0.30.0",
note = "This method has been moved to WebDriver::enter_parent_frame()"
)]
pub async fn parent_frame(self) -> WebDriverResult<()> {
self.handle.client.enter_parent_frame().await?;
Ok(())
}
#[deprecated(since = "0.30.0", note = "This method has been moved to WebDriver::new_window()")]
pub async fn new_window(self) -> WebDriverResult<WindowHandle> {
let response = self.handle.client.new_window(false).await?;
Ok(response.handle)
}
#[deprecated(since = "0.30.0", note = "This method has been moved to WebDriver::new_tab()")]
pub async fn new_tab(self) -> WebDriverResult<WindowHandle> {
let response = self.handle.client.new_window(true).await?;
Ok(response.handle)
}
#[deprecated(
since = "0.30.0",
note = "This method has been moved to WebDriver::switch_to_window()"
)]
pub async fn window(self, handle: WindowHandle) -> WebDriverResult<()> {
self.handle.client.switch_to_window(handle).await?;
Ok(())
}
#[deprecated(
since = "0.30.0",
note = "This method has been moved to WebDriver::switch_to_named_window()"
)]
pub async fn window_name(self, name: &str) -> WebDriverResult<()> {
let original_handle = self.handle.window().await?;
let handles = self.handle.windows().await?;
for handle in handles {
self.handle.switch_to_window(handle).await?;
let ret = self.handle.execute(r#"return window.name;"#, Vec::new()).await?;
let current_name: String = ret.convert()?;
if current_name == name {
return Ok(());
}
}
self.handle.switch_to_window(original_handle).await?;
Err(WebDriverError::NoSuchWindow(name.to_string()))
}
}
impl SessionHandle {
/// Return the element with focus, or the `<body>` element if nothing has focus.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // If no element has focus, active_element() will return the body tag.
/// let active_elem = driver.active_element().await?;
/// assert_eq!(active_elem.tag_name().await?, "body");
///
/// // Now let's manually focus an element and try active_element() again.
/// let elem = driver.find(By::Id("my-element-id")).await?;
/// elem.focus().await?;
///
/// // And fetch the active element again.
/// let active_elem = driver.active_element().await?;
/// assert_eq!(active_elem.element_id(), elem.element_id());
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn active_element(&self) -> WebDriverResult<WebElement> {
let elem = self.client.active_element().await?;
Ok(WebElement::new(elem, self.clone()))
}
/// Switch to the default frame.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // Enter the first iframe.
/// driver.enter_frame(0).await?;
/// // We are now inside the iframe.
/// driver.find(By::Id("button1")).await?;
/// driver.enter_default_frame().await?;
/// // We are now back in the original window.
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn enter_default_frame(&self) -> WebDriverResult<()> {
self.client.enter_frame(None).await?;
Ok(())
}
/// Switch to an iframe by index. The first iframe on the page has index 0.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // Enter the first iframe.
/// driver.enter_frame(0).await?;
/// // We can now search for elements within the iframe.
/// let elem = driver.find(By::Id("button1")).await?;
/// elem.click().await?;
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn enter_frame(&self, frame_number: u16) -> WebDriverResult<()> {
self.client.enter_frame(Some(frame_number)).await?;
Ok(())
}
/// Switch to the parent frame.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // Find the iframe element and enter the iframe.
/// let elem_iframe = driver.find(By::Id("iframeid1")).await?;
/// elem_iframe.enter_frame().await?;
/// // We can now search for elements within the iframe.
/// let elem = driver.find(By::Id("button1")).await?;
/// elem.click().await?;
/// // Now switch back to the parent frame.
/// driver.enter_parent_frame().await?;
/// // We are now back in the parent document.
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn enter_parent_frame(&self) -> WebDriverResult<()> {
self.client.enter_parent_frame().await?;
Ok(())
}
/// Switch to the specified window.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // Open a new tab.
/// driver.new_tab().await?;
///
/// // Get window handles and switch to the new tab.
/// let handles = driver.windows().await?;
/// driver.switch_to_window(handles[1].clone()).await?;
///
/// // We are now controlling the new tab.
/// driver.goto("https://www.rust-lang.org").await?;
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn switch_to_window(&self, handle: WindowHandle) -> WebDriverResult<()> {
self.client.switch_to_window(handle).await?;
Ok(())
}
/// Switch to the window with the specified name. This uses the `window.name` property.
/// You can set a window name via `WebDriver::set_window_name("someName").await?`.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // Set main window name so we can switch back easily.
/// driver.set_window_name("mywindow").await?;
///
/// // Open a new tab.
/// driver.new_tab().await?;
///
/// // Get window handles and switch to the new tab.
/// let handles = driver.windows().await?;
/// driver.switch_to_window(handles[1].clone()).await?;
///
/// // We are now controlling the new tab.
/// assert_eq!(driver.title().await?, "");
/// driver.switch_to_named_window("mywindow").await?;
///
/// // We are now back in the original tab.
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn switch_to_named_window(&self, name: &str) -> WebDriverResult<()> {
let original_handle = self.window().await?;
let handles = self.windows().await?;
for handle in handles {
self.switch_to_window(handle).await?;
let ret = self.execute(r#"return window.name;"#, Vec::new()).await?;
let current_name: String = ret.convert()?;
if current_name == name {
return Ok(());
}
}
self.switch_to_window(original_handle).await?;
Err(WebDriverError::NoSuchWindow(name.to_string()))
}
/// Switch to a new window.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // Open a new window.
/// let handle = driver.new_window().await?;
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn new_window(&self) -> WebDriverResult<WindowHandle> {
let response = self.client.new_window(false).await?;
Ok(response.handle)
}
/// Switch to a new tab.
///
/// # Example:
/// ```no_run
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// // Open a new tab in the current window.
/// let handle = driver.new_tab().await?;
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub async fn new_tab(&self) -> WebDriverResult<WindowHandle> {
let response = self.client.new_window(true).await?;
Ok(response.handle)
}
}