Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2023
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. using Azure.Storage.Blobs;
  2. using Screenshot_WorkerService.Interface;
  3. using Screenshot_WorkerService.Service;
  4. using OpenQA.Selenium.Chrome;
  5. using OpenQA.Selenium.Support.UI;
  6. using OpenQA.Selenium;
  7. using OpenQA.Selenium.Support.Extensions;
  8. using System.Drawing;
  9.  
  10. namespace Screenshot_WorkerService
  11. {
  12. public class Worker : BackgroundService
  13. {
  14. private readonly ILogger<Worker> _logger;
  15. private readonly IBlobStorageServiceClient _blobServiceClient;
  16.  
  17. public Worker(ILogger<Worker> logger, IBlobStorageServiceClient blobServiceClient)
  18. {
  19. _logger = logger;
  20. _blobServiceClient = blobServiceClient;
  21. }
  22.  
  23. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  24. {
  25. while (!stoppingToken.IsCancellationRequested)
  26. {
  27. _logger.LogInformation("Worker running at: {time}", DateTime.Now);
  28. var timeout = 10;
  29.  
  30. _logger.LogInformation("Reading urls values from blob");
  31. var urlConfiguration = await _blobServiceClient.DownloadConfigrationBlob();
  32.  
  33. if (urlConfiguration.Urls is not null)
  34. {
  35. foreach (var item in urlConfiguration.Urls)
  36. {
  37. _logger.LogInformation("Preparing headless web browser");
  38. var options = new ChromeOptions();
  39. options.AddArguments(new List<string> { "--headless", "--hide-scrollbars" });
  40.  
  41. using var driver = new ChromeDriver(options);
  42. _logger.LogInformation("web browser complete");
  43. driver.Manage().Window.Size = new Size(item.Width, item.Height);
  44. driver.Navigate().GoToUrl(item.Url);
  45. driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(timeout);
  46.  
  47. try
  48. {
  49. _logger.LogInformation($"Searching class element: {timeout}");
  50. var element = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until((d) =>
  51. {
  52. d.Navigate().GoToUrl(item.Url);
  53. Thread.Sleep(5000);
  54. return d.FindElement(By.ClassName("v-menu"));
  55. });
  56.  
  57. var screenshot = driver.TakeScreenshot();
  58. #if DEBUG
  59. screenshot.SaveAsFile($"{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.png");
  60. #endif
  61. //Save to blobStorage
  62. _blobServiceClient.UploadImageToBlobAsync(screenshot);
  63.  
  64. }
  65. catch (NoSuchElementException e)
  66. {
  67. // try Login
  68. _logger.LogInformation($"Exception: {e.Message}");
  69. _logger.LogInformation($"Trying automate login");
  70.  
  71. var username = driver.FindElement(By.Id("input-12\""));
  72. username.Clear();
  73. username.SendKeys("");
  74.  
  75. var password = driver.FindElement(By.Id("input-13"));
  76. password.Clear();
  77. password.SendKeys("");
  78.  
  79. var loginButton = driver.FindElement(By.XPath("//button[@class='v-btn v-btn--block v-btn--outlined theme--dark v-size--default primary--text']\r\n"));
  80. loginButton.Click();
  81.  
  82. driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(timeout);
  83.  
  84. var screenshot = driver.TakeScreenshot();
  85.  
  86. #if DEBUG
  87. screenshot.SaveAsFile($"{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")}.png");
  88. #endif
  89. //Save to blobStorage
  90. _blobServiceClient.UploadImageToBlobAsync(screenshot);
  91. }
  92. catch (WebDriverTimeoutException e)
  93. {
  94. _logger.LogInformation($"Exception trying login was timed out: {e.Message}");
  95. }
  96. catch (Exception e) {
  97. _logger.LogInformation($"Exception: {e.Message}");
  98. }
  99. }
  100. }
  101. else
  102. {
  103. _logger.LogWarning("Reading returned no urls, check the blobstorage for url values");
  104. }
  105. _logger.LogWarning("Done!");
  106. await Task.Delay(TimeSpan.FromMinutes(20), stoppingToken);
  107. }
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement