AutoTestService.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using AutoTestApplication.Common;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace AutoTestApplication
  9. {
  10. public class AutoTestService
  11. {
  12. /// <summary>
  13. /// 运行机器人报告
  14. /// </summary>
  15. public void RunRobotFrameworkReport()
  16. {
  17. try
  18. {
  19. WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": Start Run RunRobotFrameworkReport");
  20. var maxCount = CommonConfigManager.MaxExcuteCount;
  21. var curDateRecord = CommonConfigManager.Records.Find(d => d.ExcuteDate.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"));
  22. if (curDateRecord?.ExcuteRecords?.Count >= maxCount)
  23. {
  24. //大于5次不执行
  25. Console.WriteLine($"Today's execution count is greater than the daily maximum execution limit {maxCount}, Automated test report not executed");
  26. return;
  27. }
  28. //开始比较版本,并copy文件
  29. CheckAndCopyFile();
  30. var scheduledBeginTime = DateTime.Now;
  31. var outputResult = ProcessStarter.StartCmdProcess(CommonConfigManager.ExcuteCmdConfig);
  32. Console.WriteLine("Executed Result: " + outputResult);
  33. var scheduledEndTime = DateTime.Now;
  34. //记录今日执行次数
  35. if (curDateRecord == null)
  36. {
  37. curDateRecord = new RecordEntity()
  38. {
  39. ExcuteDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")),
  40. ExcuteRecords = new List<ExcuteRecordsEntity>()
  41. };
  42. CommonConfigManager.Records.Add(curDateRecord);
  43. }
  44. var recordsEntity = new ExcuteRecordsEntity()
  45. {
  46. ExcuteStartTime = scheduledBeginTime,
  47. ExcuteEndTime = scheduledEndTime
  48. };
  49. curDateRecord.ExcuteRecords.Add(recordsEntity);
  50. WriteJsonFile();
  51. if (outputResult.IndexOf("465") > 0)
  52. {
  53. Console.WriteLine(scheduledEndTime.ToString("yyyy-MM-dd HH:mm:ss") + ": Run the automated test report success");
  54. }
  55. else
  56. {
  57. Console.WriteLine(scheduledEndTime.ToString("yyyy-MM-dd HH:mm:ss") + ": " + CommonConfigManager.ExcuteCmdConfig + "Run the automated test report fail");
  58. }
  59. WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": End Run RunRobotFrameworkReport");
  60. }
  61. catch (Exception ex)
  62. {
  63. Console.WriteLine("RunRobotFrameworkReport error: " + ex.Message);
  64. WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Error: " + ex.Message);
  65. }
  66. }
  67. /// <summary>
  68. /// 写入日志
  69. /// </summary>
  70. /// <param name="message"></param>
  71. private void WriteLog(string message)
  72. {
  73. var logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config", "Log.txt");
  74. using (StreamWriter streamWriter = new StreamWriter(logPath, true))
  75. {
  76. streamWriter.WriteLine(message);
  77. }
  78. }
  79. /// <summary>
  80. /// 监测并copy文件
  81. /// </summary>
  82. /// <returns></returns>
  83. private bool CheckAndCopyFile()
  84. {
  85. var result = false;
  86. try
  87. {
  88. var version = GetVersion();
  89. if (!string.IsNullOrEmpty(version))
  90. {
  91. if (version != CommonConfigManager.CurrentVersion)
  92. {
  93. //需要更新
  94. //copy art文件
  95. string sourceFile = CommonConfigManager.SourceFilePath + "\\" + CommonConfigManager.RideFileName;
  96. string destinationFile = CommonConfigManager.RideArtPath + "\\" + CommonConfigManager.RideFileName;
  97. bool isrewrite = true; // true=覆盖已存在的同名文件,false则反之
  98. System.IO.File.Copy(sourceFile, destinationFile, isrewrite);
  99. //copy custom 文件夹及子项
  100. var strFromPath = CommonConfigManager.SourceFilePath + "\\" + CommonConfigManager.CustomFileName;
  101. var strToPath = CommonConfigManager.CustomPyPath;
  102. DirectoryInfo target = new DirectoryInfo(strFromPath);
  103. CopyToDirect(target, strToPath, true);
  104. //copy robot 文件夹及子项
  105. if (CommonConfigManager.RobotFileNames?.Count > 0)
  106. {
  107. foreach (var item in CommonConfigManager.RobotFileNames)
  108. {
  109. strFromPath = CommonConfigManager.SourceFilePath + "\\" + item;
  110. strToPath = CommonConfigManager.RobotRunPath + "\\" + item;
  111. target = new DirectoryInfo(strFromPath);
  112. CopyToDirect(target, strToPath, true);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. Console.WriteLine("CheckAndCopyFile异常:" + ex.Message);
  121. }
  122. return result;
  123. }
  124. /// <summary>
  125. /// 递归copy文件夹
  126. /// </summary>
  127. /// <param name="strFromPath"></param>
  128. /// <param name="strToPath"></param>
  129. private void CopyToDirect(DirectoryInfo source, string destDirectory, bool recursive)
  130. {
  131. if (source == null)
  132. throw new ArgumentNullException("source");
  133. if (destDirectory == null)
  134. throw new ArgumentNullException("destDirectory");
  135. // If the source doesn't exist, we have to throw an exception.
  136. if (!source.Exists)
  137. throw new DirectoryNotFoundException(
  138. "Source directory not found:" + source.FullName);
  139. // Compile the target.
  140. DirectoryInfo target = new DirectoryInfo(destDirectory);
  141. // If the target doesn't exist, we create it.
  142. if (!target.Exists)
  143. target.Create();
  144. // Get all files and copy them over.
  145. foreach (FileInfo file in source.GetFiles())
  146. {
  147. file.CopyTo(Path.Combine(target.FullName, file.Name), true);
  148. }
  149. // Return if no recursive call is required.
  150. if (!recursive)
  151. return;
  152. // Do the same for all sub directories.
  153. foreach (DirectoryInfo directory in source.GetDirectories())
  154. {
  155. CopyToDirect(directory,
  156. Path.Combine(target.FullName, directory.Name), recursive);
  157. }
  158. }
  159. /// <summary>
  160. /// 获取到制定文件目录的version
  161. /// </summary>
  162. /// <returns></returns>
  163. private string GetVersion()
  164. {
  165. string version = string.Empty;
  166. var filePath = CommonConfigManager.SourceFilePath + "\\version.txt";
  167. using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
  168. {
  169. using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
  170. {
  171. version = sr.ReadToEnd().ToString();
  172. }
  173. }
  174. return version;
  175. }
  176. /// <summary>
  177. /// 写入文件
  178. /// </summary>
  179. /// <returns></returns>
  180. private void WriteJsonFile()
  181. {
  182. var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config", "AppSetting.json");
  183. var model = new AppSettingEntity()
  184. {
  185. MaxExcuteCount = CommonConfigManager.MaxExcuteCount,
  186. ExcuteCmdConfig = CommonConfigManager.ExcuteCmdConfig,
  187. Records = CommonConfigManager.Records,
  188. CurrentVersion = CommonConfigManager.CurrentVersion,
  189. SourceFilePath = CommonConfigManager.SourceFilePath,
  190. RobotRunPath = CommonConfigManager.RobotRunPath,
  191. CustomPyPath = CommonConfigManager.CustomPyPath,
  192. RideArtPath = CommonConfigManager.RideArtPath,
  193. CustomFileName = CommonConfigManager.CustomFileName,
  194. RideFileName = CommonConfigManager.RideFileName,
  195. RobotFileNames = CommonConfigManager.RobotFileNames,
  196. };
  197. var jsonConents = JsonConvert.SerializeObject(model);
  198. File.WriteAllText(path, jsonConents, System.Text.Encoding.UTF8);
  199. }
  200. }
  201. }