Aracılığıyla paylaş


Web Performans Testi için Özel Doğrulama Kuralı Nasıl Oluşturulur

Kendi doğrulama kurallarınızı oluşturabilirsiniz. Bunu yapmak için, doğrulama kuralı sınıfından kendi kural sınıfınızı türetirsiniz. Doğrulama kuralları ValidationRule temel sınıfından türetilir.

Visual Studio Ultimate bazı önceden tanımlanmış doğrulama kuralları sağlar. Daha fazla bilgi için bkz. Web Başarım Testlerinde Doğrulama ve Ayıklama Kurallarını Kullanma.

Not

Özel ayıklama kuralları da oluşturabilirsiniz.Daha fazla bilgi için bkz. Yükleme ve Web Performansı Testleri için Özel Eklentiler Oluşturma ve Kullanma.

Gereksinimler

  • Visual Studio Ultimate

Özel doğrulama kuralları oluşturmak için

  1. Web performans testi içeren bir Test Projesi açın.

  2. (İsteğe bağlı) Doğrulama kuralınızı saklayacağınız ayrı bir Sınıf kitaplığı projesi oluşturun.

    Önemli

    Testlerinizin içinde olduğu aynı proje içinde bir sınıf oluşturabilirsiniz.Ancak, kuralı yeniden kullanmak istiyorsanız, kuralınızı saklayacağınız ayrı br Sınıf kitaplığı projesi oluşturmanız daha iyi olur.Ayrı bir proje oluşturursanız, bu yordamdaki isteğe bağlı adımları tamamlamanız gerekir.

  3. (İsteğe bağlı) Sınıf kitaplığı projesinde, Microsoft.VisualStudio.QualityTools.WebTestFramework dll için başvuru ekleyin.

  4. ValidationRule sınıfından türetilen sınıfı oluşturun. Validate ve RuleName üyelerini uygulayın.

  5. (İsteğe bağlı) Yeni sınıf kitaplığı projesi derleyin.

  6. (İsteğe bağlı) Test Projesinde, özel kuralı içeren Sınıf kitaplığı projesine başvuru ekleyin.

  7. Test Projesinde Web Başarım Testi Düzenleyicisi'nden bir Web başarım testi açın.

  8. Bir Web performans testi isteğine özel doğrulama kuralı eklemek için isteği sağ tıklatın ve Doğrulama Kuralı Ekle'yi seçin.

    Doğrulama Kuralı Ekle iletişim kutusu görüntülenir. Kural seç listesinde, önceden tanımlanmış doğrulama kuralları ile birlikte, özel doğrulama kuralınızı görürsünüz. Kendi özel doğrulama kuralı seçip ardından Tamam.

  9. Web performans testinizi çalıştırın.

Örnek

Aşağıdaki kod bir özel doğrulama kuralı uygulamasını gösterir. Bu doğrulama kuralı, önceden tanımlanmış Gerekli Etiket doğrulama kuralı davranışını taklit eder. Bu örneği, kendi özel doğrulama kurallarınız için bir başlangıç noktası olarak kullanın.

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

Ayrıca bkz.

Görevler

Nasıl yapılır: Web Performans Testine Doğrulama Kuralı Ekleme

İzlenecek yol: Web Performans Testine Doğrulama ve Ayıklama Kuralı Ekleme

Web Performans Testi için Özel bir Ayıklama Kuralı Nasıl Oluşturulur

Başvuru

ValidationRule

Microsoft.VisualStudio.TestTools.WebTesting.Rules

ValidateFormField

ValidationRuleFindText

ValidationRuleRequestTime

ValidationRuleRequiredAttributeValue

ValidationRuleRequiredTag

Kavramlar

Web Başarım Testlerinde Doğrulama ve Ayıklama Kurallarını Kullanma