Condividi tramite


QueryString Correlation: Custom Extraction Rule - ResponseText

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Globalization;

using Microsoft.VisualStudio.TestTools.WebTesting;

using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

namespace WhidbeyCorrelation

{

    public class DynamicQueryStringExtraction_RawResponseText : ExtractionRule

    {

        public override string RuleName

        {

            get

            {

                return "DynamicQueryStringExtraction_RawResponseText";

            }

        }

        public override string RuleDescription

        {

            get

            {

                return "Extracts a url querystring parameter embedded in the page response.";

            }

        }

        [Description("The url you wish to extract the querystring from")]

        public string Url

        {

            get

            {

                return m_Url;

            }

            set

            {

                m_Url = value;

            }

        }

       

        [Description("The name of the querystring parameter you wish to extract")]

        public string ParameterName

        {

            get

            {

                return m_ParameterName;

            }

            set

            {

                m_ParameterName = value;

            }

        }

        [Description("The zero-based numerical index of the text occurrence you " +

            "want to extract the querystring parameter from")]

        [DefaultValue(-1)]

        public int Instance

        {

            get

            {

                return m_Instance;

            }

            set

            {

                m_Instance = value;

            }

        }

        [Description("Turning this on will automatically correlate the extracted value " +

            "in matching querystring parameters for subsequent requests")]

        [DefaultValue(true)]

        public bool AutomaticCorrelation

        {

            get

            {

                return m_AutomaticCorrelation;

            }

            set

            {

                m_AutomaticCorrelation = value;

            }

        }

       

   public bool CorrelateNextRequestOnly

        {

            get

            {

                return m_CorrelateNextRequestOnly;

            }

            set

            {

                m_CorrelateNextRequestOnly = value;

            }

        }

       

        public override void Extract(object sender, ExtractionEventArgs e)

        {

            string RequestUrl = m_Url.ToLower().Substring(m_Url.ToLower().LastIndexOf('/') + 1);

            string responseText = e.Response.BodyString;

            int pos = responseText.ToLower().IndexOf(RequestUrl);

            int curInstance = 0;

           

            while (curInstance < m_Instance)

            {

                pos = responseText.ToLower().IndexOf(RequestUrl, ++pos);

            curInstance++;

            }

           

            if (pos < 0)

            {

                e.Success = false;

                e.Message = "Could not find querystring parameter in specified text.";

                return;

            }

          List<char> endOfQuerystringValue = new List<char>(new char[] { ' ', '\r', '\n', '\'', '\"', '\t', '&' });

            pos = responseText.IndexOf(m_ParameterName + "=", pos) + m_ParameterName.Length + 1;

            int startPos = pos;

            while (!endOfQuerystringValue.Contains(responseText[pos]))

            {

                pos++;

                if (pos > responseText.Length)

                {

                    e.Success = false;

                    e.Message = "Could not find querystring parameter in specified text.";

                    return;

                }

            }

            string textToExtract = responseText.Substring(startPos, pos - startPos);

            e.WebTest.Context.Add(ContextParameterName, textToExtract);

       e.Success = true;

        }

        void WebTest_PreRequest(object sender, PreRequestEventArgs e)

        {

            foreach (QueryStringParameter param in e.Request.QueryStringParameters)

            {

                if (param.Name == m_ParameterName)

                {

                    param.Value = e.WebTest.Context[ContextParameterName].ToString();

                    break;

                }

            }

            if (m_CorrelateNextRequestOnly == true)

            {

                e.WebTest.PreRequest -= WebTest_PreRequest;

            }

        }

       

        private int m_Instance;

        private string m_Url;

        private string m_ParameterName;

        private bool m_AutomaticCorrelation;

  private bool m_CorrelateNextRequestOnly;

    }

}

Comments

  • Anonymous
    April 02, 2007
    Overview: While creating WebTests for your site one problem you may encounter is in dealing with dynamic