use std::time::Duration;
use playwright_cdp::options::LaunchOptions;
use playwright_cdp::types::Viewport;
use playwright_cdp::video::VideoStartOptions;
use playwright_cdp::Playwright;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let _ = tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "playwright_cdp=info".into()),
)
.try_init();
let browser = Playwright::launch()
.await?
.chromium()
.launch_with_options(LaunchOptions::default())
.await?;
println!("browser version: {}", browser.version().await?);
let page = browser.new_page().await?;
page.set_viewport_size(Viewport::new(800, 600)).await?;
let video = page.video();
assert!(
!video.is_recording(),
"video should not be recording before start()"
);
println!("video.is_recording() before start: {}", video.is_recording());
video
.start(Some(
VideoStartOptions::default()
.max_width(800)
.max_height(600),
))
.await?;
assert!(
video.is_recording(),
"video should be recording after start()"
);
println!("video.is_recording() after start: {}", video.is_recording());
page.set_content(
r#"<html><body><div id="a" style="width:100px;height:100px;background:red"></div><script>let i=0;const d=document.getElementById('a');setInterval(()=>{d.style.background=(i++%2?'blue':'red');},100);</script></body></html>"#,
)
.await?;
tokio::time::sleep(Duration::from_millis(500)).await;
video.stop().await?;
assert!(
!video.is_recording(),
"video should not be recording after stop()"
);
println!("video.is_recording() after stop: {}", video.is_recording());
let frames = video.frame_count();
println!("video.frame_count(): {frames}");
assert!(
frames > 0,
"screencast should have captured at least one frame"
);
if let Some(path) = video.path() {
println!("video.path(): {}", path.display());
} else {
println!("video.path(): None (in-memory capture, nothing persisted)");
}
browser.close().await?;
println!("PASS");
Ok(())
}