/// <summary> /// 写XML /// </summary> public void WriteXml() { string filePath = $"{System.Environment.CurrentDirectory}\\TestCase_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xml"; FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate); using (StreamWriter sw = new StreamWriter(fs)) { string tsStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; tsStr += "<testsuite name = \"测试用例集\">"; tsStr += "<node_order><![CDATA[]]></node_order>"; tsStr += "<details><![CDATA[]]></details>"; sw.Write(tsStr); foreach (KeyValuePair <string, List <string> > casePair in this.CaseToStr()) { sw.Write($"<testsuite name = \"{casePair.Key}\">"); sw.Write("<node_order><![CDATA[]]></node_order>"); sw.Write("<details><![CDATA[]]></details>"); foreach (string str in casePair.Value) { sw.Write(str); } sw.Write("</testsuite>"); } sw.Write("</testsuite>"); sw.Close(); } OutputDisplay.ShowMessage(string.Format("文件保存路勁:{0}\n", filePath), Color.Azure); }
/// <summary> /// 写Excel /// </summary> public void WriteExcel() { string currentDir = System.Environment.CurrentDirectory; string fileName = $"{currentDir}\\TestCaseTemplate.xlsx"; Excel.Application excelApp = new Excel.ApplicationClass(); Excel.Workbook workbook; if (System.IO.File.Exists(fileName)) { workbook = excelApp.Workbooks.Open(fileName, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); } else { workbook = excelApp.Workbooks.Add(true); } this.WriteInWorkSheet(workbook); excelApp.Visible = false; excelApp.DisplayAlerts = false; string saveDir = fileName.Replace("TestCaseTemplate.xlsx", $"TestCase_{DateTime.Now:yyyyMMddHHmmss}.xlsx"); OutputDisplay.ShowMessage($"文件保存路勁:{saveDir}\n", Color.Azure); workbook.SaveAs(saveDir); workbook.Close(false, Missing.Value, Missing.Value); excelApp.Quit(); workbook = null; excelApp = null; }
public Dictionary <string, List <TestCase> > ReadExcel() { Dictionary <string, List <TestCase> > dicAllTestCases = new Dictionary <string, List <TestCase> >(); int iCount = this.excelPackage.Workbook.Worksheets.Count; if (iCount == 0) { OutputDisplay.ShowMessage("表中无Sheet页!", Color.Red); return(null); } for (int iFlag = 1; iFlag <= iCount; iFlag++) { ExcelWorksheet excelWorksheet = this.excelPackage.Workbook.Worksheets[iFlag]; var TestCase = this.GetExcelSheetData(excelWorksheet); if (dicAllTestCases.ContainsKey(excelWorksheet.Name)) { OutputDisplay.ShowMessage($"同一页签名:{excelWorksheet.Name}已在本Excel中出现过.", Color.GreenYellow); } if (TestCase.Count == 0) { OutputDisplay.ShowMessage($"页签:{excelWorksheet.Name}无任何可转换用例数据.", Color.GreenYellow); continue; } dicAllTestCases.Add(excelWorksheet.Name, TestCase); } this.excelPackage.Dispose(); return(dicAllTestCases); }
/// <summary> /// 测试用例转为Str /// </summary> /// <returns>List String</returns> private Dictionary <string, List <string> > CaseToStr() { Dictionary <string, List <string> > dicCase = new Dictionary <string, List <string> >(); foreach (KeyValuePair <string, List <TestCase> > keyValuePair in _tcList) { OutputDisplay.ShowMessage($"【{keyValuePair.Key}】数据读取......", Color.MediumVioletRed); Dictionary <string, List <string> > suiteTc = new Dictionary <string, List <string> >(); List <string> tcStrList = new List <string>(); foreach (TestCase testCase in keyValuePair.Value) { OutputDisplay.ShowMessage(testCase.Name, Color.Chartreuse); ProgressBarShow.ShowProgressValue(keyValuePair.Value.IndexOf(testCase) * 100 / keyValuePair.Value.Count); string fieldsStr = $"<node_order><![CDATA[{testCase.NodeOrder}]]></node_order>"; fieldsStr += $"<externalid><![CDATA[{testCase.ExternalId}]]></externalid>"; fieldsStr += $"<version><![CDATA[{testCase.Version}]]></version>"; fieldsStr += $"<summary><![CDATA[{testCase.Summary}]]></summary>"; fieldsStr += $"<preconditions><![CDATA[{testCase.Preconditions}]]></preconditions>"; fieldsStr += $"<execution_type><![CDATA[{testCase.ExecutionType.ToString()}]]></execution_type>"; fieldsStr += $"<importance><![CDATA[{testCase.Importance.ToString()}]]></importance>"; fieldsStr += $"<estimated_exec_duration>{testCase.EstimatedExecDuration}</estimated_exec_duration>"; fieldsStr += $"<status>{testCase.Status}</status>"; string tsStr = ""; foreach (TestStep testStep in testCase.TestSteps) { tsStr += "<step>"; tsStr += $"<step_number><![CDATA[{testStep.StepNumber}]]></step_number>"; tsStr += $"<actions><![CDATA[{testStep.Actions}]]></actions>"; tsStr += $"<expectedresults><![CDATA[{testStep.ExpectedResults}]]></expectedresults>"; tsStr += $"<execution_type><![CDATA[{testStep.ExecutionType}]]></execution_type>"; tsStr += "</step>"; } fieldsStr += $"<steps>{tsStr}</steps>"; String keywordStr = $"<keywords>"; foreach (string keyword in testCase.Keywords) { keywordStr += $"<keyword name=\"{keyword}\"><notes><![CDATA[]]></notes></keyword>"; } fieldsStr += keywordStr + "</keywords>"; string tcStr = $"<testcase name=\"{testCase.Name}\">{fieldsStr}</testcase>"; Thread.Sleep(50); tcStrList.Add(tcStr); string suitenames = string.Empty; testCase.TestCaseHierarchy.ForEach(item => suitenames = suitenames + "|" + item); if (!suiteTc.Keys.Contains(suitenames.TrimStart('|'))) { suiteTc.Add(suitenames.TrimStart('|'), new List <string>()); } suiteTc[suitenames.TrimStart('|')].Add(tcStr); } ProgressBarShow.ShowProgressValue(100); dicCase.Add(keyValuePair.Key, tcStrList); } return(dicCase); }
/// <summary> /// 写Excel /// </summary> public void WriteExcel(string fileName) { using (var p = new ExcelPackage()) { this.WriteInWorkSheet(p.Workbook); p.SaveAs(new FileInfo(fileName)); } OutputDisplay.ShowMessage($"文件保存路勁:{fileName}\n", Color.Azure); }
public void WriteXml2(string sTcs, string fileDir) { FileStream fs = new FileStream(fileDir, FileMode.OpenOrCreate); using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(sTcs); sw.Close(); } OutputDisplay.ShowMessage(string.Format("文件保存路勁:{0}\n", fileDir), Color.Azure); }
/// <summary> /// 写Excel /// </summary> public void WriteExcel() { string currentDir = Environment.CurrentDirectory; string saveDir = $"{currentDir}\\TestCase_{DateTime.Now:yyyyMMddHHmmss}.xlsx"; using (var p = new ExcelPackage()) { this.WriteInWorkSheet(p.Workbook); p.SaveAs(new FileInfo(saveDir)); } OutputDisplay.ShowMessage($"文件保存路勁:{saveDir}\n", Color.Azure); }
public void WriteXml2(string sTcs) { string filePath = $"{System.Environment.CurrentDirectory}\\TestCase_{DateTime.Now.ToString("yyyyMMddHHmmss")}_tree.xml"; FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate); using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(sTcs); sw.Close(); } OutputDisplay.ShowMessage(string.Format("文件保存路勁:{0}\n", filePath), Color.Azure); }
private void WriteInWorkSheet(Excel.Workbook workBook) { var workSheet = (Excel.Worksheet)workBook.Worksheets.Item[1]; int iFlag = 2; foreach (TestCase node in this._sourceTestCases) { if (node.Name == null && node.TestSteps == null) { continue; } OutputDisplay.ShowMessage(node.Name, Color.Chartreuse); ProgressBarShow.ShowProgressValue(this._sourceTestCases.IndexOf(node) * 100 / this._sourceTestCases.Count); workSheet.Cells[iFlag, 1] = node.ExternalId; workSheet.Cells[iFlag, 2] = CommonHelper.DelTags(node.Name); string keywords = string.Empty; if (node.Keywords != null) { foreach (string keyword in node.Keywords) { keywords = keywords + keyword + ","; } } workSheet.Cells[iFlag, 3] = keywords.TrimEnd(','); workSheet.Cells[iFlag, 4] = node.Importance.ToString(); workSheet.Cells[iFlag, 5] = node.ExecutionType.ToString(); workSheet.Cells[iFlag, 6] = CommonHelper.DelTags(node.Summary); workSheet.Cells[iFlag, 7] = CommonHelper.DelTags(node.Preconditions); int iMerge = 0; if (node.TestSteps != null && node.TestSteps.Count != 0) { foreach (var step in node.TestSteps) { workSheet.Cells[iFlag, 8] = CommonHelper.DelTags(step.Actions); workSheet.Cells[iFlag, 9] = CommonHelper.DelTags(step.ExpectedResults); iFlag++; iMerge++; } } else { iFlag++; iMerge++; } this.MergeCells(workSheet, iMerge, iFlag - iMerge); Thread.Sleep(50); } workSheet.Cells[iFlag++, 1] = "END"; ProgressBarShow.ShowProgressValue(100); }
/// <summary> /// 读取Excel数据 /// </summary> /// <returns>TestCase</returns> public Dictionary <string, List <TestCase> > ReadExcel() { object missing = Missing.Value; Application excel = new Application { Visible = false, UserControl = true }; // 以只读的形式打开EXCEL文件 Workbook wb = excel.Application.Workbooks.Open(this._eFilePath, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing); //取得第一个工作薄 Dictionary <string, List <TestCase> > dic = new Dictionary <string, List <TestCase> >(); foreach (Worksheet sheet in wb.Worksheets) { string sheetName = sheet.Name; List <TestCase> tcs = this.GetExcelData(sheet); if (dic.ContainsKey(sheetName)) { OutputDisplay.ShowMessage($"同一页签名:{sheetName}已在本Excel中出现过.", Color.GreenYellow); } if (tcs.Count == 0) { OutputDisplay.ShowMessage($"页签:{sheetName} 无任何可转换用例数据.", Color.GreenYellow); continue; } dic.Add(sheetName, tcs); } // Worksheet ws = (Worksheet) wb.Worksheets.Item[1]; // tcList = this.GetExcelData(ws); excel.Quit(); excel = null; CommonHelper.KillExcelProcess(); return(dic); }
public Dictionary <string, List <TestCase> > ReadExcel() { Dictionary <string, List <TestCase> > dicAllTestCases = new Dictionary <string, List <TestCase> >(); int iCount = this._excelPackage.Workbook.Worksheets.Count; for (int iFlag = 1; iFlag <= iCount; iFlag++) { ExcelWorksheet excelWorksheet = this._excelPackage.Workbook.Worksheets[iFlag]; var testCase = this.GetExcelSheetData(excelWorksheet); if (testCase.Count == 0) { OutputDisplay.ShowMessage($"页签:{excelWorksheet.Name}无任何可转换用例数据.", Color.GreenYellow); continue; } dicAllTestCases.Add(excelWorksheet.Name, testCase); } this._excelPackage.Dispose(); return(dicAllTestCases); }
private string TestCaseToStr(TestCase tc) { Thread.Sleep(20); _allCases.Remove(tc); OutputDisplay.ShowMessage(tc.Name, Color.Chartreuse); ProgressBarShow.ShowProgressValue((_tcCount - _allCases.Count) * 100 / _tcCount); string fieldsStr = $"<node_order><![CDATA[{tc.NodeOrder}]]></node_order>"; fieldsStr += $"<externalid><![CDATA[{tc.ExternalId}]]></externalid>"; fieldsStr += $"<version><![CDATA[{tc.Version}]]></version>"; fieldsStr += $"<summary><![CDATA[{tc.Summary}]]></summary>"; fieldsStr += $"<preconditions><![CDATA[{tc.Preconditions}]]></preconditions>"; fieldsStr += $"<execution_type><![CDATA[{tc.ExecutionType.ToString()}]]></execution_type>"; fieldsStr += $"<importance><![CDATA[{tc.Importance.ToString()}]]></importance>"; fieldsStr += $"<estimated_exec_duration>{tc.EstimatedExecDuration}</estimated_exec_duration>"; fieldsStr += $"<status>{tc.Status}</status>"; string tsStr = ""; foreach (TestStep testStep in tc.TestSteps) { tsStr += "<step>"; tsStr += $"<step_number><![CDATA[{testStep.StepNumber}]]></step_number>"; tsStr += $"<actions><![CDATA[{testStep.Actions}]]></actions>"; tsStr += $"<expectedresults><![CDATA[{testStep.ExpectedResults}]]></expectedresults>"; tsStr += $"<execution_type><![CDATA[{testStep.ExecutionType}]]></execution_type>"; tsStr += "</step>"; } fieldsStr += $"<steps>{tsStr}</steps>"; String keywordStr = $"<keywords>"; foreach (string keyword in tc.Keywords) { keywordStr += $"<keyword name=\"{keyword}\"><notes><![CDATA[]]></notes></keyword>"; } fieldsStr += keywordStr + "</keywords>"; string tcStr = $"<testcase name=\"{tc.Name}\">{fieldsStr}</testcase>"; return(tcStr); }
public ExcelAnalysisByEpplus(string excelFilePath) { if (string.IsNullOrEmpty(excelFilePath)) { OutputDisplay.ShowMessage("传入文件地址有误!", Color.Red); return; } if (!File.Exists(excelFilePath)) { OutputDisplay.ShowMessage("文件不存在!", Color.Red); return; } try { FileInfo fiExcel = new System.IO.FileInfo(excelFilePath); this._excelPackage = new ExcelPackage(fiExcel); }catch (Exception ex) { OutputDisplay.ShowMessage(ex.Message, Color.Red); return; } }
private void WriteInWorkSheet(ExcelWorkbook workBook) { var workSheet = workBook.Worksheets.Add("MySheet"); int maxHierarchy = 0; foreach (TestCase testCase in this._sourceTestCases) { if (maxHierarchy < testCase.TestCaseHierarchy.Count) { maxHierarchy = testCase.TestCaseHierarchy.Count; } } BuildTemplate(workSheet, maxHierarchy); int iFlag = 2; foreach (TestCase node in this._sourceTestCases) { if (node.Name == null && node.TestSteps == null) { continue; } OutputDisplay.ShowMessage(node.Name, Color.Chartreuse); ProgressBarShow.ShowProgressValue(this._sourceTestCases.IndexOf(node) * 100 / this._sourceTestCases.Count); workSheet.Cells[iFlag, 1].Value = node.ExternalId; for (int i = 0; i < node.TestCaseHierarchy.Count; i++) { workSheet.Cells[iFlag, i + 2].Value = node.TestCaseHierarchy.ToArray()[i]; } workSheet.Cells[iFlag, maxHierarchy + 2].Value = CommonHelper.DelTags(node.Name); string keywords = string.Empty; if (node.Keywords != null) { foreach (string keyword in node.Keywords) { keywords = keywords + keyword + ","; } } workSheet.Cells[iFlag, maxHierarchy + 3].Value = keywords.TrimEnd(','); workSheet.Cells[iFlag, maxHierarchy + 4].Value = node.Importance.ToString(); workSheet.Cells[iFlag, maxHierarchy + 5].Value = node.ExecutionType.ToString(); workSheet.Cells[iFlag, maxHierarchy + 6].Value = CommonHelper.DelTags(node.Summary); workSheet.Cells[iFlag, maxHierarchy + 7].Value = CommonHelper.DelTags(node.Preconditions); int iMerge = 0; if (node.TestSteps != null && node.TestSteps.Count != 0) { foreach (var step in node.TestSteps) { workSheet.Cells[iFlag, maxHierarchy + 8].Value = CommonHelper.DelTags(step.Actions); workSheet.Cells[iFlag, maxHierarchy + 9].Value = CommonHelper.DelTags(step.ExpectedResults); iFlag++; iMerge++; } } else { iFlag++; iMerge++; } this.MergeCells(workSheet, iMerge, iFlag - iMerge, maxHierarchy + 9); Thread.Sleep(50); } workSheet.Cells[iFlag++, 1].Value = "END"; ProgressBarShow.ShowProgressValue(100); }
/// <summary> /// Node转为Model /// </summary> /// <param name="node">XML节点Node</param> /// <returns>TestCase Model</returns> private TestCase NodeToModel(XmlNode node) { TestCase tc = new TestCase(); try { if (node.Attributes != null && node.Attributes.Count == 0) { return(tc); } if (node.Attributes != null && node.Attributes.Count != 1) { tc.InternalId = node.Attributes["internalid"].Value; } if (node.Attributes != null) { tc.Name = node.Attributes["name"].Value; } } catch (NullReferenceException ex) { this._logger.Error("用例名称为空", ex); OutputDisplay.ShowMessage("用例名称为空", Color.Red); } foreach (XmlNode xmlNode in node) { switch (xmlNode.Name) { case "node_order": tc.NodeOrder = xmlNode.InnerText; break; case "externalid": tc.ExternalId = xmlNode.InnerText; break; case "version": tc.Version = xmlNode.InnerText; break; case "summary": tc.Summary = xmlNode.InnerText; break; case "preconditions": tc.Preconditions = xmlNode.InnerText; break; case "execution_type": tc.ExecutionType = CommonHelper.StrToExecType(xmlNode.InnerText); break; case "importance": tc.Importance = CommonHelper.StrToImportanceType(xmlNode.InnerText); break; case "estimated_exec_duration": if (xmlNode.InnerText.Equals("")) { tc.EstimatedExecDuration = 0.0; break; } tc.EstimatedExecDuration = double.Parse(xmlNode.InnerText); break; case "status": tc.Status = (StatusType)int.Parse(xmlNode.InnerText); break; case "steps": tc.TestSteps = GetAllSteps(xmlNode); break; case "keywords": tc.Keywords = new List <string>(); foreach (XmlNode childNode in xmlNode.ChildNodes) { if (childNode.Attributes != null) { tc.Keywords.Add(childNode.Attributes["name"].Value); } } break; case "requirement": tc.Requirement = xmlNode.InnerText; break; default: break; } } tc.TestCaseHierarchy = GetTestCaseHierarchy(node); OutputDisplay.ShowMessage(tc.Name, Color.Chartreuse); Thread.Sleep(100); return(tc); }
public List <TestCase> GetExcelSheetData(ExcelWorksheet eWorksheet) { List <TestCase> tcList = new List <TestCase>(); int usedRows, usedCols; if (eWorksheet.Dimension == null) { this._logger.Warn(new Exception("No TestCase, this Sheet is new!")); return(new List <TestCase>()); } else { usedRows = eWorksheet.Dimension.End.Row; usedCols = eWorksheet.Dimension.End.Column; } if (usedRows == 0 || usedRows == 1) { this._logger.Warn(new Exception("No TestCase!")); return(tcList); } for (int i = 1; i < eWorksheet.Dimension.End.Row; i++) { if (eWorksheet.Cells[i, 1].Value != null || eWorksheet.Cells[i, 1].Text != string.Empty || !eWorksheet.Cells[i, 1].Text.Equals("END")) { continue; } usedRows = i; break; } TestCase tc = new TestCase(); for (int i = 2; i <= usedRows; i++) { ProgressBarShow.ShowProgressValue(i * 100 / usedRows); var currentCell = eWorksheet.Cells[i, 1]; if (currentCell.Text.Equals("END")) { continue; } //设置单元格格式为文本格式,防止为自定义格式时读取单元格报错 for (int j = 2; j <= usedCols; j++) { eWorksheet.Cells[i, j].Style.Numberformat.Format = "@"; } if (currentCell.Value == null) { TestStep ts = new TestStep { StepNumber = tc.TestSteps.Count + 1, ExecutionType = ExecType.手动, Actions = eWorksheet.Cells[i, usedCols - 1].Text, ExpectedResults = eWorksheet.Cells[i, usedCols].Text }; tc.TestSteps.Add(ts); continue; } else { List <string> testSuitesName = new List <string>(); if (usedCols > 9) { for (int j = 1; j <= usedCols - 9; j++) { if (eWorksheet.Cells[i, j + 1].Value != null) { testSuitesName.Add(eWorksheet.Cells[i, j + 1].Text); } } } tc = new TestCase { ExternalId = string.Format($"{currentCell.Text}_{new Random().Next(0, 10000)}"), TestCaseHierarchy = testSuitesName, Name = eWorksheet.Cells[i, usedCols - 7].Text, Keywords = eWorksheet.Cells[i, usedCols - 6].Text.Split(',').ToList(), Importance = CommonHelper.StrToImportanceType(eWorksheet.Cells[i, usedCols - 5].Text), ExecutionType = CommonHelper.StrToExecType(eWorksheet.Cells[i, usedCols - 4].Text), Summary = eWorksheet.Cells[i, usedCols - 3].Text, Preconditions = eWorksheet.Cells[i, usedCols - 2].Text }; TestStep tsOne = new TestStep { StepNumber = 1, ExecutionType = ExecType.手动, Actions = eWorksheet.Cells[i, usedCols - 1].Text.ToString(), ExpectedResults = eWorksheet.Cells[i, usedCols].Text.ToString() }; tc.TestSteps = new List <TestStep> { tsOne }; OutputDisplay.ShowMessage(tc.Name, Color.Chartreuse); if (tc.ExternalId != null) { tcList.Add(tc); } } } return(tcList); }