/// <summary> /// 保存案例脚本 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveScript_Click(object sender, RoutedEventArgs e) { PrintHelper.Print.PrintData = this.PrintData; string result = ""; try { if (mv.CaseScripts.Count == 0 || mv.SelectScript.FullPath == null) { PrintHelper.Print.AppendLine("请先选中要保存的脚本!"); return; } result = CSVFileHelper.SaveScript(mv.SelectScript.FullPath, mv.SrciptDoc.Text); string caseName = mv.SelectScript.CaseName; string fullPath = mv.SelectScript.FullPath; //重新加载项目 mv.LoadScriptInfo(mv.SelectProject.FullPath); //将焦点重置回去 mv.SelectScript = mv.CaseScripts.FirstOrDefault(o => o.FullPath == fullPath); PrintHelper.Print.AppendLine(result); } catch (Exception error) { PrintHelper.Print.AppendLine(error.Message + "\r\n" + error.StackTrace); } }
/// <summary> /// 一键运行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnRunAll_Click(object sender, RoutedEventArgs e) { Compile cp = new Compile(); Task.Run( () => { int i = -1; foreach (CaseScript Script in mv.CaseScripts) { i++; string result = "";//案例执行结果 成功/失败 string info = CSVFileHelper.OpenScript(Script.FullPath).ToString(); string[] textArry = info.Split('\n'); PrintHelper.Print.PrintData = this.PrintData; PrintHelper.Print.AppendLine($"\r\n**********开始执行案例:{Script.CaseName}**********"); cp.Run(textArry, ref result); DataGridRow row = (DataGridRow)this.dgCase.ItemContainerGenerator.ContainerFromIndex(i); if (result != "") { this.Dispatcher.Invoke(() => { row.Foreground = new SolidColorBrush(Colors.Red); }); MessageBox.Show($"案例{Script.CaseName}执行失败,error:{result}", "异常"); continue; } else { this.Dispatcher.Invoke(() => { row.Foreground = new SolidColorBrush(Colors.Green); }); } } PrintHelper.Print.AppendLine($"\r\n所有案例脚本执行完毕!!!!!!!!!!!!"); } ); }
/// <summary> /// 加载并显示脚本信息 /// </summary> private string LoadScript(string path) { string info = ""; if (File.Exists(path)) { SrciptDoc.FileName = path; info = CSVFileHelper.OpenScript(path).ToString(); SrciptDoc.Text = info; } return(info); }
/// <summary> /// 加载案例数据 /// </summary> /// <param name="filePath"></param> /// <param name="funList"></param> /// <returns></returns> public CaseFileData LoadCaseData(String filePath, List <CaseFunction> funList) { CaseFileData datas = new CaseFileData(); try { if (!File.Exists(filePath)) { return(datas); } datas.FilePath = @filePath; XElement rootXML = XDocument.Load(filePath).Element("bizmsgs"); var funsXml = rootXML.Elements("function"); foreach (var item in funsXml) { string functionid = item.Attribute("functionid").Value; string packettype = item.Attribute("packettype").Value; var CheckFun = funList.FirstOrDefault(o => o.Check == 1 && o.FunID == functionid); if (packettype.Equals("0") && CheckFun != null) { string functionname = item.Attribute("functionname").Value; CaseFunction fun = new CaseFunction() { FunID = functionid, FunType = 0, Check = 1, FunName = functionname }; //发送次数 if (item.Attribute("times") != null) { string sendTimes = item.Attribute("times").Value; int nSendTimes = 1; int.TryParse(sendTimes, out nSendTimes); fun.SendTimes = nSendTimes; } datas.Functions.Add(fun); var filedsXml = item.Elements("field"); LDsdkDefineEx.LDFastMessageAdapter fastmsg = new LDFastMessageAdapter(functionid, 0); LDRecordAdapter lpRecord = fastmsg.GetBizBodyRecord(); int iIndex = 0; foreach (var filedItem in filedsXml) { string fieldid = filedItem.Attribute("fieldid").Value; string fieldname = filedItem.Attribute("fieldname").Value; int filedtype = lpRecord.GetFieldType(iIndex); PARAMTYPE paramtype = PARAMTYPE.emFile; string value = ""; if (filedItem.Attribute("value") != null) { value = filedItem.Attribute("value").Value; } if (filedItem.Attribute("paramtype") != null) { string strparamtype = filedItem.Attribute("paramtype").Value; if ("file".Equals(strparamtype)) { paramtype = PARAMTYPE.emFile; } } CaseFiled filed = new CaseFiled() { FiledTag = int.Parse(fieldid), FiledName = fieldname, FiledValue = value, FiledType = filedtype, FiledIndex = iIndex, ParamType = paramtype }; var fileElemt = filedItem.Element("file"); if (fileElemt != null && paramtype == PARAMTYPE.emFile) { filed.ColumnName = fieldname; if (fileElemt.Attribute("columnname") != null) { filed.ColumnName = fileElemt.Attribute("columnname").Value; } if (fileElemt.Attribute("filename") != null) { filed.FileName = fileElemt.Attribute("filename").Value; CSVFile csvFile = null; if (File.Exists(filed.FileName) && !fun.AllCsvFile.TryGetValue(filed.FileName, out csvFile)) { var data = CSVFileHelper.OpenCSV(filed.FileName); if (data != null) { csvFile = new CSVFile(); csvFile.FileName = filed.FiledName; csvFile.Table = data; fun.AllCsvFile.Add(filed.FileName, csvFile); } } if (csvFile != null && string.IsNullOrWhiteSpace(csvFile.DefaultFieldName)) { csvFile.DefaultFieldName = fieldname; } } filed.SelectRow = VALUETYPE.emSameLine; if (fileElemt.Attribute("selectrow") != null) { try { string SelectRow = fileElemt.Attribute("selectrow").Value; if ("sequence".Equals(SelectRow)) { filed.SelectRow = VALUETYPE.emSequence; } else if (new Regex("same_line").Match(SelectRow).Success) { filed.SameLineColumn = SelectRow.Split('[')[1].Split(']')[0]; filed.SelectRow = VALUETYPE.emSameLine; } else if ("random".Equals(SelectRow)) { //默认("random".Equals(SelectRow)) filed.SelectRow = VALUETYPE.emRandom; } } catch (Exception) { //throw; } } } fun.Fileds.Add(filed); iIndex++; } fastmsg.FreeMsg(); } } return(datas); } catch (Exception error) { this.Dispatcher.Invoke(() => { MessageBox.Show($"{error.Message} \r\n {error.StackTrace}"); }); return(datas); } }
/// <summary> /// 脚本另存为 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SaveScriptTo_Click(object sender, RoutedEventArgs e) { PrintHelper.Print.PrintData = this.PrintData; string result = ""; //获取的文件保存的路径 string savePath = ""; //文件名 string fileName = ""; try { if (mv.CaseScripts.Count == 0 || mv.SelectScript.FullPath == null) { PrintHelper.Print.AppendLine("请先选中要保存的脚本!"); return; } string selectPath = mv.SelectScript.FullPath; Microsoft.Win32.SaveFileDialog saveDialog = new Microsoft.Win32.SaveFileDialog(); //定义保存文本框实体 saveDialog.Title = "保存脚本文件"; //对话框标题 saveDialog.Filter = "文件(.script)|*.script|所有文件|*.*"; //文件扩展名 saveDialog.InitialDirectory = selectPath; //默认显示当前选中的脚本名称 saveDialog.FileName = mv.SelectScript.CaseName; saveDialog.OverwritePrompt = false; if (saveDialog.ShowDialog().GetValueOrDefault()) { savePath = saveDialog.FileName; fileName = saveDialog.SafeFileName; //如果当前选择的文件路径名称与原有的不同则将源文件删除并保存当前文件(即实现修改名称) if (savePath != selectPath) { //先将文件保存 result = CSVFileHelper.SaveScript(selectPath, mv.SrciptDoc.Text); //再将文件改名 File.Move(selectPath, savePath); } else { result = CSVFileHelper.SaveScript(savePath, mv.SrciptDoc.Text); } //重新加载项目 mv.LoadScriptInfo(mv.SelectProject.FullPath); //将当前案例的选中项设置为刚刚保存的空案例 mv.SelectScript = mv.CaseScripts.FirstOrDefault(o => o.FullPath == savePath); } else { //重新加载项目 mv.LoadScriptInfo(mv.SelectProject.FullPath); //将当前案例的选中项设置为刚刚保存的空案例 mv.SelectScript = mv.CaseScripts.FirstOrDefault(o => o.FullPath == selectPath); } PrintHelper.Print.AppendLine(result); } catch (Exception error) { PrintHelper.Print.AppendLine(error.Message + "\r\n" + error.StackTrace); } }