summaryrefslogtreecommitdiff
path: root/src/browser/web_browser.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-07-30 19:24:41 +0200
committerSanto Cariotti <santo@dcariotti.me>2021-07-30 19:24:41 +0200
commit99968c72a5efbd535362e050baf314f9e0cff709 (patch)
tree910f43a609186a5719b345632fc80d218cb7b627 /src/browser/web_browser.rs
parentbba0f862251c737e576c949320f2eecf15a74057 (diff)
refactor: browser as modular funcs
Diffstat (limited to 'src/browser/web_browser.rs')
-rw-r--r--src/browser/web_browser.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs
new file mode 100644
index 0000000..53b8baa
--- /dev/null
+++ b/src/browser/web_browser.rs
@@ -0,0 +1,68 @@
+use crate::Config;
+use std::{thread, time};
+use thirtyfour::error::{WebDriverError, WebDriverErrorInfo, WebDriverErrorValue};
+use thirtyfour::prelude::{By, WebDriverResult};
+use thirtyfour::{FirefoxCapabilities, WebDriver, WebDriverCommands};
+
+const LOGIN_URL: &str = "https://studenti.smartedu.unict.it/WorkFlow2011/Logon/Logon.aspx";
+
+pub struct Browser {
+ driver: Option<WebDriver>,
+}
+
+impl Browser {
+ pub async fn new(driver_url: &String) -> Self {
+ Self {
+ driver: Some(
+ WebDriver::new(driver_url, FirefoxCapabilities::new())
+ .await
+ .unwrap(),
+ ),
+ }
+ }
+
+ // TODO: Close the driver fixing the borrowing error
+ // async fn _close(self) -> Result<(), Box<dyn Error>> {
+ // Ok(())
+ // }
+
+ pub async unsafe fn _login(&self, credentials: &Config) -> WebDriverResult<()> {
+ if let Some(_d) = &self.driver {
+ _d.get(LOGIN_URL).await?;
+
+ let cf_input = _d.find_element(By::Name("ctl01$contents$UserName")).await?;
+ cf_input.send_keys(&credentials.cf).await?;
+
+ let psw_input = _d
+ .find_element(By::Name("ctl01$contents$UserPassword"))
+ .await?;
+ psw_input.send_keys(&credentials.password).await?;
+
+ thread::sleep(time::Duration::from_millis(1000));
+
+ _d.find_element(By::Name("ctl01$contents$LogonButton"))
+ .await?
+ .click()
+ .await?;
+
+ thread::sleep(time::Duration::from_millis(2000));
+
+ if _d.current_url().await? == LOGIN_URL {
+ return Err(WebDriverError::SessionNotCreated(WebDriverErrorInfo {
+ status: 400,
+ error: "SessionNotCreated".to_string(),
+ value: WebDriverErrorValue {
+ message: "SessionNotCreated".to_string(),
+ error: None,
+ stacktrace: None,
+ data: None,
+ },
+ }));
+ }
+ }
+
+ Ok(())
+ }
+}
+
+pub static mut WEB_BROWSER: Option<Browser> = None;