firefox_webdriver/browser/tab/
scroll.rs1use tracing::debug;
4
5use crate::error::Result;
6
7use super::Tab;
8
9impl Tab {
14 pub async fn scroll_by(&self, x: i32, y: i32) -> Result<()> {
31 debug!(tab_id = %self.inner.tab_id, x = x, y = y, "Scrolling by");
32
33 let script = format!("window.scrollBy({}, {});", x, y);
34 self.execute_script(&script).await?;
35 Ok(())
36 }
37
38 pub async fn scroll_to(&self, x: i32, y: i32) -> Result<()> {
55 debug!(tab_id = %self.inner.tab_id, x = x, y = y, "Scrolling to");
56
57 let script = format!("window.scrollTo({}, {});", x, y);
58 self.execute_script(&script).await?;
59 Ok(())
60 }
61
62 pub async fn scroll_to_top(&self) -> Result<()> {
64 debug!(tab_id = %self.inner.tab_id, "Scrolling to top");
65 self.scroll_to(0, 0).await
66 }
67
68 pub async fn scroll_to_bottom(&self) -> Result<()> {
70 debug!(tab_id = %self.inner.tab_id, "Scrolling to bottom");
71
72 self.execute_script("window.scrollTo(0, document.body.scrollHeight);")
73 .await?;
74 Ok(())
75 }
76
77 pub async fn get_scroll_position(&self) -> Result<(i32, i32)> {
83 let result = self
84 .execute_script("return { x: window.scrollX, y: window.scrollY };")
85 .await?;
86
87 let x = result.get("x").and_then(|v| v.as_i64()).unwrap_or(0) as i32;
88 let y = result.get("y").and_then(|v| v.as_i64()).unwrap_or(0) as i32;
89
90 debug!(tab_id = %self.inner.tab_id, x = x, y = y, "Got scroll position");
91 Ok((x, y))
92 }
93
94 pub async fn get_page_size(&self) -> Result<(i32, i32)> {
100 let result = self
101 .execute_script(
102 r#"
103 const body = document.body;
104 const html = document.documentElement;
105 return {
106 width: Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth),
107 height: Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight)
108 };
109 "#,
110 )
111 .await?;
112
113 let width = result.get("width").and_then(|v| v.as_i64()).unwrap_or(0) as i32;
114 let height = result.get("height").and_then(|v| v.as_i64()).unwrap_or(0) as i32;
115
116 debug!(tab_id = %self.inner.tab_id, width = width, height = height, "Got page size");
117 Ok((width, height))
118 }
119
120 pub async fn get_viewport_size(&self) -> Result<(i32, i32)> {
126 let result = self
127 .execute_script("return { width: window.innerWidth, height: window.innerHeight };")
128 .await?;
129
130 let width = result.get("width").and_then(|v| v.as_i64()).unwrap_or(0) as i32;
131 let height = result.get("height").and_then(|v| v.as_i64()).unwrap_or(0) as i32;
132
133 debug!(tab_id = %self.inner.tab_id, width = width, height = height, "Got viewport size");
134 Ok((width, height))
135 }
136}