pub struct SessionHandle {
pub session_id: String,
/* private fields */
}
Expand description
The SessionHandle contains a shared reference to the fantoccini::Client
to allow sending commands to the underlying WebDriver.
Fields§
§session_id: String
Implementations§
Source§impl SessionHandle
impl SessionHandle
Sourcepub async fn get_alert_text(&self) -> WebDriverResult<String>
pub async fn get_alert_text(&self) -> WebDriverResult<String>
Sourcepub async fn dismiss_alert(&self) -> WebDriverResult<()>
pub async fn dismiss_alert(&self) -> WebDriverResult<()>
Sourcepub async fn accept_alert(&self) -> WebDriverResult<()>
pub async fn accept_alert(&self) -> WebDriverResult<()>
Sourcepub async fn send_alert_text(
&self,
keys: impl AsRef<str>,
) -> WebDriverResult<()>
pub async fn send_alert_text( &self, keys: impl AsRef<str>, ) -> WebDriverResult<()>
Send the specified keys to the active alert.
§Example:
You can specify anything that implements Into<TypingData>
. This
includes &str and String.
driver.send_alert_text("selenium").await?;
driver.accept_alert().await?;
You can also send special key combinations like this:
driver.send_alert_text(Key::Control + "a".to_string()).await?;
driver.send_alert_text("thirtyfour").await?;
Source§impl SessionHandle
impl SessionHandle
Sourcepub async fn session_id(&self) -> WebDriverResult<SessionId>
pub async fn session_id(&self) -> WebDriverResult<SessionId>
Get the session ID.
Sourcepub async fn status(&self) -> WebDriverResult<WebDriverStatus>
pub async fn status(&self) -> WebDriverResult<WebDriverStatus>
Get the WebDriver status.
§Example
let caps = DesiredCapabilities::chrome();
let mut driver = WebDriver::new("http://localhost:4444", caps).await?;
let status = driver.status().await?;
Sourcepub async fn close_window(&self) -> WebDriverResult<()>
pub async fn close_window(&self) -> WebDriverResult<()>
Close the current window or tab. This will close the session if no other windows exist.
§Example:
// 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?;
// Close the tab. This will return to the original tab.
driver.close_window().await?;
pub async fn close(&self) -> WebDriverResult<()>
Sourcepub async fn goto<S>(&self, url: S) -> WebDriverResult<()>
pub async fn goto<S>(&self, url: S) -> WebDriverResult<()>
pub async fn get<S>(&self, url: S) -> WebDriverResult<()>
Sourcepub async fn current_url(&self) -> WebDriverResult<Url>
pub async fn current_url(&self) -> WebDriverResult<Url>
Get the current URL.
Sourcepub async fn source(&self) -> WebDriverResult<String>
pub async fn source(&self) -> WebDriverResult<String>
Get the page source as a String.
pub async fn page_source(&self) -> WebDriverResult<String>
Sourcepub async fn title(&self) -> WebDriverResult<String>
pub async fn title(&self) -> WebDriverResult<String>
Get the page title as a String.
Sourcepub async fn find(&self, by: impl Into<By>) -> WebDriverResult<WebElement>
pub async fn find(&self, by: impl Into<By>) -> WebDriverResult<WebElement>
Search for an element on the current page using the specified selector.
NOTE: For more powerful element queries including polling and filters, see the
WebDriver::query
method instead.
§Example:
let elem_button = driver.find(By::Id("my-element-id")).await?;
let elem_text = driver.find(By::Name("my-text-input")).await?;
pub async fn find_element(&self, by: By) -> WebDriverResult<WebElement>
Sourcepub async fn find_all(
&self,
by: impl Into<By>,
) -> WebDriverResult<Vec<WebElement>>
pub async fn find_all( &self, by: impl Into<By>, ) -> WebDriverResult<Vec<WebElement>>
Search for all elements on the current page that match the specified selector.
NOTE: For more powerful element queries including polling and filters, see the
WebDriver::query
method instead.
§Example:
let elems = driver.find_all(By::ClassName("section")).await?;
for elem in elems {
assert!(elem.attr("class").await?.expect("Missing class on element").contains("section"));
}
pub async fn find_elements(&self, by: By) -> WebDriverResult<Vec<WebElement>>
Sourcepub async fn form(&self, by: impl Into<By>) -> WebDriverResult<Form>
pub async fn form(&self, by: impl Into<By>) -> WebDriverResult<Form>
Locate a form on the page.
Through the returned Form
, HTML forms can be filled out and submitted.
Sourcepub async fn execute(
&self,
script: &str,
args: Vec<Value>,
) -> WebDriverResult<ScriptRet>
pub async fn execute( &self, script: &str, args: Vec<Value>, ) -> WebDriverResult<ScriptRet>
Execute the specified Javascript synchronously and return the result.
§Example:
let ret = driver.execute(r#"
let elem = document.getElementById("button1");
elem.click();
return elem;
"#, Vec::new()
).await?;
let elem_out: WebElement = ret.element()?;
To supply an element as an input argument to a script, use
WebElement::to_json
as follows:
§Example:
let elem = driver.find(By::Id("button1")).await?;
let ret = driver.execute(r#"
arguments[0].innerHTML = arguments[1];
return arguments[0];
"#, vec![elem.to_json()?, serde_json::to_value("TESTING")?]
).await?;
let elem_out = ret.element()?;
assert_eq!(elem_out.element_id(), elem.element_id());
assert_eq!(elem_out.text().await?, "TESTING");
pub async fn execute_script( &self, script: &str, args: Vec<Value>, ) -> WebDriverResult<ScriptRet>
Sourcepub async fn execute_async(
&self,
script: &str,
args: Vec<Value>,
) -> WebDriverResult<ScriptRet>
pub async fn execute_async( &self, script: &str, args: Vec<Value>, ) -> WebDriverResult<ScriptRet>
Execute the specified Javascrypt asynchronously and return the result.
§Example:
let ret = driver.execute_async(r#"
// Selenium automatically provides an extra argument which is a
// function that receives the return value(s).
let done = arguments[0];
window.setTimeout(() => {
let elem = document.getElementById("button1");
elem.click();
done(elem);
}, 1000);
"#, Vec::new()
).await?;
let elem_out: WebElement = ret.element()?;
To supply an element as an input argument to a script, use
WebElement::to_json
as follows:
§Example:
let elem = driver.find(By::Id("button1")).await?;
let args = vec![elem.to_json()?, serde_json::to_value("TESTING")?];
let ret = driver.execute_async(r#"
// Selenium automatically provides an extra argument which is a
// function that receives the return value(s).
let done = arguments[2];
window.setTimeout(() => {
arguments[0].innerHTML = arguments[1];
done(arguments[0]);
}, 1000);
"#, args
).await?;
let elem_out = ret.element()?;
assert_eq!(elem_out.element_id(), elem.element_id());
assert_eq!(elem_out.text().await?, "TESTING");
pub async fn execute_script_async( &self, script: &str, args: Vec<Value>, ) -> WebDriverResult<ScriptRet>
Sourcepub async fn window(&self) -> WebDriverResult<WindowHandle>
pub async fn window(&self) -> WebDriverResult<WindowHandle>
Get the current window handle.
§Example:
// Get the current window handle.
let handle = driver.window().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?;
assert_ne!(driver.window().await?, handle);
// Switch back to original tab.
driver.switch_to_window(handle.clone()).await?;
assert_eq!(driver.window().await?, handle);
pub async fn current_window_handle(&self) -> WebDriverResult<WindowHandle>
Sourcepub async fn windows(&self) -> WebDriverResult<Vec<WindowHandle>>
pub async fn windows(&self) -> WebDriverResult<Vec<WindowHandle>>
Get all window handles for the current session.
§Example:
assert_eq!(driver.windows().await?.len(), 1);
// Open a new tab.
driver.new_tab().await?;
// Get window handles and switch to the new tab.
let handles = driver.windows().await?;
assert_eq!(handles.len(), 2);
driver.switch_to_window(handles[1].clone()).await?;
pub async fn window_handles(&self) -> WebDriverResult<Vec<WindowHandle>>
Sourcepub async fn maximize_window(&self) -> WebDriverResult<()>
pub async fn maximize_window(&self) -> WebDriverResult<()>
Sourcepub async fn minimize_window(&self) -> WebDriverResult<()>
pub async fn minimize_window(&self) -> WebDriverResult<()>
Sourcepub async fn fullscreen_window(&self) -> WebDriverResult<()>
pub async fn fullscreen_window(&self) -> WebDriverResult<()>
Sourcepub async fn get_window_rect(&self) -> WebDriverResult<Rect>
pub async fn get_window_rect(&self) -> WebDriverResult<Rect>
Get the current window rectangle, in pixels.
The returned Rect struct has members x
, y
, width
, height
,
all i32.
§Example:
use thirtyfour::Rect;
driver.set_window_rect(0, 0, 600, 400).await?;
let rect = driver.get_window_rect().await?;
assert_eq!(rect, Rect::new(0, 0, 600, 400));
Sourcepub async fn set_window_rect(
&self,
x: u32,
y: u32,
width: u32,
height: u32,
) -> WebDriverResult<()>
pub async fn set_window_rect( &self, x: u32, y: u32, width: u32, height: u32, ) -> WebDriverResult<()>
Set the current window rectangle, in pixels.
driver.set_window_rect(0, 0, 500, 400).await?;
Sourcepub async fn back(&self) -> WebDriverResult<()>
pub async fn back(&self) -> WebDriverResult<()>
Sourcepub async fn forward(&self) -> WebDriverResult<()>
pub async fn forward(&self) -> WebDriverResult<()>
Go forward. This is equivalent to clicking the browser’s forward button.
§Example:
let caps = DesiredCapabilities::chrome();
driver.forward().await?;
Sourcepub async fn refresh(&self) -> WebDriverResult<()>
pub async fn refresh(&self) -> WebDriverResult<()>
Sourcepub async fn get_timeouts(&self) -> WebDriverResult<TimeoutConfiguration>
pub async fn get_timeouts(&self) -> WebDriverResult<TimeoutConfiguration>
Get all timeouts for the current session.
§Example:
use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
let timeouts = driver.get_timeouts().await?;
println!("Page load timeout = {:?}", timeouts.page_load());
Sourcepub async fn update_timeouts(
&self,
timeouts: TimeoutConfiguration,
) -> WebDriverResult<()>
pub async fn update_timeouts( &self, timeouts: TimeoutConfiguration, ) -> WebDriverResult<()>
Set all timeouts for the current session.
NOTE: Setting the implicit wait timeout to a non-zero value will interfere with the use
of WebDriver::query
and WebElement::wait_until
.
It is therefore recommended to use these methods (which provide polling
and explicit waits) instead rather than increasing the implicit wait timeout.
§Example:
use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
// Setting timeouts to None means those timeout values will not be updated.
let timeouts = TimeoutConfiguration::new(None, Some(Duration::new(11, 0)), None);
driver.update_timeouts(timeouts).await?;
pub async fn set_timeouts( &self, timeouts: TimeoutConfiguration, ) -> WebDriverResult<()>
Sourcepub async fn set_implicit_wait_timeout(
&self,
time_to_wait: Duration,
) -> WebDriverResult<()>
pub async fn set_implicit_wait_timeout( &self, time_to_wait: Duration, ) -> WebDriverResult<()>
Set the implicit wait timeout.
This is how long the WebDriver will wait when querying elements. By default this is set to 0 seconds.
NOTE: Setting the implicit wait timeout to a non-zero value will interfere with the use
of WebDriver::query
and WebElement::wait_until
.
It is therefore recommended to use these methods (which provide polling
and explicit waits) instead rather than increasing the implicit wait timeout.
§Example:
use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
let delay = Duration::new(11, 0);
driver.set_implicit_wait_timeout(delay).await?;
Sourcepub async fn set_script_timeout(
&self,
time_to_wait: Duration,
) -> WebDriverResult<()>
pub async fn set_script_timeout( &self, time_to_wait: Duration, ) -> WebDriverResult<()>
Set the script timeout.
This is how long the WebDriver will wait for a Javascript script to execute. By default this is set to 60 seconds.
§Example:
use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
let delay = Duration::new(11, 0);
driver.set_script_timeout(delay).await?;
Sourcepub async fn set_page_load_timeout(
&self,
time_to_wait: Duration,
) -> WebDriverResult<()>
pub async fn set_page_load_timeout( &self, time_to_wait: Duration, ) -> WebDriverResult<()>
Set the page load timeout.
This is how long the WebDriver will wait for the page to finish loading. By default this is set to 60 seconds.
§Example:
use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
let delay = Duration::new(11, 0);
driver.set_page_load_timeout(delay).await?;
Sourcepub fn action_chain(&self) -> ActionChain
pub fn action_chain(&self) -> ActionChain
Create a new action chain for this session.
Action chains can be used to simulate more complex user input actions involving key combinations, mouse movements, mouse click, right-click, and more.
§Example:
let elem_text = driver.find(By::Name("input1")).await?;
let elem_button = driver.find(By::Id("button-set")).await?;
driver.action_chain()
.send_keys_to_element(&elem_text, "thirtyfour")
.move_to_element_center(&elem_button)
.click()
.perform()
.await?;
Sourcepub async fn perform_actions(
&self,
actions: impl Into<Actions>,
) -> WebDriverResult<()>
pub async fn perform_actions( &self, actions: impl Into<Actions>, ) -> WebDriverResult<()>
Create a new Actions chain.
Also see WebDriver::action_chain
for a builder-based alternative.
let mouse_actions = MouseActions::new("mouse")
.then(PointerAction::Down {
button: MOUSE_BUTTON_LEFT,
})
.then(PointerAction::MoveBy {
duration: Some(Duration::from_secs(2)),
x: 100,
y: 0,
})
.then(PointerAction::Up {
button: MOUSE_BUTTON_LEFT,
});
client.perform_actions(mouse_actions).await?;
See the documentation for Actions
for more information.
Perform the specified input actions.
Get all cookies.
§Example:
use thirtyfour::cookie::SameSite;
let cookies = driver.get_all_cookies().await?;
for cookie in &cookies {
println!("Got cookie: {}", cookie.value());
}
Get the specified cookie.
§Example:
use thirtyfour::cookie::SameSite;
let cookie = driver.get_named_cookie("key").await?;
println!("Got cookie: {}", cookie.value());
Delete the specified cookie.
§Example:
use thirtyfour::cookie::SameSite;
driver.delete_cookie("key").await?;
Add the specified cookie.
§Example:
use thirtyfour::cookie::SameSite;
driver.goto("https://wikipedia.org").await?;
let mut cookie = Cookie::new("key", "value");
cookie.set_domain("wikipedia.org");
cookie.set_path("/");
cookie.set_same_site(Some(SameSite::Lax));
driver.add_cookie(cookie.clone()).await?;
Sourcepub async fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>
pub async fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>
Take a screenshot of the current window and return it as PNG bytes.
Sourcepub async fn screenshot(&self, path: &Path) -> WebDriverResult<()>
pub async fn screenshot(&self, path: &Path) -> WebDriverResult<()>
Take a screenshot of the current window and write it to the specified filename.
Sourcepub fn switch_to(&self) -> SwitchTo
👎Deprecated since 0.30.0: SwitchTo has been deprecated. Use WebDriver::switch_to_*() methods instead
pub fn switch_to(&self) -> SwitchTo
Return a SwitchTo struct for switching to another window or frame.
Sourcepub async fn set_window_name(&self, window_name: &str) -> WebDriverResult<()>
pub async fn set_window_name(&self, window_name: &str) -> WebDriverResult<()>
Set the current window name.
Useful for switching between windows/tabs using WebDriver::switch_to_named_window
§Example:
// Get the current window handle.
let handle = driver.window().await?;
driver.set_window_name("main").await?;
// Open a new tab.
let new_handle = driver.new_tab().await?;
// Get window handles and switch to the new tab.
driver.switch_to_window(new_handle).await?;
// We are now controlling the new tab.
driver.goto("https://www.rust-lang.org").await?;
assert_ne!(driver.window().await?, handle);
// Switch back to original tab using window name.
driver.switch_to_named_window("main").await?;
assert_eq!(driver.window().await?, handle);
Sourcepub async fn in_new_tab<F, Fut, T>(&self, f: F) -> WebDriverResult<T>
pub async fn in_new_tab<F, Fut, T>(&self, f: F) -> WebDriverResult<T>
Execute the specified function in a new browser tab, closing the tab when complete.
The return value will be that of the supplied function, unless an error occurs while opening or closing the tab.
let window_title = driver.in_new_tab(|| async {
driver.goto("https://www.google.com").await?;
driver.title().await
}).await?;
assert_eq!(window_title, "Google");
Source§impl SessionHandle
impl SessionHandle
Sourcepub async fn active_element(&self) -> WebDriverResult<WebElement>
pub async fn active_element(&self) -> WebDriverResult<WebElement>
Return the element with focus, or the <body>
element if nothing has focus.
§Example:
// 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());
Sourcepub async fn enter_default_frame(&self) -> WebDriverResult<()>
pub async fn enter_default_frame(&self) -> WebDriverResult<()>
Switch to the default frame.
§Example:
// 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.
Sourcepub async fn enter_frame(&self, frame_number: u16) -> WebDriverResult<()>
pub async fn enter_frame(&self, frame_number: u16) -> WebDriverResult<()>
Switch to an iframe by index. The first iframe on the page has index 0.
§Example:
// 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?;
Sourcepub async fn enter_parent_frame(&self) -> WebDriverResult<()>
pub async fn enter_parent_frame(&self) -> WebDriverResult<()>
Switch to the parent frame.
§Example:
// 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.
Sourcepub async fn switch_to_window(
&self,
handle: WindowHandle,
) -> WebDriverResult<()>
pub async fn switch_to_window( &self, handle: WindowHandle, ) -> WebDriverResult<()>
Switch to the specified window.
§Example:
// 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?;
Sourcepub async fn switch_to_named_window(&self, name: &str) -> WebDriverResult<()>
pub async fn switch_to_named_window(&self, name: &str) -> WebDriverResult<()>
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:
// 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.
Sourcepub async fn new_window(&self) -> WebDriverResult<WindowHandle>
pub async fn new_window(&self) -> WebDriverResult<WindowHandle>
Sourcepub async fn new_tab(&self) -> WebDriverResult<WindowHandle>
pub async fn new_tab(&self) -> WebDriverResult<WindowHandle>
Switch to a new tab.
§Example:
// Open a new tab in the current window.
let handle = driver.new_tab().await?;
Trait Implementations§
Source§impl Clone for SessionHandle
impl Clone for SessionHandle
Source§fn clone(&self) -> SessionHandle
fn clone(&self) -> SessionHandle
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for SessionHandle
impl Debug for SessionHandle
Source§impl ElementQueryable for SessionHandle
impl ElementQueryable for SessionHandle
Source§fn query(&self, by: By) -> ElementQuery
fn query(&self, by: By) -> ElementQuery
Return an ElementQuery instance for more executing powerful element queries.
This uses the builder pattern to construct queries that will return one or more elements, depending on the method specified at the end of the chain.
See ElementQuery
for more documentation.