Compartir a través de


Cómo: Crear una regla de validación personalizada para una prueba de rendimiento web

Este tema se aplica a:

Visual Studio Ultimate

Visual Studio Premium

Visual Studio Professional 

Visual Studio Express

El tema es aplicable El tema no es aplicable El tema no es aplicable El tema no es aplicable

Puede crear sus propias reglas de validación. Para ello, derive su propia clase de regla de una clase de regla de validación. Las reglas de la validación se derivan de la clase base ValidationRule.

Visual Studio Ultimate proporciona algunas reglas de validación predefinidas. Para obtener más información, vea Usar las reglas de validación y extracción en las pruebas de rendimiento web.

Nota

También puede crear reglas de extracción personalizadas. Para obtener más información, vea Crear y usar complementos personalizados para las pruebas de carga y de rendimiento web.

Para crear reglas de validación personalizadas

  1. Abra un proyecto de prueba que contenga una prueba de rendimiento web.

  2. (Opcional) Cree un proyecto de bibliotecas de clase independiente para almacenar su regla de validación.

    Nota importanteImportante

    Puede crear la clase en el mismo proyecto donde están sus pruebas. No obstante, si desea volver a usar la regla, es mejor crear un proyecto de biblioteca de clases diferente para almacenar la regla. Si crea un proyecto independiente, debe completar los pasos opcionales de este procedimiento.

  3. (Opcional) En el proyecto de bibliotecas de clase, agregue una referencia al archivo DLL Microsoft.VisualStudio.QualityTools.WebTestFramework.

  4. Cree una clase que se derive de la clase ValidationRule. Implemente los miembros Validate y RuleName.

  5. (Opcional) Genere el nuevo proyecto de bibliotecas de clase.

  6. (Opcional) En el Proyecto de prueba, agregue una referencia al proyecto de bibliotecas de clase que contiene la regla de validación personalizada.

  7. En el Proyecto de prueba, abra una prueba de rendimiento web en el Editor de prueba de rendimiento web.

  8. Para agregar la regla de validación personalizada a una solicitud de prueba de rendimiento web, haga clic con el botón secundario en una solicitud y seleccione Agregar regla de validación.

    Aparecerá el cuadro de diálogo Agregar regla de validación. Verá su regla de validación personalizada en la lista Seleccione una regla, junto con las reglas de validación predefinidas. Seleccione su regla de validación personalizada y, a continuación, haga clic en Aceptar.

  9. Ejecute su prueba de rendimiento web.

Ejemplo

El código siguiente muestra una implementación de una regla de validación personalizada. Esta regla de validación imita el comportamiento de la regla de validación predefinida Etiqueta obligatoria. Utilice este ejemplo como punto de partida para sus propias reglas de validación personalizadas.

using System;
using System.Diagnostics;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace SampleWebTestRules
{
    //-------------------------------------------------------------------------
    // This class creates a custom validation rule named "Custom Validate Tag"
    // The custom validation rule is used to check that an HTML tag with a 
    // particular name is found one or more times in the HTML response.
    // The user of the rule can specify the HTML tag to look for, and the 
    // number of times that it must appear in the response.
    //-------------------------------------------------------------------------
    public class CustomValidateTag : ValidationRule
    {
        /// Specify a name for use in the user interface.
        /// The user sees this name in the Add Validation dialog box.
        //---------------------------------------------------------------------
        public override string RuleName
        {
            get { return "Custom Validate Tag"; }
        }

        /// Specify a description for use in the user interface.
        /// The user sees this description in the Add Validation dialog box.
        //---------------------------------------------------------------------
        public override string RuleDescription
        {
            get { return "Validates that the specified tag exists on the page."; }
        }

        // The name of the required tag
        private string RequiredTagNameValue;
        public string RequiredTagName
        {
            get { return RequiredTagNameValue; }
            set { RequiredTagNameValue = value; }
        }

        // The minimum number of times the tag must appear in the response
        private int MinOccurrencesValue;
        public int MinOccurrences
        {
            get { return MinOccurrencesValue; }
            set { MinOccurrencesValue = value; }
        }

        // Validate is called with the test case Context and the request context.
        // These allow the rule to examine both the request and the response.
        //---------------------------------------------------------------------
        public override void Validate(object sender, ValidationEventArgs e)
        {
            bool validated = false;
            int numTagsFound = 0;

            foreach (HtmlTag tag in e.Response.HtmlDocument.GetFilteredHtmlTags(RequiredTagName))
            {
                Debug.Assert(string.Equals(tag.Name, RequiredTagName, StringComparison.InvariantCultureIgnoreCase));

                if (++numTagsFound >= MinOccurrences)
                {
                    validated = true;
                    break;
                }
            }

            e.IsValid = validated;

            // If the validation fails, set the error text that the user sees
            if (!validated)
            {
                if (numTagsFound > 0)
                {
                    e.Message = String.Format("Only found {0} occurences of the tag", numTagsFound);
                }
                else
                {
                    e.Message = String.Format("Did not find any occurences of tag '{0}'", RequiredTagName);
                }
            }
        }
    }
}
Imports System
Imports System.Diagnostics
Imports System.Globalization
Imports Microsoft.VisualStudio.TestTools.WebTesting

Namespace SampleWebTestRules

    '-------------------------------------------------------------------------
    ' This class creates a custom validation rule named "Custom Validate Tag"
    ' The custom validation rule is used to check that an HTML tag with a 
    ' particular name is found one or more times in the HTML response.
    ' The user of the rule can specify the HTML tag to look for, and the 
    ' number of times that it must appear in the response.
    '-------------------------------------------------------------------------
    Public Class CustomValidateTag
        Inherits Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule

        ' Specify a name for use in the user interface.
        ' The user sees this name in the Add Validation dialog box.
        '---------------------------------------------------------------------
        Public Overrides ReadOnly Property RuleName() As String
            Get
                Return "Custom Validate Tag"
            End Get
        End Property

        ' Specify a description for use in the user interface.
        ' The user sees this description in the Add Validation dialog box.
        '---------------------------------------------------------------------
        Public Overrides ReadOnly Property RuleDescription() As String
            Get
                Return "Validates that the specified tag exists on the page."
            End Get
        End Property

        ' The name of the required tag
        Private RequiredTagNameValue As String
        Public Property RequiredTagName() As String
            Get
                Return RequiredTagNameValue
            End Get
            Set(ByVal value As String)
                RequiredTagNameValue = value
            End Set
        End Property

        ' The minimum number of times the tag must appear in the response
        Private MinOccurrencesValue As Integer
        Public Property MinOccurrences() As Integer
            Get
                Return MinOccurrencesValue
            End Get
            Set(ByVal value As Integer)
                MinOccurrencesValue = value
            End Set
        End Property

        ' Validate is called with the test case Context and the request context.
        ' These allow the rule to examine both the request and the response.
        '---------------------------------------------------------------------
        Public Overrides Sub Validate(ByVal sender As Object, ByVal e As ValidationEventArgs)

            Dim validated As Boolean = False
            Dim numTagsFound As Integer = 0

            For Each tag As HtmlTag In e.Response.HtmlDocument.GetFilteredHtmlTags(RequiredTagName)

                Debug.Assert(String.Equals(tag.Name, RequiredTagName, StringComparison.InvariantCultureIgnoreCase))

                numTagsFound += 1
                If numTagsFound >= MinOccurrences Then

                    validated = True
                    Exit For
                End If
            Next

            e.IsValid = validated

            ' If the validation fails, set the error text that the user sees
            If Not (validated) Then
                If numTagsFound > 0 Then
                    e.Message = String.Format("Only found {0} occurences of the tag", numTagsFound)
                Else
                    e.Message = String.Format("Did not find any occurences of tag '{0}'", RequiredTagName)
                End If
            End If
        End Sub
    End Class
End Namespace

Vea también

Tareas

Cómo: Agregar una regla de validación a una prueba de rendimiento web

Tutorial: Agregar reglas de validación y extracción a una prueba de rendimiento web

Cómo: Crear una regla de extracción personalizada para una prueba de rendimiento web

Referencia

ValidationRule

Microsoft.VisualStudio.TestTools.WebTesting.Rules

ValidateFormField

ValidationRuleFindText

ValidationRuleRequestTime

ValidationRuleRequiredAttributeValue

ValidationRuleRequiredTag

Conceptos

Usar las reglas de validación y extracción en las pruebas de rendimiento web