From 7de7e53436df4f5abf131870a1f02f2d91379bf3 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 5 Aug 2021 20:42:41 +0200 Subject: refactor: merge getters for faculties and spaces options --- src/browser/mod.rs | 6 ++--- src/browser/web_browser.rs | 59 ++++++++++++++++------------------------------ 2 files changed, 23 insertions(+), 42 deletions(-) diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 1f44493..82c9c60 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use self::web_browser::{Browser, WEB_BROWSER}; +use self::web_browser::{Browser, ROOMS_URL, WEB_BROWSER}; use crate::Config; use thirtyfour::prelude::WebDriverResult; @@ -27,7 +27,7 @@ pub async unsafe fn login(credentials: &Config) -> WebDriverResult<()> { /// Get the faculties available for booking a room pub async unsafe fn get_faculties() -> WebDriverResult>> { if let Some(driver) = &WEB_BROWSER { - if let Some(faculties) = driver.faculties().await? { + if let Some(faculties) = driver.get_options("dipartimento", ROOMS_URL).await? { return Ok(Some(faculties)); } } @@ -38,7 +38,7 @@ pub async unsafe fn get_faculties() -> WebDriverResult WebDriverResult>> { if let Some(driver) = &WEB_BROWSER { - if let Some(spaces) = driver.spaces().await? { + if let Some(spaces) = driver.get_options("space", "").await? { return Ok(Some(spaces)); } } diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs index f799866..34e78d1 100644 --- a/src/browser/web_browser.rs +++ b/src/browser/web_browser.rs @@ -9,7 +9,7 @@ use thirtyfour::{FirefoxCapabilities, WebDriver, WebDriverCommands}; /// This url is used to make the login const LOGIN_URL: &str = "https://studenti.smartedu.unict.it/WorkFlow2011/Logon/Logon.aspx?ReturnUrl=%2fStudenti%2fDefault.aspx"; /// This url is used to go to the page where a student can book a room for study -const ROOMS_URL: &str = "https://studenti.smartedu.unict.it/StudentSpaceReserv?Type=unaTantum"; +pub const ROOMS_URL: &str = "https://studenti.smartedu.unict.it/StudentSpaceReserv?Type=unaTantum"; /// Browser struct pub struct Browser { @@ -82,22 +82,35 @@ impl Browser { Ok(()) } - /// Get all faculties for booking and return an `HashMap` when `key` is the - /// key for that faculty inside the `select` tag and `value` is just the text of the option. - pub async fn faculties(&self) -> WebDriverResult>> { + /// Get all options for booking following the `label` and return an `HashMap` when `key` is the + /// key for that label inside the `select` tag and `value` is just the text of the option. + /// If `url` is specified then go to that page + pub async fn get_options( + &self, + label: &str, + url: &str, + ) -> WebDriverResult>> { if let Some(_d) = &self.driver { - _d.get(ROOMS_URL).await?; + if url != "" { + _d.get(url).await?; + } thread::sleep(time::Duration::from_millis(1000)); _d.find_element(By::Css( - "span[aria-labelledby='select2-dipartimentoSelector-container']", + &format!( + "span[aria-labelledby='select2-{}Selector-container']", + label + ) + .to_owned()[..], )) .await? .click() .await?; let list_elements = _d - .find_elements(By::Css("#select2-dipartimentoSelector-results li")) + .find_elements(By::Css( + &format!("#select2-{}Selector-results li", label).to_owned()[..], + )) .await?; let mut faculties_ids = HashMap::::new(); @@ -115,38 +128,6 @@ impl Browser { Ok(None) } - /// Get all spaces for booking and return an `HashMap` when `key` is the - /// key for that space inside the `select` tag and `value` is just the text of the option. - pub async fn spaces(&self) -> WebDriverResult>> { - if let Some(_d) = &self.driver { - thread::sleep(time::Duration::from_millis(1000)); - - _d.find_element(By::Css( - "span[aria-labelledby='select2-spaceSelector-container']", - )) - .await? - .click() - .await?; - - let list_elements = _d - .find_elements(By::Css("#select2-spaceSelector-results li")) - .await?; - - let mut spaces_ids = HashMap::::new(); - - for i in list_elements { - spaces_ids.insert( - i.get_attribute("data-select2-id").await.unwrap().unwrap(), - i.text().await.unwrap(), - ); - } - - return Ok(Some(spaces_ids)); - } - - Ok(None) - } - /// Select an option from a list of select elements pub async fn select_option_from_list( &self, -- cgit v1.2.3-18-g5258