bridge_common/
browser.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2024 StarfleetAI
// SPDX-License-Identifier: Apache-2.0

use std::marker::PhantomData;
use std::time::Duration;

use anyhow::Context;
use askama::Template;
use fantoccini::{wd::Capabilities, Client, ClientBuilder, Locator};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::runtime::Handle;
use tokio::task;
use tokio::time::sleep;
use tracing::{debug, error};

use crate::{docker::ContainerManager, types::Result};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("failed to connect to WebDriver: {0}")]
    WebDriverConnection(#[from] fantoccini::error::NewSessionError),
    #[error("failed to execute WebDriver command: {0}")]
    WebDriverCmd(#[from] fantoccini::error::CmdError),
    #[error("failed to get WebDriver host port binding")]
    WebDriverHostPort,
    #[error("failed to save screenshot: {0}")]
    ScreenshotSave(#[from] std::io::Error),
}

/// Stores virtual browser data.
#[derive(Debug)]
pub struct Browser {
    /// Folder where the screenshots and downloaded files will be stored.
    pub workdir: String,
    /// WebDriver Client instance.
    pub client: Client,
    /// Chromedriver container identifier.
    pub container_id: String,
    /// Browser status.
    status: PhantomData<()>,
}

/// Constructs browser instances.
///
/// This type itself is not particularly useful. It only creates browser instances.
#[allow(clippy::module_name_repetitions)]
pub struct BrowserBuilder {
    /// Folder where the screenshots and downloaded files will be stored.
    workdir: String,
}

#[derive(Template)]
#[template(path = "js/list_viewport_elements.js", escape = "none")]
struct ListViewportElementsTemplate {}

#[derive(Debug, Serialize, Deserialize)]
pub enum ElementType {
    #[serde(rename = "text")]
    Text,
    #[serde(rename = "link")]
    Link,
    #[serde(rename = "button")]
    Button,
    #[serde(rename = "input")]
    Input,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Element {
    pub id: i64,
    #[serde(rename = "type")]
    pub type_: ElementType,
    pub content: Option<String>,
}

impl BrowserBuilder {
    /// Create a new instance of itself.
    #[must_use]
    pub fn new(workdir: &str) -> Self {
        Self {
            workdir: workdir.to_string(),
        }
    }

    /// The Browser instance initialisation.
    ///
    /// Creates the personal chromedriver container, connects to it, saves the necessary data into Browser attributes.
    /// # Errors
    ///
    /// Returns error if there was a problem while connecting to `WebDriver`.
    pub async fn connect(self) -> Result<Browser> {
        let mut caps = Capabilities::new();
        // TODO: support geckodriver
        let opts = json!({
            "args": ["--headless", "--disable-gpu", "--no-sandbox", "--disable-dev-shm-usage"],
        });
        caps.insert("goog:chromeOptions".to_string(), opts);

        let docker_client = ContainerManager::get().await?;
        let container_id = docker_client.launch_chromedriver_container().await?;

        let host_port = Self::wait_for_host_port(docker_client, &container_id).await?;

        let client = ClientBuilder::rustls()
            .capabilities(caps)
            .connect(&format!("http://localhost:{host_port}"))
            .await
            .map_err(Error::WebDriverConnection)?;

        client
            .set_window_size(1920, 1080)
            .await
            .map_err(Error::WebDriverCmd)?;

        Ok(Browser {
            client,
            container_id,
            workdir: self.workdir,
            status: PhantomData,
        })
    }

    async fn wait_for_host_port(
        docker_client: &ContainerManager,
        container_id: &str,
    ) -> Result<String> {
        for _ in 0..30 {
            let container_info = docker_client.inspect_container(container_id).await?;

            if let Some(port) = container_info
                .network_settings
                .as_ref()
                .and_then(|network_settings| network_settings.ports.as_ref())
                .and_then(|ports| ports.get("9515/tcp"))
                .and_then(|maybe_port_bindings| maybe_port_bindings.as_ref())
                .and_then(|port_bindings| port_bindings.first())
                .and_then(|port_binding| port_binding.host_port.as_deref())
            {
                return Ok(port.to_string());
            }

            debug!("Port 9515 is not bound yet, waiting...");

            sleep(Duration::from_millis(500)).await;
        }

        Err(Error::WebDriverHostPort.into())
    }
}

impl Browser {
    /// Navigate to the given URL.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn goto(&mut self, url: &str) -> Result<()> {
        Ok(self.client.goto(url).await.map_err(Error::WebDriverCmd)?)
    }

    /// Get the current URL.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn get_current_url(&self) -> Result<String> {
        Ok(self
            .client
            .current_url()
            .await
            .map_err(Error::WebDriverCmd)?
            .to_string())
    }

    /// Get the HTML of the current page.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn get_html(&self) -> Result<String> {
        Ok(self
            .find(Locator::Css("html"))
            .await?
            .html(false)
            .await
            .map_err(Error::WebDriverCmd)?)
    }

    /// Save a screenshot of the current page.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command or saving the screenshot.
    pub async fn save_screenshot(&self) -> Result<String> {
        let bytes = self
            .client
            .screenshot()
            .await
            .map_err(Error::WebDriverCmd)?;

        let file_path = format!("{}/screenshot.png", self.workdir);
        std::fs::write(&file_path, bytes).map_err(Error::ScreenshotSave)?;

        Ok(file_path)
    }

    /// Get meaningful elements from the current viewport.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn list_viewport_elements(&self) -> Result<Vec<Element>> {
        let script_template = ListViewportElementsTemplate {};
        let content = script_template
            .render()
            .with_context(|| "Failed to render `call_tools` script")?;

        let result = self
            .client
            .execute(&content, vec![])
            .await
            .map_err(Error::WebDriverCmd)?;
        debug!("Elements from viewport: {result}");

        Ok(serde_json::from_value(result.clone())
            .with_context(|| format!("Failed to parse elements from result: {result}"))?)
    }

    /// Scrolls one screen down.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn scroll_down(&self) -> Result<()> {
        self.client
            .execute("window.scrollBy(0, window.innerHeight)", vec![])
            .await
            .map_err(Error::WebDriverCmd)?;

        Ok(())
    }

    /// Scrolls one screen up.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn scroll_up(&self) -> Result<()> {
        self.client
            .execute("window.scrollBy(0, -window.innerHeight)", vec![])
            .await
            .map_err(Error::WebDriverCmd)?;

        Ok(())
    }

    /// Calculates scroll position percentage.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn get_scroll_position(&self) -> Result<i64> {
        let scroll_top = self
            .client
            .execute("return window.scrollY", vec![])
            .await
            .map_err(Error::WebDriverCmd)?;
        let scroll_height = self
            .client
            .execute("return document.body.scrollHeight", vec![])
            .await
            .map_err(Error::WebDriverCmd)?;
        let client_height = self
            .client
            .execute("return window.innerHeight", vec![])
            .await
            .map_err(Error::WebDriverCmd)?;
        let scroll_position = scroll_top.as_f64().unwrap_or_default()
            / (scroll_height.as_f64().unwrap_or_default()
                - client_height.as_f64().unwrap_or_default());

        #[allow(clippy::cast_possible_truncation)]
        Ok((scroll_position * 100.0).ceil() as i64)
    }

    /// Clicks on the element with a given `data-sfai` attribute value.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn click(&self, id: i64) -> Result<()> {
        self.client
            .execute(
                &format!("document.querySelector('[data-sfai=\"{id}\"]').click()"),
                vec![],
            )
            .await
            .map_err(Error::WebDriverCmd)?;

        Ok(())
    }

    /// Sends keys to the element with a given `data-sfai` attribute value.
    ///
    /// # Errors
    ///
    /// Returns error if there was a problem while executing `WebDriver` command.
    pub async fn send_keys(&self, id: i64, text: &str) -> Result<()> {
        self.find(Locator::Css(&format!("[data-sfai=\"{id}\"]")))
            .await?
            .send_keys(text)
            .await
            .map_err(Error::WebDriverCmd)?;

        Ok(())
    }

    async fn find(&self, locator: Locator<'_>) -> Result<fantoccini::elements::Element> {
        Ok(self
            .client
            .find(locator)
            .await
            .map_err(Error::WebDriverCmd)?)
    }
}

impl Drop for Browser {
    fn drop(&mut self) {
        let container_id = self.container_id.clone();

        task::block_in_place(move || {
            Handle::current().block_on(async move {
                let docker_client = match ContainerManager::get().await {
                    Ok(client) => client,
                    Err(e) => {
                        error!("Can't get container manager to kill container: {e}");
                        return;
                    }
                };

                if let Err(e) = docker_client.kill_container(&container_id).await {
                    error!("Can't kill container {container_id}: {e}");
                }
            });
        });
    }
}