use anyhow::Result;
use thirtyfour::By;
use crate::finding::{OutOfTime, poll};
use crate::{IntoBy, TestDriver, TestElement};
impl TestDriver {
pub async fn pointer_drag_onto(&self, from: &TestElement, to: &TestElement) -> Result<()> {
from.inner.scroll_into_view().await?;
self.inner
.action_chain()
.move_to_element_center(&from.inner)
.click_and_hold()
.move_by_offset(8, 0)
.release_on_element(&to.inner)
.perform()
.await?;
Ok(())
}
pub async fn release_all_input(&self) -> Result<()> {
if self.holding.take() {
self.inner.action_chain().release().perform().await?;
}
self.inner.action_chain().reset_actions().await?;
Ok(())
}
pub async fn middle_click(&self, element: &TestElement) -> Result<()> {
tracing::trace!("Middle-clicking an element");
let script = "for (const type of ['mousedown', 'mouseup', 'auxclick']) {\
arguments[0].dispatchEvent(new MouseEvent(type, \
{ button: 1, buttons: 4, bubbles: true, cancelable: true })); \
}";
self.inner
.execute(script, vec![element.inner.to_json()?])
.await?;
Ok(())
}
pub async fn middle_drag_by(&self, element: &TestElement, dx: i64) -> Result<()> {
use thirtyfour::common::command::Command;
let actions = serde_json::json!([{
"type": "pointer", "id": "pointer", "parameters": {"pointerType": "mouse"},
"actions": [
{"type": "pointerMove", "duration": 0, "origin": element.inner.to_json()?, "x": 0, "y": 0},
{"type": "pointerDown", "button": 1},
{"type": "pointerMove", "duration": 150, "origin": "pointer", "x": dx, "y": 0},
{"type": "pointerUp", "button": 1}
]
}]);
self.inner
.cmd(Command::PerformActions(actions.into()))
.await?;
Ok(())
}
pub async fn move_held(&self, dx: i64, dy: i64) -> Result<()> {
tracing::trace!(dx, dy, "Carrying a held drag");
self.inner
.action_chain()
.move_by_offset(dx, dy)
.perform()
.await?;
Ok(())
}
pub async fn drop_held(&self) -> Result<()> {
tracing::trace!("Releasing a held drag");
self.inner.action_chain().release().perform().await?;
self.holding.set(false);
Ok(())
}
pub async fn cancel_pointer(&self, element: &TestElement) -> Result<()> {
self.inner
.execute(
"arguments[0].dispatchEvent(new PointerEvent('pointercancel', { bubbles: true }));",
vec![element.inner.to_json()?],
)
.await?;
Ok(())
}
pub async fn drop_held_alt(&self) -> Result<()> {
tracing::trace!("Releasing a held drag and the alt key with it");
self.inner
.action_chain()
.release()
.key_up(thirtyfour::Key::Alt)
.perform()
.await?;
self.holding.set(false);
Ok(())
}
pub async fn press_nth(&self, by: impl IntoBy, index: usize) -> Result<()> {
let by = by.into_by();
tracing::trace!(?by, index, "Pressing element");
self.press_nth_with(by, index, "pressing", async |item| item.click().await)
.await
}
pub async fn context_press_nth(&self, by: impl IntoBy, index: usize) -> Result<()> {
let by = by.into_by();
tracing::trace!(?by, index, "Right-clicking element");
self.press_nth_with(by, index, "right-clicking", async |item| {
item.context_click().await
})
.await
}
async fn press_nth_with(
&self,
by: By,
index: usize,
doing: &str,
press: impl AsyncFn(&TestElement) -> Result<()>,
) -> Result<()> {
let mut refused = None;
let pressed = poll(self.wait_timeout, async || {
if let Some(item) = self.find_all(by.clone()).await?.get(index) {
match press(item).await {
Ok(()) => return Ok(Some(())),
Err(error) => refused = Some(error),
}
}
Ok(None)
})
.await?;
if pressed.is_some() {
return Ok(());
}
let timeout = OutOfTime(format!(
"Timed out after {:?} {doing} {by} at {index}",
self.wait_timeout
));
Err(match refused {
Some(error) => error.context(timeout),
None => timeout.into(),
})
}
}