Not everything works but added most of the old features

This commit is contained in:
2023-05-02 10:51:19 +02:00
parent 044a00e167
commit 221fb88e08
2 changed files with 132 additions and 13 deletions

View File

@@ -1,5 +1,7 @@
using OpenQA.Selenium; using OpenQA.Selenium;
using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@@ -16,7 +18,7 @@ namespace Test_Merge
{ {
public const string COOKIE_HOST = ".formula1.com"; public const string COOKIE_HOST = ".formula1.com";
public const string PYTHON_COOKIE_RETRIEVAL_FILENAME = "recoverCookiesCSV.py"; public const string PYTHON_COOKIE_RETRIEVAL_FILENAME = "recoverCookiesCSV.py";
public const string GECKODRIVER_FILENAME = @"geckodriver-v0.27.0-win32\geckodriver.exe"; public const string GECKODRIVER_FILENAME = @"geckodriver-v0.27.0-win64\geckodriver.exe";
//BE CAREFULL IF YOU CHANGE IT HERE YOU NEED TO CHANGE IT IN THE PYTHON SCRIPT TOO //BE CAREFULL IF YOU CHANGE IT HERE YOU NEED TO CHANGE IT IN THE PYTHON SCRIPT TOO
public const string COOKIES_CSV_FILENAME = "cookies.csv"; public const string COOKIES_CSV_FILENAME = "cookies.csv";
@@ -82,20 +84,29 @@ namespace Test_Merge
} }
public async Task<int> Start() public async Task<int> Start()
{ {
var service = FirefoxDriverService.CreateDefaultService(AppDomain.CurrentDomain.BaseDirectory + @"geckodriver-v0.27.0-win64\geckodriver.exe"); Ready = false;
string loginCookieName = "login";
string loginSessionCookieName = "login-session";
string loginCookieValue = GetCookie(COOKIE_HOST, loginCookieName);
string loginSessionValue = GetCookie(COOKIE_HOST, loginSessionCookieName);
int windowWidth = 1920;
int windowHeight = 1200;
var service = FirefoxDriverService.CreateDefaultService(GECKODRIVER_FILENAME);
service.Host = "127.0.0.1"; service.Host = "127.0.0.1";
service.Port = 5555; service.Port = 5555;
FirefoxOptions options = new FirefoxOptions(); FirefoxOptions options = new FirefoxOptions();
options.AcceptInsecureCertificates = true; options.AcceptInsecureCertificates = true;
options.AddArgument("--headless"); options.AddArgument("--headless");
options.AddArgument("--width=1920"); options.AddArgument("--width="+windowWidth);
options.AddArgument("--height=1200"); options.AddArgument("--height="+windowHeight);
try try
{ {
Driver = new FirefoxDriver(service, options); Driver = new FirefoxDriver(service, options);
Ready = true;
} }
catch catch
{ {
@@ -103,27 +114,132 @@ namespace Test_Merge
return 101; return 101;
} }
Driver.Navigate().GoToUrl("https://f1tv.formula1.com/"); Actions actions = new Actions(Driver);
Thread.Sleep(2000); var loginCookie = new Cookie(loginCookieName, loginCookieValue, COOKIE_HOST, "/", DateTime.Now.AddDays(5));
Screenshot("AAAAAAAAAAA"); var loginSessionCookie = new Cookie(loginSessionCookieName, loginSessionValue, COOKIE_HOST, "/", DateTime.Now.AddDays(5));
Driver.Navigate().GoToUrl("https://f1tv.formula1.com/");
Driver.Manage().Cookies.AddCookie(loginCookie);
Driver.Manage().Cookies.AddCookie(loginSessionCookie);
try
{
Driver.Navigate().GoToUrl(GrandPrixUrl);
}
catch
{
//The url is not a valid url
Driver.Dispose();
return 103;
}
//Waits for the page to fully load
Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
//Removes the cookie prompt
try
{
IWebElement conscentButton = Driver.FindElement(By.Id("truste-consent-button"));
conscentButton.Click();
}
catch
{
//Could not locate the cookie button
Driver.Dispose();
return 104;
}
//Again waits for the page to fully load (when you accept cookies it takes a little time for the page to load)
//Cannot use The timeout because the feed loading is not really loading so there is not event or anything
Thread.Sleep(5000);
//Switches to the Data channel
try
{
IWebElement dataChannelButton = Driver.FindElement(By.ClassName("data-button"));
dataChannelButton.Click();
}
catch
{
//If the data button does not exists its because the user is not connected
Screenshot("ERROR102");
Driver.Dispose();
return 102;
}
//Open settings
// Press the space key, this should make the setting button visible
// 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 < 50 && !success)
{
Thread.Sleep(100);
try
{
IWebElement settingsButton = Driver.FindElement(By.ClassName("bmpui-ui-settingstogglebutton"));
settingsButton.Click();
IWebElement selectElement = Driver.FindElement(By.ClassName("bmpui-ui-videoqualityselectbox"));
SelectElement select = new SelectElement(selectElement);
IWebElement selectOption = selectElement.FindElement(By.CssSelector("option[value^='1080_']"));
selectOption.Click();
success = 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++;
}
}
if(success = false)
{
Screenshot("ERROR105");
Driver.Dispose();
return 105;
}
//Makes the feed fullscreen
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
try
{
IWebElement fullScreenButton = Driver.FindElement(By.ClassName("bmpui-ui-fullscreentogglebutton"));
fullScreenButton.Click();
}
catch
{
Screenshot("ERROR106");
Driver.Dispose();
return 106;
}
//STARTUP FINISHED READY TO SCREENSHOT
Ready = true;
return 0; return 0;
} }
public Bitmap Screenshot(string name = "TEST") public Bitmap Screenshot(string name = "TEST")
{ {
Bitmap result = new Bitmap(100, 100); Bitmap result = new Bitmap(4242, 6969);
if (Ready) try
{ {
//IWebElement element = Driver.FindElement(By.Id("bitmovinplayer-video-slave-embeddedPlayer"));
//Screenshot scrsht = ((ITakesScreenshot)element).GetScreenshot();
Screenshot scrsht = ((ITakesScreenshot)Driver).GetScreenshot(); Screenshot scrsht = ((ITakesScreenshot)Driver).GetScreenshot();
byte[] screenshotBytes = Convert.FromBase64String(scrsht.AsBase64EncodedString); byte[] screenshotBytes = Convert.FromBase64String(scrsht.AsBase64EncodedString);
MemoryStream stream = new MemoryStream(screenshotBytes); MemoryStream stream = new MemoryStream(screenshotBytes);
result = new Bitmap(stream); result = new Bitmap(stream);
result.Save(name + ".png");
}
catch
{
//Nothing for now
} }
result.Save(name+".png");
return result; return result;
} }
public void Stop() public void Stop()

View File

@@ -329,6 +329,9 @@ namespace Test_Merge
case 105: case 105:
message = "Error " + errorCode + " There has been an error trying to emulate button presses. Please try again"; message = "Error " + errorCode + " There has been an error trying to emulate button presses. Please try again";
break; break;
case 106:
message = "Error " + errorCode + " There has been an error trying to emulate button presses. Please try again";
break;
default: default:
message = "Could not start the emulator Error " + errorCode; message = "Could not start the emulator Error " + errorCode;
break; break;