AutoTestService.cs 8.0 KB

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