summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-08-05 20:42:41 +0200
committerSanto Cariotti <santo@dcariotti.me>2021-08-05 20:42:41 +0200
commit7de7e53436df4f5abf131870a1f02f2d91379bf3 (patch)
tree93d154fbb5420bdb87835e8a06c51e1bc5aa222e /src
parent5ddacf8e8131bacbcbd360b24c4c45b5380678ee (diff)
refactor: merge getters for faculties and spaces options
Diffstat (limited to 'src')
-rw-r--r--src/browser/mod.rs6
-rw-r--r--src/browser/web_browser.rs59
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<Option<HashMap<String, String>>> {
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<Option<HashMap<String, St
/// Get the spaces (rooms) available to book
pub async unsafe fn get_spaces() -> WebDriverResult<Option<HashMap<String, String>>> {
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<key, value>` 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<Option<HashMap<String, String>>> {
+ /// Get all options for booking following the `label` and return an `HashMap<key, value>` 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<Option<HashMap<String, String>>> {
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::<String, String>::new();
@@ -115,38 +128,6 @@ impl Browser {
Ok(None)
}
- /// Get all spaces for booking and return an `HashMap<key, value>` 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<Option<HashMap<String, String>>> {
- 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::<String, String>::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,