/// <summary> /// reads data from the previously saved evidence file to create environment required for session restore /// </summary> /// <param name="strFileName">The path of the saved evidence</param> public static void ResumeState(String strFileName) { strTargetDocumentName = strFileName; Range rngCellRange; Dictionary <string, int[]> dictShpDummyScreenshots = new Dictionary <string, int[]>(); List <Table> lstCorruptedTables = new List <Table>(); int[] arrShapeRangeDummy = new int[2]; Queue <string> queStrCapturedEvidences = new Queue <string>(); List <string> lstStrTablesWritten = new List <string>(); List <string> lstStrEvidencesWritten = new List <string>(); string strEvidenceImagesFolder = System.IO.Path.GetTempPath() + "EvidenceCollector\\" + EvidenceCollector.strPropTestPlanName + "\\"; wordApp = new Microsoft.Office.Interop.Word.Application(); openTargetDocument(false); string strEvidenceId, strStatus, strComments; bool bFirstTable = true; foreach (Table tbCurTable in docTargetFile.Tables) { if (bFirstTable) { bFirstTable = false; continue; } rngCellRange = tbCurTable.Cell(1, 2).Range; strEvidenceId = rngCellRange.Text; if (strEvidenceId.Contains("\x0d\x07")) { strEvidenceId = strEvidenceId.Remove(strEvidenceId.IndexOf("\x0d\x07"), 2); } if (string.IsNullOrEmpty(strEvidenceId) || string.IsNullOrWhiteSpace(strEvidenceId)) { // last table of document incompletely populated. lstCorruptedTables.Add(tbCurTable); EvidenceCollector.Log("One table corrupted, flagged for deletion"); continue; } rngCellRange = tbCurTable.Cell(1, 4).Range; strStatus = rngCellRange.Text; rngCellRange = tbCurTable.Cell(2, 2).Range; strComments = rngCellRange.Text; if (strStatus.Contains("\x0d\x07")) { strStatus = strStatus.Remove(strStatus.IndexOf("\x0d\x07"), 2); } if (strComments.Contains("\x0d\x07")) { strComments = strComments.Remove(strComments.IndexOf("\x0d\x07"), 2); } if (string.IsNullOrEmpty(strEvidenceId) || string.IsNullOrWhiteSpace(strEvidenceId)) { throw new Exception("Invalid Step Number"); } InlineShape shape = docTargetFile.Range(tbCurTable.Range.End + 1, docTargetFile.Content.End).InlineShapes[1]; arrShapeRangeDummy[0] = shape.Range.Start; arrShapeRangeDummy[1] = shape.Range.End; dictShpDummyScreenshots.Add(strEvidenceId, arrShapeRangeDummy); if (strStatus.ToLower().Equals("passed") || strStatus.ToLower().Equals("failed")) { lstStrEvidencesWritten.Add(strEvidenceId); EvidenceCollector.AddActualEvidence(strEvidenceId, EvidenceCollector.GetEvidenceOrdinalOf(strEvidenceId), strEvidenceImagesFolder + strEvidenceId + ".png", strStatus, strComments); // SaveInlineShapeToFile(shape, tempPath + strEvidenceId + ".png"); } else { if (File.Exists(strEvidenceImagesFolder + strEvidenceId + ".png")) { EvidenceCollector.AddActualEvidence(strEvidenceId, EvidenceCollector.GetEvidenceOrdinalOf(strEvidenceId), strEvidenceImagesFolder + strEvidenceId + ".png", "Passed", ""); queStrCapturedEvidences.Enqueue(strEvidenceId); } } lstStrTablesWritten.Add(strEvidenceId); } if (Directory.Exists(strEvidenceImagesFolder)) { foreach (string strPotentialEvidenceImages in Directory.GetFiles(strEvidenceImagesFolder)) { string strID = strPotentialEvidenceImages.Split('\\').LastOrDefault(); if (strID.Length >= 5) { strID = strID.Remove(strID.Length - 4, 4); } if (EvidenceCollector.lstPropEvidenceID.Contains(strID) && !queStrCapturedEvidences.Contains(strID)) { EvidenceCollector.AddActualEvidence(strID, EvidenceCollector.GetEvidenceOrdinalOf(strID), strPotentialEvidenceImages, "Passed", ""); queStrCapturedEvidences.Enqueue(strID); } } } foreach (Table tbCorruptedTable in lstCorruptedTables) { tbCorruptedTable.Delete(); EvidenceCollector.Log("One corrupted table deleted"); } EvidenceCollector.LoadSession(dictShpDummyScreenshots, queStrCapturedEvidences, lstStrTablesWritten, lstStrEvidencesWritten); docTargetFile.Save(); docTargetFile.Close(); wordApp.Quit(); }
/// <summary> /// Parse the test plan document for collecting metadata of the evidences to be captured. /// </summary> /// <param name="strFileName"></param> /// <param name="bgwReadEvidenceSender"></param> public void ParseFile(string strFileName, BackgroundWorker bgwReadEvidenceSender) { lstTestPlanSteps = new List <string[]>(); lstStrEvidenceID = new List <string>(); Cell clStepNameCell, clDescriptionCell, clExpectedResultsCell, clEvidenceRequiredCell; Range rngEvidenceIDRange, rngDescriptionRange, rngExpectedResultsRange, rngDecisionStringRange; Table tbTestPlanTable; bool bIsCandidateEvidence; string strEvidenceID, strDescription, strExpectedResults, strDecisionString; appTestPlanReaderWordApp.Visible = false; if (bgwReadEvidenceSender.CancellationPending) { closeTestPlanReaderWordApp(); return; } EvidenceCollector.Log("TestPlanDocument open time: " + DateTime.UtcNow.Hour + ":" + DateTime.UtcNow.Minute + ":" + DateTime.UtcNow.Second + ":" + DateTime.UtcNow.Millisecond); docTestPlanDocument = appTestPlanReaderWordApp.Documents.Open(strFileName, ReadOnly: true); strTestPlanName = docTestPlanDocument.Sentences[1].Text.Replace('\n', ' ').Replace('\r', ' ').Replace('\v', ' ').Replace('\t', ' ').Trim(' '); tbTestPlanTable = docTestPlanDocument.Tables[1]; Match m = Regex.Match(docTestPlanDocument.Content.Text, "[Pp]rerequisites.*[Cc]hange [Cc]ontrol"); strPrerequisites = m.Value; strPrerequisites = strPrerequisites.Remove(strPrerequisites.Length - 14, 14); strPrerequisites = strPrerequisites.Trim(' ', '\r', '\n', '\t'); foreach (Row rwTestPlanRow in tbTestPlanTable.Rows) { if (bgwReadEvidenceSender.CancellationPending) { closeTestPlanDocument(); closeTestPlanReaderWordApp(); return; } /// Ignore the header row of the table. if (rwTestPlanRow.Cells[2].Range.Text.ToLower().Contains("description")) { continue; } clStepNameCell = rwTestPlanRow.Cells[1]; clDescriptionCell = rwTestPlanRow.Cells[2]; clExpectedResultsCell = rwTestPlanRow.Cells[3]; clEvidenceRequiredCell = rwTestPlanRow.Cells[rwTestPlanRow.Cells.Count]; rngEvidenceIDRange = clStepNameCell.Range; strEvidenceID = rngEvidenceIDRange.Text; if (rngEvidenceIDRange.ListParagraphs.Count > 0) { strEvidenceID = clStepNameCell.Range.ListFormat.ListString; } else { strEvidenceID = clStepNameCell.Range.Text; } rngDescriptionRange = clDescriptionCell.Range; rngExpectedResultsRange = clExpectedResultsCell.Range; rngDecisionStringRange = clEvidenceRequiredCell.Range; strDescription = rngDescriptionRange.Text; strExpectedResults = rngExpectedResultsRange.Text; strDecisionString = rngDecisionStringRange.Text; // trim the cell marker string "\x0d\x07" if (strEvidenceID.Contains("\x0d\x07")) { strEvidenceID = strEvidenceID.Remove(strEvidenceID.IndexOf("\x0d\x07"), 2); } if (strDescription.Contains("\x0d\x07")) { strDescription = strDescription.Remove(strDescription.IndexOf("\x0d\x07"), 2); } if (strExpectedResults.Contains("\x0d\x07")) { strExpectedResults = strExpectedResults.Remove(strExpectedResults.IndexOf("\x0d\x07"), 2); } if (strDecisionString.Contains("\x0d\x07")) { strDecisionString = strDecisionString.Remove(strDecisionString.IndexOf("\x0d\x07"), 2); } if (string.IsNullOrEmpty(strEvidenceID) || string.IsNullOrWhiteSpace(strEvidenceID)) { throw new Exception("Invalid Step Number"); } arrStepData = new string[3]; arrStepData[0] = strEvidenceID; arrStepData[1] = strDescription; arrStepData[2] = strExpectedResults; lstTestPlanSteps.Add(arrStepData); bIsCandidateEvidence = false; /* * Any non whitespace printable character in the last column of the test plan table is an indicator that screenshot is to be taken for that step. * So, checking for an ASCII character within range [22,126] */ if (string.Compare(strDecisionString.ToLower(), "none") != 0) { foreach (char ch in strDecisionString) { if ((int)ch >= 33 && (int)ch <= 126) { bIsCandidateEvidence = true; break; } } } if (bIsCandidateEvidence) { lstStrEvidenceID.Add(strEvidenceID); } } closeTestPlanDocument(); closeTestPlanReaderWordApp(); }