Merged barclays/login into main

This commit is contained in:
AaronJamesY 2017-02-10 20:17:24 +00:00
commit 45064edd5d
10 changed files with 201 additions and 58 deletions

View file

@ -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<string, string>
{
_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
};
}
}
}

View file

@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -29,6 +30,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -62,7 +64,8 @@
<Compile Include="Contracts\IScriptManager.cs" />
<Compile Include="Enums\Provider.cs" />
<Compile Include="Enums\ResponseStatus.cs" />
<Compile Include="Helpers\CardHelpers.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Helpers\AccountHelpers.cs" />
<Compile Include="LoginCredentials\BarclaysLoginCredentials.cs" />
<Compile Include="LoginCredentials\LloydsLoginCredentials.cs" />
<Compile Include="LoginCredentials\LoginCredentials.cs" />
@ -80,9 +83,15 @@
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Content Include="ScriptManagement\Scripts\barclays-login.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="ScriptManagement\Scripts\global.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ScriptManagement\Scripts\Helpers\account-helpers.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="ScriptManagement\Scripts\jquery-3.1.1.min.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -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]", ""));
}
}
}

View file

@ -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)
};
}
}
}

View file

@ -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)
};
}
}
}

View file

@ -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;
}
}

View file

@ -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"
};
}
}

View file

@ -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;
}
}
}

View file

@ -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 || {});

View file

@ -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();
}
})();