pub struct ActionChain { /* private fields */ }
Expand description
The ActionChain struct allows you to perform multiple input actions in a sequence, including drag-and-drop, send keystrokes to an element, and hover the mouse over an element.
The easiest way to construct an ActionChain struct is via the WebDriver struct.
§Example:
driver
.action_chain()
.drag_and_drop_element(elem_src, elem_target)
.perform()
.await?;
Implementations§
Source§impl ActionChain
impl ActionChain
Sourcepub fn new(handle: SessionHandle) -> Self
pub fn new(handle: SessionHandle) -> Self
Create a new ActionChain struct.
See WebDriver::action_chain() for more details.
Sourcepub async fn reset_actions(&self) -> WebDriverResult<()>
pub async fn reset_actions(&self) -> WebDriverResult<()>
Reset all actions, reverting all input devices back to default states.
§Example:
// Hold mouse button down on element.
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.click_and_hold_element(&elem)
.perform()
.await?;
// Now reset all actions.
driver.action_chain().reset_actions().await?;
// Mouse button is now released.
Sourcepub async fn perform(self) -> WebDriverResult<()>
pub async fn perform(self) -> WebDriverResult<()>
Perform the action sequence. No actions are actually performed until this method is called.
Sourcepub fn click(self) -> Self
pub fn click(self) -> Self
Click and release the left mouse button.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.move_to_element_center(&elem)
.click()
.perform()
.await?;
Sourcepub fn click_element(self, element: &WebElement) -> Self
pub fn click_element(self, element: &WebElement) -> Self
Click on the specified element using the left mouse button and release.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.click_element(&elem)
.perform()
.await?;
Sourcepub fn click_and_hold(self) -> Self
pub fn click_and_hold(self) -> Self
Click the left mouse button and hold it down.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.move_to_element_center(&elem)
.click_and_hold()
.perform()
.await?;
Sourcepub fn click_and_hold_element(self, element: &WebElement) -> Self
pub fn click_and_hold_element(self, element: &WebElement) -> Self
Click on the specified element using the left mouse button and hold the button down.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.click_and_hold_element(&elem)
.perform()
.await?;
Sourcepub fn context_click(self) -> Self
pub fn context_click(self) -> Self
Click and release the right mouse button.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.move_to_element_center(&elem)
.context_click()
.perform()
.await?;
Sourcepub fn context_click_element(self, element: &WebElement) -> Self
pub fn context_click_element(self, element: &WebElement) -> Self
Click on the specified element using the right mouse button and release.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.context_click_element(&elem)
.perform()
.await?;
Sourcepub fn double_click(self) -> Self
pub fn double_click(self) -> Self
Double-click the left mouse button.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.move_to_element_center(&elem)
.double_click()
.perform()
.await?;
Sourcepub fn double_click_element(self, element: &WebElement) -> Self
pub fn double_click_element(self, element: &WebElement) -> Self
Double-click on the specified element.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.double_click_element(&elem)
.perform()
.await?;
Sourcepub fn drag_and_drop_element(
self,
source: &WebElement,
target: &WebElement,
) -> Self
pub fn drag_and_drop_element( self, source: &WebElement, target: &WebElement, ) -> Self
Drag the mouse cursor from the center of the source element to the center of the target element.
This method is not working correctly due to a selenium bug
It appears selenium has a bug in the drag and drop feature causing it to start the drag but not perform the drop. See https://github.com/SeleniumHQ/selenium/issues/8003
This method has been confirmed to produce identical JSON output compared to the python selenium library (which also fails due to the same bug).
Sourcepub fn drag_and_drop_by_offset(self, x_offset: i64, y_offset: i64) -> Self
pub fn drag_and_drop_by_offset(self, x_offset: i64, y_offset: i64) -> Self
Drag the mouse cursor by the specified X and Y offsets.
This method is not working correctly due to a selenium bug
It appears selenium has a bug in the drag and drop feature causing it to start the drag but not perform the drop. See https://github.com/SeleniumHQ/selenium/issues/8003
This method has been confirmed to produce identical JSON output compared to the python selenium library (which also fails due to the same bug).
Sourcepub fn drag_and_drop_element_by_offset(
self,
element: &WebElement,
x_offset: i64,
y_offset: i64,
) -> Self
pub fn drag_and_drop_element_by_offset( self, element: &WebElement, x_offset: i64, y_offset: i64, ) -> Self
Drag the mouse cursor by the specified X and Y offsets, starting from the center of the specified element.
This method is not working correctly due to a selenium bug
It appears selenium has a bug in the drag and drop feature causing it to start the drag but not perform the drop. See https://github.com/SeleniumHQ/selenium/issues/8003
This method has been confirmed to produce identical JSON output compared to the python selenium library (which also fails due to the same bug).
Sourcepub fn key_down(self, value: impl Into<char>) -> Self
pub fn key_down(self, value: impl Into<char>) -> Self
Press the specified key down.
§Example:
let elem = driver.find(By::Name("input1")).await?;
driver
.action_chain()
.click_element(&elem)
.key_down('a')
.perform().await?;
Sourcepub fn key_down_on_element(
self,
element: &WebElement,
value: impl Into<char>,
) -> Self
pub fn key_down_on_element( self, element: &WebElement, value: impl Into<char>, ) -> Self
Click the specified element and then press the specified key down.
§Example:
let elem = driver.find(By::Name("input1")).await?;
driver
.action_chain()
.key_down_on_element(&elem, 'a')
.perform()
.await?;
Sourcepub fn key_up(self, value: impl Into<char>) -> Self
pub fn key_up(self, value: impl Into<char>) -> Self
Release the specified key. This usually follows a key_down()
action.
§Example:
let elem = driver.find(By::Name("input1")).await?;
elem.send_keys("selenium").await?;
driver
.action_chain()
.key_down_on_element(&elem, Key::Control)
.key_down('a')
.key_up(Key::Control)
.key_up('a')
.key_down('b')
.perform()
.await?;
Sourcepub fn key_up_on_element(
self,
element: &WebElement,
value: impl Into<char>,
) -> Self
pub fn key_up_on_element( self, element: &WebElement, value: impl Into<char>, ) -> Self
Click the specified element and release the specified key.
§Example:
let elem = driver.find(By::Name("input1")).await?;
elem.send_keys("selenium").await?;
assert_eq!(elem.value().await?.unwrap(), "selenium");
driver
.action_chain()
.key_down_on_element(&elem, Key::Control)
.key_down('a')
.key_up_on_element(&elem, 'a')
.key_up_on_element(&elem, Key::Control)
.key_down('b')
.perform()
.await?;
assert_eq!(elem.value().await?.unwrap(), "b");
Sourcepub fn move_to(self, x: i64, y: i64) -> Self
pub fn move_to(self, x: i64, y: i64) -> Self
Move the mouse cursor to the specified X and Y coordinates.
§Example:
let elem = driver.find(By::Id("button1")).await?;
let center = elem.rect().await?.icenter();
driver
.action_chain()
.move_to(center.0, center.1)
.click()
.perform()
.await?;
Sourcepub fn move_by_offset(self, x_offset: i64, y_offset: i64) -> Self
pub fn move_by_offset(self, x_offset: i64, y_offset: i64) -> Self
Move the mouse cursor by the specified X and Y offsets.
§Example:
let elem1 = driver.find(By::Id("button1")).await?;
let elem2 = driver.find(By::Id("button2")).await?;
// We will calculate the distance between the two center points and then
// use action_chain() to move to the second button before clicking.
let offset = elem2.rect().await?.center().0 as i64 - elem1.rect().await?.center().0 as i64;
driver
.action_chain()
.move_to_element_center(&elem1)
.move_by_offset(offset, 0)
.click()
.perform()
.await?;
Sourcepub fn move_to_element_center(self, element: &WebElement) -> Self
pub fn move_to_element_center(self, element: &WebElement) -> Self
Move the mouse cursor to the center of the specified element.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.move_to_element_center(&elem)
.click()
.perform()
.await?;
Sourcepub fn move_to_element_with_offset(
self,
element: &WebElement,
x_offset: i64,
y_offset: i64,
) -> Self
pub fn move_to_element_with_offset( self, element: &WebElement, x_offset: i64, y_offset: i64, ) -> Self
Move the mouse cursor to the specified offsets relative to the specified element’s center position.
§Example:
// Select the text in the source element and copy it to the clipboard.
let elem = driver.find(By::Id("button-result")).await?;
let width = elem.rect().await?.width;
driver
.action_chain()
.move_to_element_with_offset(&elem, (-(width as f64) / 2.0) as i64, 0)
.drag_and_drop_by_offset(width as i64, 0)
.key_down(Key::Control)
.key_down('c')
.key_up('c')
.key_up(Key::Control)
.perform()
.await?;
Sourcepub fn release(self) -> Self
pub fn release(self) -> Self
Release the left mouse button.
§Example:
let elem = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.click_and_hold_element(&elem)
.release()
.perform()
.await?;
Sourcepub fn release_on_element(self, element: &WebElement) -> Self
pub fn release_on_element(self, element: &WebElement) -> Self
Move the mouse to the specified element and release the mouse button.
§Example:
let elem1 = driver.find(By::Id("source-element-id")).await?;
let elem2 = driver.find(By::Id("target-element-id")).await?;
driver
.action_chain()
.click_and_hold_element(&elem1)
.release_on_element(&elem2)
.perform()
.await?;
Sourcepub fn send_keys(self, text: impl AsRef<str>) -> Self
pub fn send_keys(self, text: impl AsRef<str>) -> Self
Send the specified keystrokes to the active element.
§Example:
use thirtyfour::Key;
let elem = driver.find(By::Name("input1")).await?;
let button = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.click_element(&elem)
.send_keys("selenium")
.click_element(&button)
.perform()
.await?;
Sourcepub fn send_keys_to_element(
self,
element: &WebElement,
text: impl AsRef<str>,
) -> Self
pub fn send_keys_to_element( self, element: &WebElement, text: impl AsRef<str>, ) -> Self
Click on the specified element and send the specified keystrokes.
§Example:
let elem = driver.find(By::Name("input1")).await?;
let button = driver.find(By::Id("button1")).await?;
driver
.action_chain()
.send_keys_to_element(&elem, "selenium")
.click_element(&button)
.perform()
.await?;