Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(page): add wait_for_network_idle #201

Closed
wants to merge 5 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,65 @@ impl Page {
Ok(self)
}

/// Wait for the network to be idle for 500ms
j-mendez marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "tokio-runtime")]
pub async fn wait_for_network_idle(&self) -> Result<&Self> {
let mut events = self.event_listener::<chromiumoxide_cdp::cdp::browser_protocol::network::EventLoadingFinished>().await?;

loop {
let sleep = tokio::time::sleep(tokio::time::Duration::from_millis(500));
tokio::pin!(sleep);
tokio::select! {
_ = &mut sleep => break,
_ = events.next() => (),
else => break,
j-mendez marked this conversation as resolved.
Show resolved Hide resolved
}
j-mendez marked this conversation as resolved.
Show resolved Hide resolved
}

Ok(self)
}

/// Wait for the network to be idle for 500ms
#[cfg(feature = "async-std-runtime")]
pub async fn wait_for_network_idle(&self) -> Result<&Self> {
use futures::{future::FutureExt, pin_mut, select};
let mut events = self.event_listener::<chromiumoxide_cdp::cdp::browser_protocol::network::EventLoadingFinished>().await?;

loop {
let t1 = async_std::task::sleep(std::time::Duration::from_millis(500)).fuse();
let t2 = events.next().fuse();

pin_mut!(t1, t2);

select! {
() = t1 => break,
_ = t2 => (),
}
}

Ok(self)
}

/// Wait for the network to be idle for 500ms with a duration timeout
#[cfg(feature = "tokio-runtime")]
pub async fn wait_for_network_idle_with_timeout(
&self,
timeout: core::time::Duration,
) -> Result<&Self> {
if let Err(_) = tokio::time::timeout(timeout, self.wait_for_network_idle()).await {}
Ok(self)
}

/// Wait for the network to be idle for 500ms with a duration timeout
#[cfg(feature = "async-std-runtime")]
pub async fn wait_for_network_idle_with_timeout(
&self,
timeout: core::time::Duration,
) -> Result<&Self> {
let _ = (async_std::future::timeout(timeout, self.wait_for_network_idle()).await).is_err();
Ok(self)
}

/// Navigate directly to the given URL.
///
/// This resolves directly after the requested URL is fully loaded.
Expand Down
Loading