WebTestDetail.cs 3.83 KB
//*************************************************************************************************
// WebTestDetail.cs
// Owner: Mahipal Kante
//
// Details of WebTest
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE80;
using EnvDTE;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebTest.WebService.Plugin.ResultTab
{
    /// <summary>
    /// Details of a Declarative WebTest project Item
    /// </summary>
    public class WebTestDetail
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullPath"></param>
        /// <param name="applicationObject"></param>
        public WebTestDetail(string fullPath, DTE2 applicationObject)
        {
            this.FullPath = fullPath;
            this._applicationObject = applicationObject;
        }

        /// <summary>
        /// Web Test object
        /// </summary>
        public DeclarativeWebTest WebTest
        {
            get
            {
                if (webTest == null || (new FileInfo(FullPath).LastWriteTime > LastSavedTimeStamp))
                {
                    webTest = DeclarativeWebTestSerializer.Open(FullPath);
                    LastSavedTimeStamp = new FileInfo(FullPath).LastWriteTime;
                }
                return webTest;
            }
            set
            {
                webTest = value;
                LastSavedTimeStamp = new FileInfo(FullPath).LastWriteTime;
            }
        }

        /// <summary>
        /// Path to the WebTest file
        /// </summary>
        public string FullPath
        {
            get;
            set;
        }

        /// <summary>
        /// LastSavedTimeStamp
        /// </summary>
        public DateTime LastSavedTimeStamp
        {
            get;
            set;
        }

        /// <summary>
        /// ProjectItem of DTE
        /// </summary>
        public ProjectItem ProjectItem
        {
            get;
            set;
        }

        /// <summary>
        /// If there are pending changes, save first, as we are going to make our 
        /// changes to the file directly.
        /// </summary>
        public void PrepareToSave()
        {
            if (this.ProjectItem != null && this.ProjectItem.Document != null)
            {
                this.ProjectItem.Document.Close(vsSaveChanges.vsSaveChangesPrompt);
            }
        }

        /// <summary>
        /// Save WebTest to file
        /// </summary>
        public void Save()
        {
            DeclarativeWebTestSerializer.Save(this.WebTest, this.FullPath);
            this.LastSavedTimeStamp = DateTime.Now;

            if (this.ProjectItem != null)
            {
                Window window = this.ProjectItem.Open();
                window.Activate();
            }
        }

        /// <summary>
        /// Parse parameters from all items of Web Test
        /// </summary>
        /// <returns></returns>
        public List<string> GetParameterList()
        {
            List<string> list = new List<string>();

            DeclarativeWebTest w = WebTest;

            for (int i = 1; i <= w.Items.Count; i++)
            {
                WebTestRequest requestInWebTest = webTest.GetItem(i) as WebTestRequest;
                if (requestInWebTest != null)
                {
                    foreach (ExtractionRuleReference reference in requestInWebTest.ExtractionRuleReferences)
                    {
                        list.Add(reference.ContextParameterName);
                    }
                }
            }

            return list;
        }

        private DTE2 _applicationObject;
        private DeclarativeWebTest webTest = null;
    }
}