You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.1 KiB

  1. 
  2. namespace SeleniumBrowser
  3. {
  4. using Newtonsoft.Json;
  5. using OpenQA.Selenium;
  6. using OpenQA.Selenium.Chrome;
  7. using SeleniumBrowser.Command;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Runtime.InteropServices;
  15. public class Commander
  16. {
  17. [DllImport("User32.dll")]
  18. static extern int SetForegroundWindow(IntPtr point);
  19. public const string FFMPEG_FILE = "ffmpeg.exe";
  20. public const string RECORD_FILE = "Record.mp4";
  21. private IWebDriver InitBrowser(BrowserInfo i_bi)
  22. {
  23. IWebDriver iwd = null;
  24. if (i_bi.Name.ToLower() == "chrome")
  25. {
  26. ChromeOptions options = new ChromeOptions();
  27. i_bi.Parameters.ForEach(sPara => options.AddArgument(sPara));
  28. iwd = new ChromeDriver(Environment.CurrentDirectory, options);
  29. }
  30. return iwd;
  31. }
  32. public string RunAction(BrowserInfo i_bi, List<CommandInfo> i_laiCmds,
  33. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  34. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = null,
  35. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = null)
  36. {
  37. string sMsg = null;
  38. CommanderGlobalData cgd = new CommanderGlobalData();
  39. if (!Directory.Exists(cgd.StotrFolder))
  40. {
  41. Directory.CreateDirectory(cgd.StotrFolder);
  42. }
  43. // Start Recording
  44. ProcessStartInfo startInfo = new ProcessStartInfo(FFMPEG_FILE)
  45. {
  46. WindowStyle = ProcessWindowStyle.Minimized
  47. };
  48. string sRecordPath = Path.Combine(cgd.StotrFolder, RECORD_FILE);
  49. // https://stackoverflow.com/questions/6766333/capture-windows-screen-with-ffmpeg
  50. // ffmpeg -y -rtbufsize 100M -f gdigrab -t 00:00:30 -framerate 30 -probesize 10M -draw_mouse 1 -i desktop -c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 25 -pix_fmt yuv420p c:/video_comapre2.mp4
  51. startInfo.Arguments = makeRecordCommand(i_laiCmds.Sum(f => 2 + f.SleepAfterEvent), sRecordPath);
  52. Process.Start(startInfo);
  53. IWebDriver iwdBrowser = null;
  54. try
  55. {
  56. do
  57. {
  58. iwdBrowser = InitBrowser(i_bi);
  59. if (iwdBrowser == null)
  60. {
  61. sMsg = "NO_BROWSER";
  62. break;
  63. }
  64. ConcurrentDictionary<string, object> cdGlobalData = new ConcurrentDictionary<string, object>();
  65. foreach (CommandInfo ai in i_laiCmds)
  66. {
  67. ai.BatchProcessData = cgd;
  68. #if DEBUG
  69. System.Diagnostics.Debug.WriteLine($"Command={ai.Command} Target={ai.Target} title={iwdBrowser.CurrentWindowHandle}");
  70. #endif
  71. sMsg = ai.Process(iwdBrowser, ref cdGlobalData);
  72. if (sMsg != null)
  73. {
  74. break;
  75. }
  76. cgd.CommandCount++;
  77. }
  78. }
  79. while (false);
  80. }
  81. catch (Exception ex)
  82. {
  83. sMsg = $"{nameof(RunAction)} unhandle exception !. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
  84. #if DEBUG
  85. sMsg += ex.Message;
  86. #endif
  87. }
  88. #if DEBUG
  89. if (sMsg != null)
  90. {
  91. System.Diagnostics.Debug.WriteLine(sMsg);
  92. }
  93. #endif
  94. if (iwdBrowser != null)
  95. {
  96. try
  97. {
  98. iwdBrowser.Close();
  99. iwdBrowser.Quit();
  100. }
  101. catch
  102. {
  103. // Ignore
  104. }
  105. }
  106. return sMsg;
  107. }
  108. public string RunAction(BrowserInfo i_bi, string i_sJsonCommands,
  109. [System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
  110. [System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = null,
  111. [System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = null)
  112. {
  113. return RunAction(i_bi, JsonConvert.DeserializeObject<List<CommandInfo>>(i_sJsonCommands), i_nCodeLine, i_sMemberName, i_sSourcePath);
  114. }
  115. protected string makeRecordCommand(int nRecordTime, string sRecordPath)
  116. {
  117. int nRecordSecondMod = nRecordTime % 60;
  118. int nRecordMinute = nRecordTime / 60;
  119. int nRecordMinuteMod = nRecordMinute % 60;
  120. int nHour = nRecordMinute / 60;
  121. return $"-y -rtbufsize 100M -f gdigrab -t {nHour:00}:{nRecordMinuteMod:00}:{nRecordSecondMod:00} -framerate 30 -probesize 10M -draw_mouse 1 -i desktop -c:v libx264 -r 30 -preset ultrafast -tune zerolatency -crf 25 -pix_fmt yuv420p {sRecordPath}";
  122. }
  123. }
  124. }