mod common;
use std::time::Duration;
use assert_matches::assert_matches;
use common::*;
use rstest::rstest;
use thirtyfour::components::ElementResolverSingle;
use thirtyfour::error::WebDriverErrorInner;
use thirtyfour::prelude::*;
use thirtyfour::support::block_on;
#[rstest]
fn query_absence_polling(test_harness: TestHarness) -> WebDriverResult<()> {
let c = test_harness.driver();
block_on(async {
c.goto(sample_page_url()).await?;
assert!(
!c.query(By::Id("doesnotexist")).or(By::Id("navigation")).nowait().not_exists().await?
);
let error = c
.query(By::Id("doesnotexist"))
.or(By::Id("navigation"))
.desc("navigation alternatives")
.nowait()
.wait_until_gone()
.await
.expect_err("the navigation branch still matches");
let message = error.to_string();
assert!(message.contains("navigation alternatives"));
assert!(message.contains("Id(doesnotexist)"));
assert!(message.contains("Id(navigation)"));
assert_matches!(error.into_inner(), WebDriverErrorInner::Timeout(_));
c.execute("setTimeout(() => document.getElementById('navigation').remove(), 100);", vec![])
.await?;
assert!(
c.query(By::Id("navigation"))
.wait(Duration::from_secs(2), Duration::from_millis(20))
.not_exists()
.await?
);
c.execute(
"const footer = document.getElementById('footer'); \
footer.classList.add('temporary-match'); \
setTimeout(() => footer.classList.remove('temporary-match'), 100);",
vec![],
)
.await?;
assert!(
c.query(By::Id("footer"))
.with_class("temporary-match")
.wait(Duration::from_secs(2), Duration::from_millis(20))
.not_exists()
.await?
);
assert!(c.query(By::Id("footer")).nowait().exists().await?);
c.goto(sample_page_url()).await?;
c.execute(
"setTimeout(() => document.getElementById('navigation').remove(), 50); \
setTimeout(() => document.getElementById('footer').remove(), 150);",
vec![],
)
.await?;
c.query(By::Id("navigation"))
.or(By::Id("footer"))
.wait(Duration::from_secs(2), Duration::from_millis(20))
.wait_until_gone()
.await?;
c.query(By::Id("footer")).nowait().wait_until_gone().await?;
Ok(())
})
}
#[rstest]
fn resolver_element_helpers(test_harness: TestHarness) -> WebDriverResult<()> {
let c = test_harness.driver();
block_on(async {
c.goto(sample_page_url()).await?;
let section = c.find(By::Id("section-text")).await?;
let input = ElementResolverSingle::new_single(section.clone(), By::Id("text-input2"));
let button = ElementResolverSingle::new_single(section, By::Id("button-copy"));
assert_eq!(input.value().await?.as_deref(), Some(""));
input.send_keys("from resolver").await?;
assert_eq!(input.value().await?.as_deref(), Some("from resolver"));
assert_eq!(button.text().await?, "Copy");
button.click().await?;
assert_eq!(c.find(By::Id("text-output")).await?.text().await?, "from resolver");
c.execute(
"const old = document.getElementById('button-copy'); old.replaceWith(old.cloneNode(true));",
vec![],
)
.await?;
input.clear().await?;
input.send_keys("ready again").await?;
button.click_when_ready().await?;
assert_eq!(c.find(By::Id("text-output")).await?.text().await?, "ready again");
Ok(())
})
}