Skip to content

Commit

Permalink
Added browser headless mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
Harabe-x committed Jan 16, 2024
1 parent 144a5a0 commit 535b600
Show file tree
Hide file tree
Showing 22 changed files with 306 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public static void CreateConfigFile()
{
StartWithSystem = false,
ToastNotifications = false,
CreditsAutoClaim = false
CreditsAutoClaim = false,
MultiThreading = false,
HeadlessBrowser = false,
NumberOfThreads = 1,
};
JsonWriter.WriteJson(userConfig, UserConfigPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy;
using PixaiBot.Data.Interfaces;
using PixaiBot.UI.Models;

Expand All @@ -21,26 +22,18 @@ public AccountCreatorV2(IPixaiNavigation pixaiNavigation, ITempMailApiManager te
}


/// <summary>
/// Starts the account creation process
/// </summary>
/// <param name="amount"></param>
/// <param name="tempMailApiKey"></param>
/// <param name="shouldUseProxy"></param>
/// <param name="shouldVerifyEmail"></param>
public void CreateAccounts(int amount, string tempMailApiKey, bool shouldUseProxy, bool shouldVerifyEmail,
CancellationToken token, TimeSpan interval)

public void CreateAccounts(int amount, string tempMailApiKey, bool shouldVerifyEmail,
IDriverCreationStrategy driverCreationStrategy, TimeSpan interval, CancellationToken token)
{
_logger.Log("The account creation process has started", _logger.ApplicationLogFilePath);

for (var i = 0; i < amount; i++)
{
if (token.IsCancellationRequested) return;

using var driver = shouldUseProxy
? ChromeDriverFactory.CreateDriver(_proxyManager.GetRandomProxy())
: ChromeDriverFactory.CreateDriver();

var driver = driverCreationStrategy.CreateDriver();

_logger.Log("=====Launched Chrome Driver=====", _logger.CreditClaimerLogFilePath);

try
Expand All @@ -66,16 +59,17 @@ public void CreateAccounts(int amount, string tempMailApiKey, bool shouldUseProx
_logger.Log("The account creation process has ended", _logger.CreditClaimerLogFilePath);
}

private void CreateAccount(ChromeDriver driver, bool shouldVerifyEmail, string tempMailApiKey)
private void CreateAccount(IWebDriver driver,bool shouldVerifyEmail, string tempMailApiKey)
{
_logger.Log("Creating account login details", _logger.CreditClaimerLogFilePath);


var email = shouldVerifyEmail
? _loginCredentialsMaker.GenerateEmail(tempMailApiKey)
: _loginCredentialsMaker.GenerateEmail();

var password = _loginCredentialsMaker.GeneratePassword();


_logger.Log("Creating account login details", _logger.CreditClaimerLogFilePath);

_pixaiNavigation.NavigateToUrl(driver, RegistrationPageUrl);
Expand Down Expand Up @@ -114,7 +108,7 @@ private void CreateAccount(ChromeDriver driver, bool shouldVerifyEmail, string t
VerifyEmail(userAccount, driver, tempMailApiKey);
}

private void VerifyEmail(UserAccount userAccount, ChromeDriver driver, string tempMailApiKey)
private void VerifyEmail(UserAccount userAccount, IWebDriver driver, string tempMailApiKey)
{
var verificationLink = string.Empty;
const int maxAttempts = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading;
using OpenQA.Selenium.Support.UI;
using PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy;
using PixaiBot.Business_Logic.Extension;
using PixaiBot.Data.Interfaces;
using PixaiBot.UI.Models;
Expand Down Expand Up @@ -32,8 +33,10 @@ public void ClearStringBuilderContent()
_stringBuilder.Clear();
}



public string StartLoggingAccountsInfo(IEnumerable<UserAccount> userAccountsList,
IAccountInfoLoggerSettings settings,
IDriverCreationStrategy driverCreationStrategy, IAccountInfoLoggerSettings settings,
CancellationToken cancellationToken)
{
_logger.Log($"Logging information about {userAccountsList.Count()} accounts", _logger.CreditClaimerLogFilePath);
Expand All @@ -42,7 +45,7 @@ public string StartLoggingAccountsInfo(IEnumerable<UserAccount> userAccountsList
if (cancellationToken.IsCancellationRequested) return _stringBuilder.ToString();
try
{
LogAccountInfo(account, settings);
LogAccountInfo(account,settings,driverCreationStrategy);
}
catch (Exception e)
{
Expand All @@ -54,11 +57,11 @@ public string StartLoggingAccountsInfo(IEnumerable<UserAccount> userAccountsList
return _stringBuilder.ToString();
}

private void LogAccountInfo(UserAccount account, IAccountInfoLoggerSettings settings)
private void LogAccountInfo(UserAccount account, IAccountInfoLoggerSettings settings, IDriverCreationStrategy driverCreationStrategy)
{
_logger.Log("=====Launched Chrome Driver=====", _logger.CreditClaimerLogFilePath);

using var driver = ChromeDriverFactory.CreateDriver();
using var driver = driverCreationStrategy.CreateDriver();
var internalStringBuilder = new StringBuilder();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(MaxLoginAttemptSeconds));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading;
using OpenQA.Selenium.Support.UI;
using PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy;
using PixaiBot.Data.Interfaces;
using PixaiBot.UI.Models;

Expand All @@ -20,14 +21,9 @@ public AccountLoginChecker(ILogger logger, IPixaiNavigation pixaiNavigation)
#endregion


/// <summary>
/// Checks account login credentials
/// </summary>
/// <param name="userAccount"></param>
/// <returns>True if the account is valid</returns>
public bool CheckAccountLogin(UserAccount userAccount)
public bool CheckAccountLogin(UserAccount userAccount,IDriverCreationStrategy driverCreationStrategy)
{
using var driver = ChromeDriverFactory.CreateDriver();
using var driver = driverCreationStrategy.CreateDriver();

_logger.Log("=====Launched Chrome Driver=====", _logger.CreditClaimerLogFilePath);

Expand All @@ -53,13 +49,9 @@ public bool CheckAccountLogin(UserAccount userAccount)
return false;
}

/// <summary>
/// Checks all accounts login credentials
/// </summary>
/// <param name="accountsList"></param>
/// <returns>IEnumerable with valid accounts</returns>
public IEnumerable<UserAccount> CheckAllAccountsLogin(IEnumerable<UserAccount> accountsList,
CancellationToken token)

public IEnumerable<UserAccount> CheckAllAccountsLogin(IEnumerable<UserAccount> accountsList, IDriverCreationStrategy driverCreationStrategy
,CancellationToken token)
{
var validAccounts = new List<UserAccount>();

Expand All @@ -69,7 +61,7 @@ public IEnumerable<UserAccount> CheckAllAccountsLogin(IEnumerable<UserAccount> a

try
{
if (CheckAccountLogin(userAccount))
if (CheckAccountLogin(userAccount,driverCreationStrategy))
{
validAccounts.Add(userAccount);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,28 @@ public static ChromeDriver CreateDriver()

options.AddArgument("--window-position=-32000,-32000");

options.Proxy = new Proxy();
var service = ChromeDriverService.CreateDefaultService();

service.HideCommandPromptWindow = true;

var driver = new ChromeDriver(service, options);

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(MaxWaitTime);

return driver;
}

/// <summary>
/// Creates a ChromeDriver with the needed settings to hide the process and run it headless.
/// </summary>
/// <returns>Chrome Driver Instance</returns>
public static ChromeDriver CreateHeadlessDriver()
{
var options = new ChromeOptions();

options.AddArgument("--window-position=-32000,-32000");

options.AddArgument("--headless");

var service = ChromeDriverService.CreateDefaultService();

Expand All @@ -30,6 +51,7 @@ public static ChromeDriver CreateDriver()
}



/// <summary>
/// Creates a ChromeDriver with the needed settings to hide the process.
/// </summary>
Expand All @@ -44,13 +66,14 @@ public static ChromeDriver CreateDriver(string proxy)
var proxyObject = new Proxy()
{
HttpProxy = proxy,
Kind = ProxyKind.Manual
FtpProxy = proxy,
SocksProxy = proxy,
SslProxy = proxy,
Kind = ProxyKind.AutoDetect
};

options.Proxy = proxyObject;

options.AddArgument("--ignore-certificate-errors");

var service = ChromeDriverService.CreateDefaultService();

service.HideCommandPromptWindow = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy;
using PixaiBot.Business_Logic.Driver_and_Browser_Management.WebNavigationCore.WebNavigationCoreException;
using PixaiBot.Data.Interfaces;
using PixaiBot.UI.Models;
Expand All @@ -24,9 +25,9 @@ public CreditClaimerV2(ILogger logger, IPixaiNavigation pixaiNavigation)

#region Methods

public void ClaimCredits(UserAccount userAccount)
public void ClaimCredits(UserAccount userAccount,IDriverCreationStrategy driverCreationStrategy)
{
using var driver = ChromeDriverFactory.CreateDriver();
using var driver = driverCreationStrategy.CreateDriver();

_logger.Log("=====Launched Chrome Driver=====", _logger.CreditClaimerLogFilePath);

Expand Down Expand Up @@ -65,7 +66,9 @@ public void ClaimCredits(UserAccount userAccount)
_logger.Log("=====Chrome Drive Closed=====\n", _logger.CreditClaimerLogFilePath);
}

public void ClaimCreditsForAllAccounts(IEnumerable<UserAccount> accounts, CancellationToken cancellationToken)


public void ClaimCreditsForAllAccounts(IEnumerable<UserAccount> accounts,IDriverCreationStrategy driverCreationStrategy, CancellationToken cancellationToken)
{
foreach (var account in accounts)
{
Expand All @@ -75,7 +78,15 @@ public void ClaimCreditsForAllAccounts(IEnumerable<UserAccount> accounts, Cancel

try
{
ClaimCredits(account);
ClaimCredits(account, driverCreationStrategy);
}
catch (ElementClickInterceptedException e)
{
// This exception may occur in headless mode when the claim button is clicked many times.
// It seems to be related to the headless environment, possibly due to rapid claim button clicks.
// I think it can be safely ignored because the credits are successfully claimed regardless.
_logger.Log(e.Message, _logger.CreditClaimerLogFilePath);

}
catch (ChromeDriverException e)
{
Expand Down Expand Up @@ -114,4 +125,4 @@ public void ClaimCreditsForAllAccounts(IEnumerable<UserAccount> accounts, Cancel
public event EventHandler CreditsAlreadyClaimed;

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy
{
class DebugDriverCreationStrategy : IDriverCreationStrategy
{
public IWebDriver CreateDriver()
{

var driver = new ChromeDriver();

var service =ChromeDriverService.CreateDefaultService();

service.HideCommandPromptWindow = true;

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

return driver;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy
{
public class HeadlessDriverCreationStrategy : IDriverCreationStrategy
{
public IWebDriver CreateDriver()
{
var options = new ChromeOptions();

options.AddArgument("--headless");

var service = ChromeDriverService.CreateDefaultService();

service.HideCommandPromptWindow = true;

var driver = new ChromeDriver(service, options);

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

return driver;
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy
{
class HiddenDriverCreationStrategy : IDriverCreationStrategy
{
public IWebDriver CreateDriver()
{
var options = new ChromeOptions();

options.AddArgument("--window-position=-32000,-32000");


var service = ChromeDriverService.CreateDefaultService();

service.HideCommandPromptWindow = true;

var driver = new ChromeDriver(service, options);

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

return driver;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;

namespace PixaiBot.Business_Logic.Driver_and_Browser_Management.Driver_Creation_Strategy
{
public interface IDriverCreationStrategy
{
IWebDriver CreateDriver();
}
}
Loading

0 comments on commit 535b600

Please sign in to comment.