예제 #1
0
        private void UpdateSourceWorkItem(MHTSection mhtSection, ISourceWorkItem workItem, string fieldName, StringBuilder textBuilder, List <SourceTestStep> testSteps)
        {
            if (string.IsNullOrEmpty(textBuilder.ToString()) &&
                (testSteps == null || testSteps.Count == 0))
            {
                return;
            }
            switch (mhtSection)
            {
            case MHTSection.Title:

                string title  = textBuilder.ToString();
                Regex  regEx1 = new Regex(@"\d+[a-z]*[A-Z]*[ ]*–[ ]*");
                Regex  regEx2 = new Regex(@"\d+[a-z]*[A-Z]*[ ]*-[ ]*");
                if (regEx1.IsMatch(title))
                {
                    title = title.Substring(title.IndexOf('–') + 1).Trim();
                }
                else if (regEx2.IsMatch(title))
                {
                    title = title.Substring(title.IndexOf('-') + 1).Trim();
                }
                workItem.FieldValuePairs.Add(m_storageInfo.TitleField, title);
                textBuilder.Clear();
                break;

            case MHTSection.Steps:
                if (!workItem.FieldValuePairs.ContainsKey(m_storageInfo.StepsField))
                {
                    workItem.FieldValuePairs.Add(m_storageInfo.StepsField, new List <SourceTestStep>());
                }
                var workItemSteps = workItem.FieldValuePairs[m_storageInfo.StepsField] as List <SourceTestStep>;
                foreach (var step in testSteps)
                {
                    workItemSteps.Add(step);
                }
                testSteps.Clear();
                break;

            case MHTSection.History:

                textBuilder.Append("</div>");
                if (workItem.FieldValuePairs.ContainsKey(fieldName))
                {
                    workItem.FieldValuePairs[fieldName] += textBuilder.ToString();
                }
                else
                {
                    workItem.FieldValuePairs.Add(fieldName, textBuilder.ToString());
                }
                textBuilder.Clear();
                break;

            case MHTSection.Default:
                if (workItem.FieldValuePairs.ContainsKey(fieldName))
                {
                    workItem.FieldValuePairs[fieldName] += textBuilder.ToString();
                }
                else
                {
                    workItem.FieldValuePairs.Add(fieldName, textBuilder.ToString());
                }
                textBuilder.Clear();
                break;

            default:
                break;
            }
        }
예제 #2
0
        /// <summary>
        /// parses the complete MHT source and returns the corresponding Source Workitem
        /// </summary>
        /// <returns></returns>
        public ISourceWorkItem GetNextWorkItem()
        {
            if (m_isProcessed)
            {
                return(null);
            }
            else
            {
                m_isProcessed = true;
            }
            // The Source WorkItem which has to return
            ISourceWorkItem workItem = new SourceWorkItem();

            // The Complete range of the MHT file
            Range documentRange = m_document.Range(Type.Missing, Type.Missing);

            MHTSection mhtSection = MHTSection.Skip;

            string fieldName           = string.Empty;
            bool   skipMHTSectionCheck = false;

            if (m_storageInfo.IsFirstLineTitle)
            {
                m_storageInfo.TitleField = TestTitleDefaultTag;
                skipMHTSectionCheck      = true;
                mhtSection = MHTSection.Title;
            }

            // Initializing paragraphNumber
            int paragraphNumber = 1;

            // String Builder to store the text parsed from mht
            StringBuilder textBuilder = new StringBuilder();
            var           testSteps   = new List <SourceTestStep>();

            // bool to check that whether line to parse is first line in the section or not
            bool isFirstLineInSection = true;

            // Loop to traverse the complete MHT Source
            while (paragraphNumber <= documentRange.Paragraphs.Count)
            {
                // The Current pargraph to parse
                Paragraph paragraph = documentRange.Paragraphs[paragraphNumber];

                string paragraphText = CleanString(paragraph.Range.Text);

                // If this paragraph is empty line then continue to next paragraph
                if (string.IsNullOrEmpty(CleanString(paragraph.Range.Text)))
                {
                    paragraphNumber++;
                }
                // else parse the paragraph
                else
                {
                    bool isFieldName = false;
                    foreach (SourceField field in StorageInfo.FieldNames)
                    {
                        if (String.CompareOrdinal(field.FieldName, paragraphText) == 0)
                        {
                            isFieldName = true;
                            break;
                        }
                    }
                    if (!skipMHTSectionCheck && isFieldName)
                    {
                        MHTSection previousMHTSection = mhtSection;
                        if (String.CompareOrdinal(paragraphText, m_storageInfo.TitleField) == 0)
                        {
                            mhtSection = MHTSection.Title;
                        }
                        else if (String.CompareOrdinal(paragraphText, m_storageInfo.StepsField) == 0)
                        {
                            mhtSection = MHTSection.Steps;
                        }
                        else if (m_fieldNameToFields.ContainsKey(paragraphText))
                        {
                            if (m_fieldNameToFields[paragraphText].IsHtmlField)
                            {
                                mhtSection = MHTSection.History;
                            }
                            else
                            {
                                mhtSection = MHTSection.Default;
                            }
                        }
                        else
                        {
                            mhtSection = MHTSection.Skip;
                        }

                        isFirstLineInSection = true;
                        UpdateSourceWorkItem(previousMHTSection, workItem, fieldName, textBuilder, testSteps);
                        fieldName = paragraphText;
                    }

                    switch (mhtSection)
                    {
                    case MHTSection.Title:

                        // If the current paragraph has test Title Label then skip
                        if (String.CompareOrdinal(paragraphText, m_storageInfo.TitleField) == 0)
                        {
                            paragraphNumber++;
                        }
                        // else if we have not reached the end of Test Title Section
                        else
                        {
                            textBuilder.Append(CleanString(paragraph.Range.Text));
                            if (isFirstLineInSection)
                            {
                                isFirstLineInSection = false;
                            }
                            paragraphNumber++;
                        }
                        skipMHTSectionCheck = false;

                        break;

                    case MHTSection.History:

                        if (isFirstLineInSection)
                        {
                            textBuilder.Append("<div>");
                            isFirstLineInSection = false;
                        }

                        if (paragraph.Range.Tables.Count > 0)
                        {
                            textBuilder.Append(GetHTMLTable(paragraph.Range.Tables[1]));
                            paragraphNumber = GetParagraphNumberAfterSpecifiedRange(documentRange, paragraph.Range.Tables[1].Range.End);
                        }
                        else if (paragraph.Range.InlineShapes.Count > 0)
                        {
                            string fileName = "Image" + (++m_workItemAttachmentCount) + ".bmp";
                            string filePath = Path.Combine(Path.GetTempPath(), fileName);
                            ExtractImage(paragraph.Range.InlineShapes[1].Range, filePath);
                            textBuilder.Append("<div><img src='" + filePath + "' /></div>");
                            paragraphNumber++;
                        }
                        else if (String.CompareOrdinal((paragraph.get_Style() as Style).NameLocal, "Heading 2") == 0)
                        {
                            textBuilder.Append("<h2>" + CleanString(paragraph.Range.Text).Replace("<", "&lt;").Replace(">", "&gt;") + "</h2>");
                            paragraphNumber++;
                        }
                        else
                        {
                            textBuilder.Append("<div>" + SecurityElement.Escape(CleanString(paragraph.Range.Text)) + "</div>");
                            paragraphNumber++;
                        }

                        break;

                    case MHTSection.Steps:
                        if (paragraph.Range.Tables.Count == 0)
                        {
                            paragraphNumber++;
                        }
                        else
                        {
                            Tables stepsTables = paragraph.Range.Tables;
                            foreach (Table table in stepsTables)
                            {
                                int rowNumber = 0;
                                foreach (Row currentRow in table.Rows)
                                {
                                    if (!currentRow.IsFirst)
                                    {
                                        rowNumber++;

                                        string        title            = string.Empty;
                                        string        expectedResult   = string.Empty;
                                        List <string> attachments      = new List <string>();
                                        int           attachmentCount  = 0;
                                        string        attachmentPrefix = "Step" + rowNumber;

                                        if (currentRow.Cells.Count == 1)
                                        {
                                            title = ParseTestStepCell(currentRow.Cells[1], attachments, attachmentPrefix, ref attachmentCount, 1);
                                        }
                                        else if (currentRow.Cells.Count == 3)
                                        {
                                            title          = ParseTestStepCell(currentRow.Cells[2], attachments, attachmentPrefix, ref attachmentCount, 1);
                                            expectedResult = ParseTestStepCell(currentRow.Cells[3], attachments, attachmentPrefix, ref attachmentCount, 1);
                                        }
                                        else
                                        {
                                            throw new WorkItemMigratorException("Steps table is not in correct format",
                                                                                "Steps table is not having three columns.",
                                                                                "Please make sure that you have mapped the correct field to 'Steps'.");
                                        }
                                        if (!string.IsNullOrEmpty(title) || !string.IsNullOrEmpty(expectedResult))
                                        {
                                            testSteps.Add(new SourceTestStep(title, expectedResult, attachments));
                                        }
                                    }
                                }
                            }
                            paragraphNumber = GetParagraphNumberAfterSpecifiedRange(documentRange, paragraph.Range.Tables[1].Range.End);
                        }
                        break;

                    case MHTSection.Default:
                        if (isFirstLineInSection)
                        {
                            isFirstLineInSection = false;
                        }
                        else
                        {
                            textBuilder.Append(CleanString(paragraph.Range.Text));
                        }
                        paragraphNumber++;
                        break;

                    case MHTSection.Skip:
                        paragraphNumber++;
                        break;

                    default:
                        break;
                    }
                }
            }
            UpdateSourceWorkItem(mhtSection, workItem, fieldName, textBuilder, testSteps);

            if (m_storageInfo.IsFileNameTitle)
            {
                string title = Path.GetFileNameWithoutExtension(StorageInfo.Source);
                workItem.FieldValuePairs.Add(MHTParser.TestTitleDefaultTag, title);
            }
            workItem.SourcePath = StorageInfo.Source;
            RawSourceWorkItems.Add(workItem);
            ParsedSourceWorkItems.Add(workItem);

            return(workItem);
        }