Поделиться через


Практическое руководство. Создание пользовательского правила проверки для веб-теста производительности

Пользователь может создавать собственные правила проверки.Для этого используется класс правила, производный от класса правила проверки.Правила проверки являются производными от базового класса ValidationRule:

В Visual Studio Ultimate представлено несколько предопределенных правил проверки.Дополнительные сведения см. в разделе Использование правил проверки и извлечения в веб-тестах производительности.

ПримечаниеПримечание

Кроме того, можно создавать пользовательские правила извлечения.Дополнительные сведения см. в разделе Создание и применение пользовательских подключаемых модулей для нагрузочных тестов и веб-тестов производительности.

Требования

  • Visual Studio Ultimate

Создание пользовательских правил проверки

  1. Откройте тестовый проект, содержащий веб-тест производительности.

  2. (Необязательно) Создайте отдельный проект библиотеки классов для хранения правил проверки.

    Важное примечаниеВажно

    Требуемый класс можно создать в том же проекте, в котором находятся тесты.Однако если предполагается повторно использовать правило, то лучше создать для хранения этого правила отдельный проект библиотеки классов.При создании отдельного проекта необходимо выполнить дополнительные действия, описанные в данной процедуре.

  3. (Необязательно) В проект библиотеки классов следует добавить ссылку на библиотеку DLL Microsoft.VisualStudio.QualityTools.WebTestFramework.

  4. Создайте класс, производный от класса ValidationRule.Реализуйте члены Validate и RuleName.

  5. (Необязательно) Выполните построение нового проекта библиотеки классов.

  6. (Необязательно) Добавьте в тестовый проект ссылку на проект библиотеки классов, в котором содержится пользовательское правило проверки.

  7. В тестовом проекте откройте веб-тест производительности в редакторе веб-тестов производительности.

  8. Чтобы добавить пользовательское правило проверки к запросу веб-теста производительности, щелкните правой кнопкой мыши запрос и в контекстном меню выберите команду Добавить правило проверки.

    Откроется диалоговое окно Добавление правила проверки.Пользовательское правило проверки появится в списке Выбрать правило вместе с предопределенными правилами проверки.Выберите пользовательское правило проверки, а затем выберите ОК.

  9. Выполните веб-тест производительности.

Пример

В следующем коде показана реализация пользовательского правила.Это правило проверки имитирует поведение предопределенного правила проверки "обязательный тег".Этот пример используется в качестве основы для создания пользовательских правил проверки.

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

См. также

Задачи

Практическое руководство. Добавление правила проверки к веб-тесту производительности

Пошаговое руководство. Добавление правил проверки и извлечения в веб-тест производительности

Практическое руководство. Создание пользовательского правила извлечения для веб-теста производительности

Ссылки

ValidationRule

Microsoft.VisualStudio.TestTools.WebTesting.Rules

ValidateFormField

ValidationRuleFindText

ValidationRuleRequestTime

ValidationRuleRequiredAttributeValue

ValidationRuleRequiredTag

Основные понятия

Использование правил проверки и извлечения в веб-тестах производительности