Pierre Greborio.NET

Talking about .NET world

September 2005 - Posts

Web Testing extraction rule

When you test a web application with Visual Studio Team System you can use extraction rules in order to exctract information from the request and reply HTTP streams. By default, the September CTP provides the following extraction rules:

  • Attribute Value
  • Form Field
  • HTTP Header
  • Regular Expression
  • Text
  • Hidden Field

Today I added my custom extraction rule: query string parameter. This expraction rule is very simple, it extract the value of a given query string parameter and eventually copy that value into the web context variable. The code is pretty simple:

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.ComponentModel;

namespace PEWay.VisualStudio.Extensibility
{
    public class QueryStringParameterExtractionRule : ExtractionRule   
    {
        private string ctxParameter = string.Empty;
        private string parameterName = string.Empty;
        private bool isCaseSensitive = false;

        [Description("Type a name for the QueryString parameter.")]
        public string ParameterName
        {
            get { return parameterName; }
            set
            {
                if (value != string.Empty)
                    parameterName = value;
            }
        }

        [Description("Ignore case during search for matching the parameter name.")]
        public bool IsCaseSensitive
        {
            get { return isCaseSensitive; }
            set { isCaseSensitive = value; }
        }

        [Description("The name of a text context variable to associate with the extracted value.")]
        public override string ContextParameterName
        {
            get { return ctxParameter; }
            set
            {
                if (value != string.Empty)
                    ctxParameter = value;
            }
        }
       
        public override string RuleName
        {
            get { return "Extract Query String parameter"; }
        }

        public override string RuleDescription
        {
            get { return "Extracts a query string parameter value."; }
        }

        public override void Extract(object sender, ExtractionEventArgs e)
        {
            StringComparison compType;
            e.Success = false;

            if (string.IsNullOrEmpty(parameterName))
            {
                e.Message = "No query string parameter name defined.";
                return;
            }

            if (isCaseSensitive == true)
                compType = StringComparison.Ordinal;
            else
                compType = StringComparison.OrdinalIgnoreCase;

            if (e.Request.HasQueryStringParameters)
            {
                foreach (QueryStringParameter parameter in e.Request.QueryStringParameters)
                {
                    if (parameter.Name.Equals(parameterName, compType))
                    {
                        e.Success = true;

                        if (!string.IsNullOrEmpty(ctxParameter))
                        {
                            // Check if the context variable exists
                            if (e.WebTest.Context[ctxParameter] == null)
                                e.WebTest.Context.Add(ctxParameter, parameter.Value);
                            else
                                e.WebTest.Context[ctxParameter] = parameter.Value;
                        }
                    }
                }
            }

            if (e.Success)
                e.Message = "Query string parameter extracted.";
            else
                e.Message = "No query string parameter extracted.";
        }
    }
}

How to use it ? Create a class library and reference it to your web test project. The rule will be included automatically in the list.

Posted: Sep 22 2005, 11:52 AM by PierreG | with 2 comment(s)
Filed under:
Busy time
It's a lot that I don't post. Why ? I'm in study phase: Indigo (WFC) and Team System. Too many things to study :-)
Posted: Sep 06 2005, 02:52 PM by PierreG | with 3 comment(s)
Filed under:
More Posts