diff options
Diffstat (limited to 'src/browser/web_browser.rs')
-rw-r--r-- | src/browser/web_browser.rs | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs index d014bd6..552d72c 100644 --- a/src/browser/web_browser.rs +++ b/src/browser/web_browser.rs @@ -53,20 +53,46 @@ impl Browser { // Ok(()) // } + /// Compare `current_url` with the login path. If they are the same, it + /// needs to re-login + async unsafe fn check_login(&self) -> WebDriverResult<bool> { + if let Some(_d) = &self.driver { + if !_d.current_url().await?.starts_with(LOGIN_URL) { + return Ok(false); + } + + log::debug!("Logging..."); + match self._login().await { + Ok(_) => { + log::info!("Logged in Smartedu!"); + } + Err(e) => { + // Using the bot when the user is not logged in, is simply useless. + panic!( + "You can't connect: `{}`, credentials are {:?}", + e, self.credentials + ); + } + }; + } + + Ok(true) + } + /// Login on `LOGIN_URL` - pub async unsafe fn _login(&self, credentials: &Config) -> WebDriverResult<()> { + async unsafe fn _login(&self) -> 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?; + cf_input.send_keys(&self.credentials.cf).await?; thread::sleep(time::Duration::from_millis(3000)); let psw_input = _d .find_element(By::Name("ctl01$contents$UserPassword")) .await?; - psw_input.send_keys(&credentials.password).await?; + psw_input.send_keys(&self.credentials.password).await?; thread::sleep(time::Duration::from_millis(3000)); @@ -108,6 +134,19 @@ impl Browser { if url != "" { _d.get(url).await?; } + + unsafe { + match self.check_login().await { + Ok(true) => { + // Login has been made, so reload the url + if url != "" { + _d.get(url).await?; + } + } + Ok(false) => {} + Err(e) => panic!("{:?}", e), + }; + } thread::sleep(time::Duration::from_millis(1000)); _d.find_element(By::Css( |