다음을 통해 공유


ExtractionRule 클래스

업데이트: 2007년 11월

웹 테스트에서 생성된 웹 응답에서 데이터를 가져오기 위한 규칙을 정의하는 데 사용되는 기본 클래스입니다.

네임스페이스:  Microsoft.VisualStudio.TestTools.WebTesting
어셈블리:  Microsoft.VisualStudio.QualityTools.WebTestFramework(Microsoft.VisualStudio.QualityTools.WebTestFramework.dll)

구문

Public MustInherit Class ExtractionRule

Dim instance As ExtractionRule
public abstract class ExtractionRule
public ref class ExtractionRule abstract
public abstract class ExtractionRule

설명

ExtractionRule 클래스는 사용자가 작성한 것이건 기본 제공 규칙이건 상관없이 모든 추출 규칙에 상속되어야 합니다. 요청 정의와 관련된 추출 규칙은 응답을 받은 후에 실행되고 추출 결과가 WebTestContext에 추가됩니다.

상속자 참고 사항:

ExtractionRule에서 상속하는 경우 Extract, RuleDescriptionRuleName 멤버를 재정의해야 합니다.

예제

다음은 HtmlDocument에서 확인란을 추출하고 발견된 확인란의 상태를 컨텍스트에 추가하는 사용자 지정 추출 규칙입니다.

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

namespace ClassLibrary2
{
    public class ExtractCheckBoxes : ExtractionRule
    {
        private string myContextParameter = "ExtractCBoxParam";
        private bool isChecked = true;

        public override string ContextParameterName
        {
            get { return myContextParameter; }
            set
            {
                if (value != string.Empty)
                    myContextParameter = value;
            }
        }

        public bool FindCheckedBoxes
        {
            get { return isChecked; }
            set { isChecked = value; }
        }

        public override string RuleName
        {
            get { return "Extract Check Boxes"; }
        }

        public override string RuleDescription
        {
            get { return "Extract check boxes based on whether they are" +
                    " checked or not."; }
        }

        public override void Extract(object sender, ExtractionEventArgs e)
        {   
            e.Success = false;
            
            if (e.Response.HtmlDocument != null)
            {   // Gets all input tags
                foreach (HtmlTag tag in e.Response.HtmlDocument
                    .GetFilteredHtmlTags(new string[] { "input" }))
                {   // Verify that current tag is a checkbox
                    if (tag.GetAttributeValueAsString("type") == "checkbox")
                    {   // Is the checkbox checked
                        if (tag.GetAttributeValueAsString("checked") == "CHECKED")
                        {   // Add checked checkboxes to context
                            if (isChecked)
                            {
                                e.WebTest.Context.Add(myContextParameter + "_" +
                                    tag.GetAttributeValueAsString("id"),
                                    "Is Checked");
                                e.Success = true;
                            }
                        }
                        else // The checkbox is not checked
                        {   // Add non-checked boxes to context
                            if (!isChecked)
                            {
                                e.WebTest.Context.Add(myContextParameter + "_" +
                                    tag.GetAttributeValueAsString("id"),
                                    "Is Not Checked");
                                e.Success = true;
                            }
                        }
                    }
                }
            }
            if (e.Success)
                e.Message = "Extracted checkboxes.";
            else
                e.Message = "No checkboxes extracted.";
        }
    }
}
Imports System
Imports Microsoft.VisualStudio.TestTools.WebTesting

Namespace ClassLibrary2
    Public Class ExtractCheckBoxes
        Inherits ExtractionRule

        Private myContextParameter As String = "ExtractCBoxParam"
        Private isChecked As Boolean = True

        Public Overrides Property ContextParameterName() As String
            Get
                Return myContextParameter
            End Get
            Set(ByVal value As String)
                If (value <> String.Empty) Then
                    myContextParameter = value
                End If
            End Set
        End Property

        Public Property FindCheckedBoxes() As Boolean
            Get
                Return isChecked
            End Get
            Set(ByVal value As Boolean)
                isChecked = value
            End Set
        End Property

        Public Overrides ReadOnly Property RuleName() As String
            Get
                Return "Extract Check Boxes"
            End Get
        End Property

        Public Overrides ReadOnly Property RuleDescription() As String
            Get
                Return "Extract check boxes based on whether they are" + _
                    " checked or not."
            End Get
        End Property

        Public Overrides Sub Extract(ByVal sender As Object, ByVal e As ExtractionEventArgs)
            e.Success = False
            If Not e.Response.HtmlDocument Is Nothing Then
                ' Gets all input tags
                Dim tag As HtmlTag
                For Each tag In e.Response.HtmlDocument.GetFilteredHtmlTags(New String() {"input"})
                    ' Verify if current tag is a checkbox
                    If tag.GetAttributeValueAsString("type") = "checkbox" Then
                        ' Is the check box checked
                        If tag.GetAttributeValueAsString("checked") = "CHECKED" Then
                            ' Add checked checkbox to context
                            If isChecked = True Then
                                e.WebTest.Context.Add(myContextParameter + "_" + _
                                tag.GetAttributeValueAsString("id"), "Is Checked")
                                e.Success = True
                            End If
                        Else ' The check box is not checked
                            If isChecked = False Then
                                ' Add non-checked boxes to context
                                e.WebTest.Context.Add(myContextParameter + "_" + _
                                tag.GetAttributeValueAsString("id"), "Is Not Checked")
                                e.Success = True
                            End If
                        End If
                    End If
                Next
            End If
            If e.Success = True Then
                e.Message = "Extracted check boxes."
            Else
                e.Message = "No check boxes extracted."
            End If
        End Sub
    End Class
End Namespace

상속 계층 구조

System.Object
  Microsoft.VisualStudio.TestTools.WebTesting.ExtractionRule
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractAttributeValue
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractFormField
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractHiddenFields
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractHttpHeader
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractRegularExpression
    Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractText

스레드로부터의 안전성

이 형식의 모든 공용 static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

참고 항목

참조

ExtractionRule 멤버

Microsoft.VisualStudio.TestTools.WebTesting 네임스페이스

기타 리소스

방법: 사용자 지정 추출 규칙 만들기

방법: 웹 테스트에 추출 규칙 추가