123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using AutoTestApplication.Common;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace AutoTestApplication
- {
- public class AutoTestService
- {
- /// <summary>
- /// 运行机器人报告
- /// </summary>
- public void RunRobotFrameworkReport()
- {
- try
- {
- var maxCount = CommonConfigManager.MaxExcuteCount;
- var curDateRecord = CommonConfigManager.Records.Find(d => d.ExcuteDate.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"));
- if (curDateRecord?.ExcuteRecords?.Count >= maxCount)
- {
- //大于5次不执行
- Console.WriteLine($"今日执行次数大于每日执行最大次数{maxCount}, 自动化测试报告不执行");
- return;
- }
- //开始比较版本,并copy文件
- CheckAndCopyFile();
- var scheduledBeginTime = DateTime.Now;
- var outputResult = ProcessStarter.StartCmdProcess(CommonConfigManager.ExcuteCmdConfig);
- var scheduledEndTime = DateTime.Now;
- //记录今日执行次数
- if (curDateRecord == null)
- {
- curDateRecord = new RecordEntity()
- {
- ExcuteDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")),
- ExcuteRecords = new List<ExcuteRecordsEntity>()
- };
- CommonConfigManager.Records.Add(curDateRecord);
- }
- var recordsEntity = new ExcuteRecordsEntity()
- {
- ExcuteStartTime = scheduledBeginTime,
- ExcuteEndTime = scheduledEndTime
- };
- curDateRecord.ExcuteRecords.Add(recordsEntity);
- WriteJsonFile();
- if (outputResult.IndexOf("465") > 0)
- {
- Console.WriteLine(scheduledEndTime.ToString("yyyy-MM-dd HH:mm:ss") + ":执行自动化测试报告Success");
- }
- else
- {
- Console.WriteLine(scheduledEndTime.ToString("yyyy-MM-dd HH:mm:ss") + ":执行自动化测试报告Fail");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("RunRobotFrameworkReport异常:" + ex.Message);
- }
- }
- /// <summary>
- /// 监测并copy文件
- /// </summary>
- /// <returns></returns>
- private bool CheckAndCopyFile()
- {
- var result = false;
- try
- {
- var version = GetVersion();
- if (!string.IsNullOrEmpty(version))
- {
- if (version != CommonConfigManager.CurrentVersion)
- {
- //需要更新
- //copy art文件
- string sourceFile = CommonConfigManager.SourceFilePath + "\\" + CommonConfigManager.RideFileName;
- string destinationFile = CommonConfigManager.RideArtPath + "\\" + CommonConfigManager.RideFileName;
- bool isrewrite = true; // true=覆盖已存在的同名文件,false则反之
- System.IO.File.Copy(sourceFile, destinationFile, isrewrite);
- //copy custom 文件夹及子项
- var strFromPath = CommonConfigManager.SourceFilePath + "\\" + CommonConfigManager.CustomFileName;
- var strToPath = CommonConfigManager.CustomPyPath;
- DirectoryInfo target = new DirectoryInfo(strFromPath);
- CopyToDirect(target, strToPath, true);
- //copy robot 文件夹及子项
- if (CommonConfigManager.RobotFileNames?.Count > 0)
- {
- foreach (var item in CommonConfigManager.RobotFileNames)
- {
- strFromPath = CommonConfigManager.SourceFilePath + "\\" + item;
- strToPath = CommonConfigManager.RobotRunPath + "\\" + item;
- target = new DirectoryInfo(strFromPath);
- CopyToDirect(target, strToPath, true);
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("CheckAndCopyFile异常:" + ex.Message);
- }
- return result;
- }
- /// <summary>
- /// 递归copy文件夹
- /// </summary>
- /// <param name="strFromPath"></param>
- /// <param name="strToPath"></param>
- private void CopyToDirect(DirectoryInfo source, string destDirectory, bool recursive)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- if (destDirectory == null)
- throw new ArgumentNullException("destDirectory");
- // If the source doesn't exist, we have to throw an exception.
- if (!source.Exists)
- throw new DirectoryNotFoundException(
- "Source directory not found:" + source.FullName);
- // Compile the target.
- DirectoryInfo target = new DirectoryInfo(destDirectory);
- // If the target doesn't exist, we create it.
- if (!target.Exists)
- target.Create();
- // Get all files and copy them over.
- foreach (FileInfo file in source.GetFiles())
- {
- file.CopyTo(Path.Combine(target.FullName, file.Name), true);
- }
- // Return if no recursive call is required.
- if (!recursive)
- return;
- // Do the same for all sub directories.
- foreach (DirectoryInfo directory in source.GetDirectories())
- {
- CopyToDirect(directory,
- Path.Combine(target.FullName, directory.Name), recursive);
- }
- }
- /// <summary>
- /// 获取到制定文件目录的version
- /// </summary>
- /// <returns></returns>
- private string GetVersion()
- {
- string version = string.Empty;
- var filePath = CommonConfigManager.SourceFilePath + "\\version.txt";
- using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
- {
- using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
- {
- version = sr.ReadToEnd().ToString();
- }
- }
- return version;
- }
-
- /// <summary>
- /// 写入文件
- /// </summary>
- /// <returns></returns>
- private void WriteJsonFile()
- {
- var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Config", "AppSetting.json");
- var model = new AppSettingEntity()
- {
- MaxExcuteCount = CommonConfigManager.MaxExcuteCount,
- ExcuteCmdConfig = CommonConfigManager.ExcuteCmdConfig,
- Records = CommonConfigManager.Records,
- CurrentVersion = CommonConfigManager.CurrentVersion,
- SourceFilePath = CommonConfigManager.SourceFilePath,
- RobotRunPath = CommonConfigManager.RobotRunPath,
- CustomPyPath = CommonConfigManager.CustomPyPath,
- RideArtPath = CommonConfigManager.RideArtPath,
- CustomFileName = CommonConfigManager.CustomFileName,
- RideFileName = CommonConfigManager.RideFileName,
- RobotFileNames = CommonConfigManager.RobotFileNames,
- };
- var jsonConents = JsonConvert.SerializeObject(model);
- File.WriteAllText(path, jsonConents, System.Text.Encoding.UTF8);
- }
- }
- }
|