use std::path::Path;
use tracing::{info, instrument};
use super::screenshot::{Animations, ScreenshotBuilder, ScreenshotFormat};
use crate::error::LocatorError;
use crate::page::Locator;
#[derive(Debug)]
pub struct ElementScreenshotBuilder<'a, 'b> {
locator: &'a Locator<'b>,
format: ScreenshotFormat,
quality: Option<u8>,
path: Option<String>,
omit_background: bool,
animations: Animations,
}
impl<'a, 'b> ElementScreenshotBuilder<'a, 'b> {
pub(crate) fn new(locator: &'a Locator<'b>) -> Self {
Self {
locator,
format: ScreenshotFormat::Png,
quality: None,
path: None,
omit_background: false,
animations: Animations::default(),
}
}
#[must_use]
pub fn format(mut self, format: ScreenshotFormat) -> Self {
self.format = format;
self
}
#[must_use]
pub fn quality(mut self, quality: u8) -> Self {
self.quality = Some(quality.min(100));
self
}
#[must_use]
pub fn path(mut self, path: impl AsRef<Path>) -> Self {
self.path = Some(path.as_ref().to_string_lossy().to_string());
self
}
#[must_use]
pub fn omit_background(mut self, omit: bool) -> Self {
self.omit_background = omit;
self
}
#[must_use]
pub fn animations(mut self, animations: Animations) -> Self {
self.animations = animations;
self
}
#[instrument(level = "info", skip(self), fields(format = ?self.format))]
pub async fn capture(self) -> Result<Vec<u8>, LocatorError> {
let page = self.locator.page();
if page.is_closed() {
return Err(LocatorError::PageClosed);
}
let bbox = self.get_element_bounding_box().await?;
info!(
x = bbox.x,
y = bbox.y,
width = bbox.width,
height = bbox.height,
"Capturing element screenshot"
);
let mut builder = ScreenshotBuilder::new(page)
.format(self.format)
.clip(bbox.x, bbox.y, bbox.width, bbox.height)
.omit_background(self.omit_background)
.animations(self.animations);
if let Some(quality) = self.quality {
builder = builder.quality(quality);
}
if let Some(ref path) = self.path {
builder = builder.path(path);
}
builder
.capture()
.await
.map_err(|e| LocatorError::EvaluationError(e.to_string()))
}
async fn get_element_bounding_box(&self) -> Result<BoundingBox, LocatorError> {
let page = self.locator.page();
let js_selector = self.locator.selector().to_js_expression();
let script = format!(
r"
(function() {{
const element = {js_selector};
if (!element) return null;
const rect = element.getBoundingClientRect();
return JSON.stringify({{
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height
}});
}})()
"
);
let result: viewpoint_cdp::protocol::runtime::EvaluateResult = page
.connection()
.send_command(
"Runtime.evaluate",
Some(viewpoint_cdp::protocol::runtime::EvaluateParams {
expression: script,
object_group: None,
include_command_line_api: None,
silent: Some(false),
context_id: None,
return_by_value: Some(true),
await_promise: Some(false),
}),
Some(page.session_id()),
)
.await?;
let json_str = result
.result
.value
.and_then(|v| {
if v.is_null() {
None
} else {
v.as_str().map(String::from)
}
})
.ok_or_else(|| LocatorError::NotFound(self.locator.selector().to_string()))?;
let bbox: serde_json::Value = serde_json::from_str(&json_str).map_err(|e| {
LocatorError::EvaluationError(format!("Failed to parse bounding box: {e}"))
})?;
Ok(BoundingBox {
x: bbox["x"].as_f64().unwrap_or(0.0),
y: bbox["y"].as_f64().unwrap_or(0.0),
width: bbox["width"].as_f64().unwrap_or(0.0),
height: bbox["height"].as_f64().unwrap_or(0.0),
})
}
}
#[derive(Debug, Clone)]
struct BoundingBox {
x: f64,
y: f64,
width: f64,
height: f64,
}