Added a lot to the doc and modified the pdf generation
This commit is contained in:
+87
-46
@@ -1,8 +1,8 @@
|
||||
/// Author : Maxime Rohmer
|
||||
/// Date : 08/05/2023
|
||||
/// Date : 30/05/2023
|
||||
/// File : F1TVEmulator.cs
|
||||
/// Brief : Class that contains methods to emulate a browser and navigate the F1TV website
|
||||
/// Version : 0.1
|
||||
/// Version : Alpha 1.0
|
||||
|
||||
using OpenQA.Selenium;
|
||||
using OpenQA.Selenium.Firefox;
|
||||
@@ -18,7 +18,7 @@ using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Test_Merge
|
||||
namespace TrackTrends
|
||||
{
|
||||
internal class F1TVEmulator
|
||||
{
|
||||
@@ -39,6 +39,9 @@ namespace Test_Merge
|
||||
GrandPrixUrl = grandPrixUrl;
|
||||
Ready = false;
|
||||
}
|
||||
/// <summary>
|
||||
/// Will start the python programm that runs the Cookie Recovering
|
||||
/// </summary>
|
||||
private void StartCookieRecovering()
|
||||
{
|
||||
string scriptPath = PYTHON_COOKIE_RETRIEVAL_FILENAME;
|
||||
@@ -51,43 +54,56 @@ namespace Test_Merge
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
}
|
||||
/// <summary>
|
||||
/// Method that will recover the needed cookies in the DB
|
||||
/// </summary>
|
||||
/// <param name="host"> The host of the wanted cookie ex: ./formula1.com</param>
|
||||
/// <param name="name">The name of the wanted cookie ex: login</param>
|
||||
/// <returns>returns the value of the cookie if it has been found</returns>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public string GetCookie(string host, string name)
|
||||
{
|
||||
StartCookieRecovering();
|
||||
string value = "";
|
||||
List<Cookie> cookies = new List<Cookie>();
|
||||
using (var reader = new StreamReader(COOKIES_CSV_FILENAME))
|
||||
if (File.Exists(COOKIES_CSV_FILENAME))
|
||||
{
|
||||
// Read the header row and validate column order
|
||||
string header = reader.ReadLine();
|
||||
string[] expectedColumns = { "host_key", "name", "value", "path", "expires_utc", "is_secure", "is_httponly" };
|
||||
string[] actualColumns = header.Split(',');
|
||||
for (int i = 0; i < expectedColumns.Length; i++)
|
||||
using (var reader = new StreamReader(COOKIES_CSV_FILENAME))
|
||||
{
|
||||
if (expectedColumns[i] != actualColumns[i])
|
||||
// Read the header row and validate column order
|
||||
string header = reader.ReadLine();
|
||||
string[] expectedColumns = { "host_key", "name", "value", "path", "expires_utc", "is_secure", "is_httponly" };
|
||||
string[] actualColumns = header.Split(',');
|
||||
for (int i = 0; i < expectedColumns.Length; i++)
|
||||
{
|
||||
throw new InvalidOperationException($"Expected column '{expectedColumns[i]}' at index {i} but found '{actualColumns[i]}'");
|
||||
if (expectedColumns[i] != actualColumns[i])
|
||||
{
|
||||
throw new InvalidOperationException($"Expected column '{expectedColumns[i]}' at index {i} but found '{actualColumns[i]}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Read each data row and parse values into a Cookie object
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
string line = reader.ReadLine();
|
||||
string[] fields = line.Split(',');
|
||||
|
||||
string hostname = fields[0];
|
||||
string cookieName = fields[1];
|
||||
|
||||
if (hostname == host && cookieName == name)
|
||||
// Read each data row and parse values into a Cookie object
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
value = fields[2];
|
||||
string line = reader.ReadLine();
|
||||
string[] fields = line.Split(',');
|
||||
|
||||
string hostname = fields[0];
|
||||
string cookieName = fields[1];
|
||||
|
||||
if (hostname == host && cookieName == name)
|
||||
{
|
||||
value = fields[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
/// <summary>
|
||||
/// Starts the headless browser
|
||||
/// </summary>
|
||||
/// <returns>Error code 1xx</returns>
|
||||
public async Task<int> Start()
|
||||
{
|
||||
Ready = false;
|
||||
@@ -97,8 +113,9 @@ namespace Test_Merge
|
||||
string loginCookieValue = GetCookie(COOKIE_HOST, loginCookieName);
|
||||
string loginSessionValue = GetCookie(COOKIE_HOST, loginSessionCookieName);
|
||||
|
||||
int windowWidth = 1920;
|
||||
int windowHeight = 768;
|
||||
//Cookie retreival has gone wrong (usually its because of python not being installed properly)
|
||||
if (loginCookieValue == "" || loginSessionValue == "")
|
||||
return 100;
|
||||
|
||||
var service = FirefoxDriverService.CreateDefaultService(GECKODRIVER_FILENAME);
|
||||
service.Host = "127.0.0.1";
|
||||
@@ -193,10 +210,10 @@ namespace Test_Merge
|
||||
// It does not matter if the feed is paused because when changing channel it autoplays
|
||||
actions.SendKeys(OpenQA.Selenium.Keys.Space).Perform();
|
||||
//Clicks on the settings Icon
|
||||
|
||||
int tries = 0;
|
||||
bool success = false;
|
||||
while (tries < 100 && !success)
|
||||
|
||||
int settingsClickTries = 0;
|
||||
bool settingsClickSuccess = false;
|
||||
while (settingsClickTries < 100 && !settingsClickSuccess)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
try
|
||||
@@ -207,17 +224,17 @@ namespace Test_Merge
|
||||
SelectElement select = new SelectElement(selectElement);
|
||||
IWebElement selectOption = selectElement.FindElement(By.CssSelector("option[value^='1080_']"));
|
||||
selectOption.Click();
|
||||
success = true;
|
||||
settingsClickSuccess = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Sometimes it can crash because it could not get the options to show up in time. When it happens just retry
|
||||
success = false;
|
||||
tries++;
|
||||
settingsClickSuccess = false;
|
||||
settingsClickTries++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!success)
|
||||
|
||||
if (!settingsClickSuccess)
|
||||
{
|
||||
Screenshot("ERROR105");
|
||||
Driver.Dispose();
|
||||
@@ -227,15 +244,27 @@ namespace Test_Merge
|
||||
Screenshot("BEFOREFULLSCREEN");
|
||||
|
||||
//Makes the feed fullscreen
|
||||
//Driver.Manage().Window.Size = new System.Drawing.Size(windowWidth, windowHeight);
|
||||
int fullScreenClickTries = 0;
|
||||
bool fullScreenClickSuccess = false;
|
||||
Driver.Manage().Window.Maximize();
|
||||
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
|
||||
try
|
||||
//WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
|
||||
while (fullScreenClickTries < 100 && !fullScreenClickSuccess)
|
||||
{
|
||||
IWebElement fullScreenButton = Driver.FindElement(By.ClassName("bmpui-ui-fullscreentogglebutton"));
|
||||
fullScreenButton.Click();
|
||||
Thread.Sleep(150);
|
||||
try
|
||||
{
|
||||
IWebElement fullScreenButton = Driver.FindElement(By.ClassName("bmpui-ui-fullscreentogglebutton"));
|
||||
fullScreenButton.Click();
|
||||
fullScreenClickSuccess = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
fullScreenClickSuccess = false;
|
||||
fullScreenClickTries++;
|
||||
}
|
||||
}
|
||||
catch
|
||||
|
||||
if (!fullScreenClickSuccess)
|
||||
{
|
||||
Screenshot("ERROR106");
|
||||
Driver.Dispose();
|
||||
@@ -248,18 +277,22 @@ namespace Test_Merge
|
||||
Ready = true;
|
||||
return 0;
|
||||
}
|
||||
/// <summary>
|
||||
/// Takes a screenshot of what the headless browser is displaying
|
||||
/// </summary>
|
||||
/// <param name="name">Optional ! The name of the picture so it can be saved</param>
|
||||
/// <returns>Returns the screenshot in the bitmap format</returns>
|
||||
public Bitmap Screenshot(string name = "TEST")
|
||||
{
|
||||
Bitmap result = new Bitmap(4242, 6969);
|
||||
try
|
||||
{
|
||||
//Screenshot scrsht = ((ITakesScreenshot)Driver).GetScreenshot();
|
||||
//profileriver.SetPreference("layout.css.devPixelsPerPx", "1.0");
|
||||
|
||||
//profileriver.SetPreferencC:\Users\Moi\source\repos\Test_Merge\README.mde("layout.css.devPixelsPerPx", "1.0");
|
||||
|
||||
//Screenshot scrsht = Driver.GetFullPageScreenshot();
|
||||
Screenshot scrsht = Driver.GetScreenshot();
|
||||
|
||||
|
||||
byte[] screenshotBytes = Convert.FromBase64String(scrsht.AsBase64EncodedString);
|
||||
MemoryStream stream = new MemoryStream(screenshotBytes);
|
||||
|
||||
@@ -273,15 +306,23 @@ namespace Test_Merge
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Stops the Emulation. Note: if you plan to start it again please use ResetDriver() instead
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
Ready = false;
|
||||
Driver.Dispose();
|
||||
if (Driver != null)
|
||||
Driver.Dispose();
|
||||
}
|
||||
/// <summary>
|
||||
/// Resets the emulation
|
||||
/// </summary>
|
||||
public void ResetDriver()
|
||||
{
|
||||
Ready = false;
|
||||
Driver.Dispose();
|
||||
if (Driver != null)
|
||||
Driver.Dispose();
|
||||
Driver = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user