Changing Barclays login to use JS

This commit is contained in:
AaronJamesY 2017-02-10 19:58:10 +00:00
parent c2d69ceb71
commit 8591bfb8d5
6 changed files with 139 additions and 53 deletions

View file

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

View file

@ -83,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

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