From dbc425ea9af4c6c3ff7352513d45694b94ab0aa6 Mon Sep 17 00:00:00 2001 From: AaronJamesY Date: Fri, 10 Feb 2017 18:51:03 +0000 Subject: [PATCH 1/6] Moved helper function from CardHelpers to AccountHelpers --- BankingBot/BankingBot.csproj | 2 +- BankingBot/Helpers/{CardHelpers.cs => AccountHelpers.cs} | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) rename BankingBot/Helpers/{CardHelpers.cs => AccountHelpers.cs} (78%) diff --git a/BankingBot/BankingBot.csproj b/BankingBot/BankingBot.csproj index a5fd61c..abff66c 100644 --- a/BankingBot/BankingBot.csproj +++ b/BankingBot/BankingBot.csproj @@ -62,7 +62,7 @@ - + diff --git a/BankingBot/Helpers/CardHelpers.cs b/BankingBot/Helpers/AccountHelpers.cs similarity index 78% rename from BankingBot/Helpers/CardHelpers.cs rename to BankingBot/Helpers/AccountHelpers.cs index e9fc9aa..e94128e 100644 --- a/BankingBot/Helpers/CardHelpers.cs +++ b/BankingBot/Helpers/AccountHelpers.cs @@ -1,8 +1,12 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace BankingBot.Helpers { - public static class CardHelpers + public static class AccountHelpers { public static string[] SplitCardNumber(string cardNumber) { From e4456a5df88ab24a4d7a01b69479455186dc30e3 Mon Sep 17 00:00:00 2001 From: AaronJamesY Date: Fri, 10 Feb 2017 18:53:39 +0000 Subject: [PATCH 2/6] Added SplitSortCode helper --- BankingBot/Helpers/AccountHelpers.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/BankingBot/Helpers/AccountHelpers.cs b/BankingBot/Helpers/AccountHelpers.cs index e94128e..a413736 100644 --- a/BankingBot/Helpers/AccountHelpers.cs +++ b/BankingBot/Helpers/AccountHelpers.cs @@ -11,7 +11,7 @@ namespace BankingBot.Helpers public static string[] SplitCardNumber(string cardNumber) { if (cardNumber.Length != 16) - throw new ArgumentException("Card number was have a length of 16 characters."); + throw new ArgumentException("Card number must have a length of 16 characters."); return new[] { @@ -21,5 +21,18 @@ namespace BankingBot.Helpers 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) + }; + } } } From 5f47d4d66e7f2262a403f8498ee4ada8ae19451c Mon Sep 17 00:00:00 2001 From: AaronJamesY Date: Fri, 10 Feb 2017 19:02:09 +0000 Subject: [PATCH 3/6] Adding login funcionality for Barclays --- .../LoginManagers/BarclaysLoginManager.cs | 35 +++++++++++++++---- .../BarclaysLoginCredentials.cs | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs b/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs index dd72901..0789839 100644 --- a/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs +++ b/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs @@ -1,14 +1,11 @@ -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 OpenQA.Selenium; +using System; namespace BankingBot.ActionManagers.LoginManagers { @@ -51,15 +48,39 @@ namespace BankingBot.ActionManagers.LoginManagers { _browserBot.WebDriver.FindElement(By.Id("card-radio")).Click(); - var cardSplit = Helpers.CardHelpers.SplitCardNumber(barcCreds.CardNumber); + var cardSplit = AccountHelpers.SplitCardNumber(barcCreds.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 (barcCreds.SortCode != null || barcCreds.AccountNumber != null) + { + _browserBot.WebDriver.FindElement(By.Id("account-radio")).Click(); + var sortcodeSplit = AccountHelpers.SplitSortCode(barcCreds.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(barcCreds.Passcode); + + var characters = _browserBot.WebDriver.FindElement(By.ClassName("letter-select")) + .FindElements(By.TagName("strong")); + } } } 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; } } From c2d69ceb710c007954cd34fbb73224af39a62364 Mon Sep 17 00:00:00 2001 From: AaronJamesY Date: Fri, 10 Feb 2017 19:05:26 +0000 Subject: [PATCH 4/6] Added StringExtensions --- BankingBot/BankingBot.csproj | 3 +++ BankingBot/Extensions/StringExtensions.cs | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 BankingBot/Extensions/StringExtensions.cs diff --git a/BankingBot/BankingBot.csproj b/BankingBot/BankingBot.csproj index abff66c..908f6ea 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,6 +64,7 @@ + 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]", "")); + } + } +} From 8591bfb8d56f4c4414730de73e529b58b9be6b9b Mon Sep 17 00:00:00 2001 From: AaronJamesY Date: Fri, 10 Feb 2017 19:58:10 +0000 Subject: [PATCH 5/6] Changing Barclays login to use JS --- .../LoginManagers/BarclaysLoginManager.cs | 128 +++++++++++------- BankingBot/BankingBot.csproj | 6 + BankingBot/ScriptManagement/ScriptBundles.cs | 7 +- BankingBot/ScriptManagement/ScriptManager.cs | 8 ++ .../Scripts/Helpers/account-helpers.js | 13 ++ .../Scripts/barclays-login.js | 30 ++++ 6 files changed, 139 insertions(+), 53 deletions(-) create mode 100644 BankingBot/ScriptManagement/Scripts/Helpers/account-helpers.js create mode 100644 BankingBot/ScriptManagement/Scripts/barclays-login.js diff --git a/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs b/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs index 0789839..d3a40cd 100644 --- a/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs +++ b/BankingBot/ActionManagers/LoginManagers/BarclaysLoginManager.cs @@ -4,16 +4,20 @@ 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 { @@ -24,63 +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 = AccountHelpers.SplitCardNumber(barcCreds.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 (barcCreds.SortCode != null || barcCreds.AccountNumber != null) - { - _browserBot.WebDriver.FindElement(By.Id("account-radio")).Click(); - - var sortcodeSplit = AccountHelpers.SplitSortCode(barcCreds.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(barcCreds.Passcode); - - var characters = _browserBot.WebDriver.FindElement(By.ClassName("letter-select")) - .FindElements(By.TagName("strong")); - + Status = ResponseStatus.Success + }; } } } diff --git a/BankingBot/BankingBot.csproj b/BankingBot/BankingBot.csproj index 908f6ea..2eb2700 100644 --- a/BankingBot/BankingBot.csproj +++ b/BankingBot/BankingBot.csproj @@ -83,9 +83,15 @@ + + Always + PreserveNewest + + Always + PreserveNewest 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..3d93ed9 --- /dev/null +++ b/BankingBot/ScriptManagement/Scripts/barclays-login.js @@ -0,0 +1,30 @@ +(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) { + $("#card-radio").click(); + + // 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]); + } + + alert("Working!"); + } +})(); From 722ef42fe4db33b3d049510aa39f2b1241b7c000 Mon Sep 17 00:00:00 2001 From: AaronJamesY Date: Fri, 10 Feb 2017 20:17:04 +0000 Subject: [PATCH 6/6] Amended Barclays login script --- BankingBot/ScriptManagement/Scripts/barclays-login.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/BankingBot/ScriptManagement/Scripts/barclays-login.js b/BankingBot/ScriptManagement/Scripts/barclays-login.js index 3d93ed9..7f2ef19 100644 --- a/BankingBot/ScriptManagement/Scripts/barclays-login.js +++ b/BankingBot/ScriptManagement/Scripts/barclays-login.js @@ -16,7 +16,9 @@ $("#membership-num").val(values.membershipNumber); } else if (values.cardNumber) { - $("#card-radio").click(); + + $("#debitCardStep1").val("Testing"); + $("#debitCardStep1").hide(); // Populate card number text boxes var cardNumberSplit = accountHelpers.splitCardNumber(values.cardNumber); @@ -25,6 +27,7 @@ $("#debitCardStep" + (i + 1)).val(cardNumberSplit[i]); } - alert("Working!"); + $("#card-radio").click(); + } })();