Share via


ExtractionRule.Extract Method

When overridden in a derived class, this method extracts information from a HtmlDocument and places it into the WebTestContext.

Namespace:  Microsoft.VisualStudio.TestTools.WebTesting
Assembly:  Microsoft.VisualStudio.QualityTools.WebTestFramework (in Microsoft.VisualStudio.QualityTools.WebTestFramework.dll)

Syntax

'Declaration
Public MustOverride Sub Extract ( _
    sender As Object, _
    e As ExtractionEventArgs _
)
public abstract void Extract(
    Object sender,
    ExtractionEventArgs e
)
public:
virtual void Extract(
    Object^ sender, 
    ExtractionEventArgs^ e
) abstract
abstract Extract : 
        sender:Object * 
        e:ExtractionEventArgs -> unit 
public abstract function Extract(
    sender : Object, 
    e : ExtractionEventArgs
)

Parameters

Remarks

This method contains the core extraction functionality. This method has the responsibility of extracting the data from the response.

Notes to Inheritors

When overridden in a derived class, the Extract method adds the extracted value to the WebTestContext provided by e.WebTest.Context. The name for the value added to the WebTestContext is set with the ContextParameterName property. If the extraction is a success, e.Success is set to true; otherwise it should be set to false.

Examples

The following is a Web performance test that uses the ExtractCheckBoxes custom extraction rule. The code for this can be found at ExtractionRule. The code checks for the existence of check boxes in the HtmlDocument provided by the WebTestResponse that is contained in the ExtractionEventArgs. The results are yielded to the test engine.

namespace TestProject1
{
    using System;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using ClassLibrary2;

    public class MyWebTest : WebTest
    {
        public MyWebTest()
        {
            this.PreAuthenticate = true;
        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
            WebTestRequest request1 = new WebTestRequest("https://localhost/ts");
            ExtractCheckBoxes rule1 = new ExtractCheckBoxes();
            rule1.FindCheckedBoxes = true;
            rule1.ContextParameterName = "CheckedBoxes";
            request1.ExtractValues += new EventHandler
                <ExtractionEventArgs>(rule1.Extract);
            ExtractCheckBoxes rule2 = new ExtractCheckBoxes();
            rule2.FindCheckedBoxes = false;
            rule2.ContextParameterName = "";
            request1.ExtractValues += new EventHandler
                <ExtractionEventArgs>(rule2.Extract);
            yield return request1;
        }
    }
}

The following is a coded Web performance test called MyCodedWebTest that inherits from ThreadedWebTest. This test extracts Hidden Fields. The second request posts form information contained within in three controls back to the server.

Option Strict Off
Option Explicit On

Imports Microsoft.VisualStudio.TestTools.WebTesting
Imports Microsoft.VisualStudio.TestTools.WebTesting.Rules
Imports System
Imports System.Collections.Generic

Namespace TestProject2
    
    Public Class MyCodedWebTest
        Inherits ThreadedWebTest
        
        Public Sub New()
            MyBase.New
            Me.PreAuthenticate = true
            Me.Proxy = "myproxy.com:80"
        End Sub
        
        Public Overrides Sub Run()
            Dim request1 As WebTestRequest = New WebTestRequest _
                ("https://localhost/MyWebSite")
            request1.ThinkTime = 1
            Dim rule1 As ExtractHiddenFields = New ExtractHiddenFields
            rule1.ContextParameterName = "1"
            AddHandler request1.ExtractValues, AddressOf rule1.Extract
            MyBase.Send(request1)

            Dim request2 As WebTestRequest = New WebTestRequest _
                ("https://localhost/MyWebSite/Default.aspx")
            request2.Method = "POST"
            Dim request2Body As FormPostHttpBody = New FormPostHttpBody
            request2Body.FormPostParameters.Add("__VIEWSTATE", "{{$HIDDEN1" + _
                ".__VIEWSTATE}}")
            request2Body.FormPostParameters.Add("Button1", "Button")
            request2Body.FormPostParameters.Add("TextBox1", "Hello text")
            request2.Body = request2Body
            Dim rule2 As ExtractHiddenFields = New ExtractHiddenFields
            rule2.ContextParameterName = ""
            AddHandler request2.ExtractValues, AddressOf rule2.Extract
            MyBase.Send(request2)
        End Sub
    End Class
End Namespace

.NET Framework Security

See Also

Reference

ExtractionRule Class

Microsoft.VisualStudio.TestTools.WebTesting Namespace

Other Resources

How to: Create a Custom Extraction Rule for a Web Performance Test