From 8c6bcc19b20fbc9318cf952331a07fb63224d1a6 Mon Sep 17 00:00:00 2001 From: Aaron Yarborough Date: Wed, 25 Jan 2017 17:38:37 +0000 Subject: [PATCH] Implemented final login logic for lloyds --- .../LoginManagers/LloydsLoginManager.cs | 74 ++++++++++++++++++- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/BankingBot/LoginManagers/LloydsLoginManager.cs b/BankingBot/LoginManagers/LloydsLoginManager.cs index 5a75097..0c6a722 100644 --- a/BankingBot/LoginManagers/LloydsLoginManager.cs +++ b/BankingBot/LoginManagers/LloydsLoginManager.cs @@ -1,4 +1,5 @@ using System; +using System.Text.RegularExpressions; using BankingBot.Attributes; using BankingBot.Contracts; using BankingBot.LoginCredentials; @@ -9,7 +10,13 @@ namespace BankingBot.LoginManagers [ProviderIdentifier(Enums.Provider.Lloyds)] public class LloydsLoginManager : IProviderLoginManager { - //private LloydsLoginCredentials _credentials; + private static class Urls + { + public const string Login = "https://online.lloydsbank.co.uk/personal/logon/login.jsp"; + public const string MemorableInfo = "https://secure.lloydsbank.co.uk/personal/a/logon/entermemorableinformation.jsp"; + public const string AccountOverview = "https://secure.lloydsbank.co.uk/personal/a/account_overview_personal/"; + } + private readonly IBrowserBot _browserBot; public LloydsLoginManager(IBrowserBot browserBot) @@ -21,13 +28,72 @@ namespace BankingBot.LoginManagers { var lloydsCreds = (LloydsLoginCredentials)credentials; - _browserBot.WebDriver.Url = "https://online.lloydsbank.co.uk/personal/logon/login.jsp"; + LoginStep1(lloydsCreds); + + if (_browserBot.WebDriver.Url != Urls.MemorableInfo) + throw new Exception("An error occured"); + + LoginStep2(lloydsCreds); + + if (_browserBot.WebDriver.Url != Urls.AccountOverview) + throw new Exception("An error occured"); + } + + private void LoginStep1(LloydsLoginCredentials credentials) + { + _browserBot.WebDriver.Url = Urls.Login; _browserBot.WebDriver.Navigate(); - _browserBot.WebDriver.FindElement(By.Id("frmLogin:strCustomerLogin_userID")).SendKeys(lloydsCreds.Username); - _browserBot.WebDriver.FindElement(By.Id("frmLogin:strCustomerLogin_pwd")).SendKeys(lloydsCreds.Password); + _browserBot.WebDriver.FindElement(By.Id("frmLogin:strCustomerLogin_userID")).SendKeys(credentials.Username); + _browserBot.WebDriver.FindElement(By.Id("frmLogin:strCustomerLogin_pwd")).SendKeys(credentials.Password); _browserBot.WebDriver.FindElement(By.Id("frmLogin:btnLogin2")).Click(); } + + private void LoginStep2(LloydsLoginCredentials credentials) + { + var passphraseIndexes = GetPassphraseIndexes(); + + var maxPassphraseLength = passphraseIndexes[2]; + if (credentials.Passphrase.Length < maxPassphraseLength) + throw new IndexOutOfRangeException("Paspshrase is too short"); + + _browserBot.WebDriver.FindElement(By.Id(GetPassphraseDdlId(1))).SendKeys( + credentials.Passphrase[passphraseIndexes[0]].ToString()); + + _browserBot.WebDriver.FindElement(By.Id(GetPassphraseDdlId(2))).SendKeys( + credentials.Passphrase[passphraseIndexes[1]].ToString()); + + _browserBot.WebDriver.FindElement(By.Id(GetPassphraseDdlId(3))).SendKeys( + credentials.Passphrase[passphraseIndexes[2]].ToString()); + + _browserBot.WebDriver.FindElement(By.Id("frmentermemorableinformation1:btnContinue")).Click(); + } + + private int[] GetPassphraseIndexes() + { + if (_browserBot.WebDriver.Url != Urls.MemorableInfo) + throw new InvalidOperationException("Must be on the memorable info page"); + + var charIndexes = new int[3]; + for (var i = 0; i < 3; i++) + { + var cssSelector = $"label[for='{GetPassphraseDdlId(i + 1)}']"; + var labelText = _browserBot.WebDriver.FindElement(By.CssSelector(cssSelector)).Text; + var labelIndex = int.Parse(Regex.Replace(labelText, "[^0-9]", "")); + + charIndexes[i] = labelIndex; + } + + return charIndexes; + } + + private string GetPassphraseDdlId(int index) + { + if (index < 1 || index > 3) + throw new ArgumentException("Must be between 1 and 3"); + + return $"frmentermemorableinformation1:strEnterMemorableInformation_memInfo{index}"; + } } }