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)

public abstract function Extract(
	sender : Object, 
	e : ExtractionEventArgs


Type: System.Object
The source of the event.
Type: Microsoft.VisualStudio.TestTools.WebTesting.ExtractionEventArgs
An ExtractionEventArgs that contains the event data.

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.

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("http://localhost/ts");
            ExtractCheckBoxes rule1 = new ExtractCheckBoxes();
            rule1.FindCheckedBoxes = true;
            rule1.ContextParameterName = "CheckedBoxes";
            request1.ExtractValues += new EventHandler
            ExtractCheckBoxes rule2 = new ExtractCheckBoxes();
            rule2.FindCheckedBoxes = false;
            rule2.ContextParameterName = "";
            request1.ExtractValues += new EventHandler
            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()
            Me.PreAuthenticate = true
            Me.Proxy = "myproxy.com:80"
        End Sub
        Public Overrides Sub Run()
            Dim request1 As WebTestRequest = New WebTestRequest _
            request1.ThinkTime = 1
            Dim rule1 As ExtractHiddenFields = New ExtractHiddenFields
            rule1.ContextParameterName = "1"
            AddHandler request1.ExtractValues, AddressOf rule1.Extract

            Dim request2 As WebTestRequest = New WebTestRequest _
            request2.Method = "POST"
            Dim request2Body As FormPostHttpBody = New FormPostHttpBody
            request2Body.FormPostParameters.Add("__VIEWSTATE", "{{$HIDDEN1" + _
            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
        End Sub
    End Class
End Namespace

Community Additions