diff --git a/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs b/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs index dd72901..d3a40cd 100644 --- a/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs +++ b/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs @@ -1,22 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using BankingBot.Attributes; +using BankingBot.Attributes; using BankingBot.Contracts; using BankingBot.Enums; using BankingBot.LoginCredentials; using BankingBot.Responses; +using BankingBot.Helpers; +using BankingBot.Extensions; using OpenQA.Selenium; +using System; +using System.Collections.Generic; +using BankingBot.ScriptManagement; namespace BankingBot.ActionManagers.LoginManagers { [ProviderIdentifier(Provider.Barclays)] public class BarclaysLoginManager : IProviderLoginManager { - private readonly IScriptManager _scriptManager; - private readonly IBrowserBot _browserBot; + readonly IScriptManager scriptManager; + readonly IBrowserBot browserBot; + private BarclaysLoginCredentials _credentials; private static class Urls { @@ -27,39 +28,87 @@ namespace BankingBot.ActionManagers.LoginManagers IBrowserBot browserBot, IScriptManager scriptManager) { - _browserBot = browserBot; - _scriptManager = scriptManager; + this.browserBot = browserBot; + this.scriptManager = scriptManager; } public Response Login(ILoginCredentials credentials) { - var barcCreds = credentials as BarclaysLoginCredentials; + _credentials = credentials as BarclaysLoginCredentials; - _browserBot.WebDriver.Url = Urls.Login; - _browserBot.WebDriver.Navigate(); + browserBot.WebDriver.Url = Urls.Login; + browserBot.WebDriver.Navigate(); - _browserBot.WebDriver.FindElement(By.Id("surname")).SendKeys(barcCreds.Surname); + //_browserBot.WebDriver.FindElement(By.Id("surname")).SendKeys(_credentials.Surname); - // Chosen to use membership number - if (barcCreds.MembershipNumber != null) + //// Chosen to use membership number + //if (_credentials.MembershipNumber != null) + //{ + // _browserBot.WebDriver.FindElement(By.Id("membership-radio")).Click(); + // _browserBot.WebDriver.FindElement(By.Id("membership-num")).SendKeys(_credentials.MembershipNumber); + //} + //// Chosen to use card number + //else if (_credentials.CardNumber != null) + //{ + // _browserBot.WebDriver.FindElement(By.Id("card-radio")).Click(); + + // var cardSplit = AccountHelpers.SplitCardNumber(_credentials.CardNumber); + // for (var i = 0; i < 4; i++) + // { + // var fieldId = $"debitCardSet{i + 1}"; + // _browserBot.WebDriver.FindElement(By.Id(fieldId)).SendKeys(cardSplit[i]); + // } + //} + //// Chosen to use account details + //else if (_credentials.SortCode != null || _credentials.AccountNumber != null) + //{ + // _browserBot.WebDriver.FindElement(By.Id("account-radio")).Click(); + + // var sortcodeSplit = AccountHelpers.SplitSortCode(_credentials.SortCode); + // for (var i = 0; i < 3; i++) + // { + // var fieldId = $"sortCodeSet{i + 1}"; + // _browserBot.WebDriver.FindElement(By.Id(fieldId)).SendKeys(sortcodeSplit[i]); + // } + //} + //else + //{ + // throw new InvalidOperationException("Could not determine login procedure from given properties."); + //} + + //// Advance to stage 2 + //_browserBot.WebDriver.FindElement(By.Id("forward")).Click(); + + //_browserBot.WebDriver.FindElement(By.Id("passcode-radio")).Click(); + //_browserBot.WebDriver.FindElement(By.Id("passcode")).SendKeys(_credentials.Passcode); + + //var passcodeCharElements = _browserBot.WebDriver + // .FindElement(By.ClassName("letter-select")) + // .FindElements(By.TagName("strong")); + + //var passcodeChar1 = _credentials.Passcode[passcodeCharElements[0].Text.AsInteger() - 1].ToString(); + //var passcodeChar2 = _credentials.Passcode[passcodeCharElements[1].Text.AsInteger() - 1].ToString(); + + //_browserBot.WebDriver.FindElement(By.Id("nameOne")).SendKeys(passcodeChar1); + //_browserBot.WebDriver.FindElement(By.Id("nameTwo")).SendKeys(passcodeChar1); + + //_browserBot.WebDriver.FindElement(By.Id("log-in-to-online-banking2")).Click(); + + var scriptData = new Dictionary { - _browserBot.WebDriver.FindElement(By.Id("membership-radio")).Click(); - _browserBot.WebDriver.FindElement(By.Id("membership-num")).SendKeys(barcCreds.MembershipNumber); - } - // Chosen to use card number - else if (barcCreds.CardNumber != null) + { "surname", _credentials.Surname }, + { "cardNumber", _credentials.CardNumber }, + { "sortCode", _credentials.SortCode }, + { "membershipNumber", _credentials.MembershipNumber }, + { "accountNumber", _credentials.AccountNumber } + }; + + scriptManager.Execute("barclays-login.js", scriptData, ScriptBundles.ProviderLogin); + + return new Response { - _browserBot.WebDriver.FindElement(By.Id("card-radio")).Click(); - - var cardSplit = Helpers.CardHelpers.SplitCardNumber(barcCreds.CardNumber); - for (var i = 0; i < 4; i++) - { - var fieldId = $"debitCardSet{i + 1}"; - _browserBot.WebDriver.FindElement(By.Id(fieldId)).SendKeys(cardSplit[i]); - } - } - - _browserBot.WebDriver.FindElement(By.Id("forward")).Click(); + Status = ResponseStatus.Success + }; } } } diff --git a/BankingBot/BankingBot.csproj b/BankingBot/BankingBot.csproj index a5fd61c..2eb2700 100644 --- a/BankingBot/BankingBot.csproj +++ b/BankingBot/BankingBot.csproj @@ -21,6 +21,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -29,6 +30,7 @@ TRACE prompt 4 + false @@ -62,7 +64,8 @@ - + + @@ -80,9 +83,15 @@ + + Always + PreserveNewest + + Always + PreserveNewest diff --git a/BankingBot/Extensions/StringExtensions.cs b/BankingBot/Extensions/StringExtensions.cs new file mode 100644 index 0000000..a32b0c5 --- /dev/null +++ b/BankingBot/Extensions/StringExtensions.cs @@ -0,0 +1,13 @@ +using System; +using System.Text.RegularExpressions; + +namespace BankingBot.Extensions +{ + public static class StringExtensions + { + public static int AsInteger(this string str) + { + return int.Parse(Regex.Replace(str, @"[^\d]", "")); + } + } +} diff --git a/BankingBot/Helpers/AccountHelpers.cs b/BankingBot/Helpers/AccountHelpers.cs new file mode 100644 index 0000000..a413736 --- /dev/null +++ b/BankingBot/Helpers/AccountHelpers.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankingBot.Helpers +{ + public static class AccountHelpers + { + public static string[] SplitCardNumber(string cardNumber) + { + if (cardNumber.Length != 16) + throw new ArgumentException("Card number must have a length of 16 characters."); + + return new[] + { + cardNumber.Substring(0, 4), + cardNumber.Substring(4, 4), + cardNumber.Substring(8, 4), + cardNumber.Substring(12, 4) + }; + } + + public static string[] SplitSortCode(string sortcode) + { + if (sortcode.Length != 6) + throw new ArgumentException("Sortcode must have a length of 16 characters."); + + return new[] + { + sortcode.Substring(0, 2), + sortcode.Substring(2, 2), + sortcode.Substring(4, 2) + }; + } + } +} diff --git a/BankingBot/Helpers/CardHelpers.cs b/BankingBot/Helpers/CardHelpers.cs deleted file mode 100644 index e9fc9aa..0000000 --- a/BankingBot/Helpers/CardHelpers.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; - -namespace BankingBot.Helpers -{ - public static class CardHelpers - { - public static string[] SplitCardNumber(string cardNumber) - { - if (cardNumber.Length != 16) - throw new ArgumentException("Card number was have a length of 16 characters."); - - return new[] - { - cardNumber.Substring(0, 4), - cardNumber.Substring(4, 4), - cardNumber.Substring(8, 4), - cardNumber.Substring(12, 4) - }; - } - } -} diff --git a/BankingBot/LoginCredentials/BarclaysLoginCredentials.cs b/BankingBot/LoginCredentials/BarclaysLoginCredentials.cs index ae93573..ac87304 100644 --- a/BankingBot/LoginCredentials/BarclaysLoginCredentials.cs +++ b/BankingBot/LoginCredentials/BarclaysLoginCredentials.cs @@ -11,7 +11,7 @@ namespace BankingBot.LoginCredentials public string CardNumber; public string SortCode; public string AccountNumber; - public string Password; + public string Passcode; public string MemorableWord; } } diff --git a/BankingBot/ScriptManagement/ScriptBundles.cs b/BankingBot/ScriptManagement/ScriptBundles.cs index 822af3e..a6073b5 100644 --- a/BankingBot/ScriptManagement/ScriptBundles.cs +++ b/BankingBot/ScriptManagement/ScriptBundles.cs @@ -2,10 +2,11 @@ { public static class ScriptBundles { - public static string[] Global => new[] + public static string[] ProviderLogin => new[] { - "ScriptManagement/Scripts/jquery-3.1.1.min.js", - "ScriptManagement/Scripts/global.js" + "jquery-3.1.1.min.js", + "global.js", + "helpers/account-helpers.js" }; } } diff --git a/BankingBot/ScriptManagement/ScriptManager.cs b/BankingBot/ScriptManagement/ScriptManager.cs index bc0f6a0..a3b862a 100644 --- a/BankingBot/ScriptManagement/ScriptManager.cs +++ b/BankingBot/ScriptManagement/ScriptManager.cs @@ -8,6 +8,8 @@ namespace BankingBot.ScriptManagement { public class ScriptManager : IScriptManager { + private static string _basePath => "ScriptManagement/Scripts/"; + private readonly IBrowserBot _browserBot; public ScriptManager(IBrowserBot browserBot) @@ -44,6 +46,7 @@ namespace BankingBot.ScriptManagement private string GetScriptContent(string scriptPath) { + scriptPath = GetPath(scriptPath); return File.ReadAllText(scriptPath).Trim() + Environment.NewLine; } @@ -70,5 +73,10 @@ namespace BankingBot.ScriptManagement return scriptContent; } + + private string GetPath(string scriptPath) + { + return _basePath + scriptPath; + } } } diff --git a/BankingBot/ScriptManagement/Scripts/Helpers/account-helpers.js b/BankingBot/ScriptManagement/Scripts/Helpers/account-helpers.js new file mode 100644 index 0000000..73dafa8 --- /dev/null +++ b/BankingBot/ScriptManagement/Scripts/Helpers/account-helpers.js @@ -0,0 +1,13 @@ +var accountHelpers = (function (module) { + + module.splitCardNumber = function (cardNumber) { + return [ + cardNumber.substring(0, 4), + cardNumber.substring(4, 8), + cardNumber.substring(8, 12), + cardNumber.substring(12, 16) + ]; + } + + return module; +})(accountHelpers || {}); \ No newline at end of file diff --git a/BankingBot/ScriptManagement/Scripts/barclays-login.js b/BankingBot/ScriptManagement/Scripts/barclays-login.js new file mode 100644 index 0000000..7f2ef19 --- /dev/null +++ b/BankingBot/ScriptManagement/Scripts/barclays-login.js @@ -0,0 +1,33 @@ +(function () { + var values = { + surname: __$surname, + membershipNumber: __$membershipNumber, + cardNumber: __$cardNumber, + sortCode: __$sortCode, + accountNumber: __$accountNumber + }; + + console.log(values); + + $("#surname").val(values.surname); + + if (values.membershipNumber) { + $("#membership-radio").click(); + $("#membership-num").val(values.membershipNumber); + } + else if (values.cardNumber) { + + $("#debitCardStep1").val("Testing"); + $("#debitCardStep1").hide(); + + // Populate card number text boxes + var cardNumberSplit = accountHelpers.splitCardNumber(values.cardNumber); + console.log(cardNumberSplit); + for (var i = 0; i < 4; i++) { + $("#debitCardStep" + (i + 1)).val(cardNumberSplit[i]); + } + + $("#card-radio").click(); + + } +})();