Dela via


How to: Create a Custom HTTP Body Editor for the Web Performance Test Editor

This topic applies to:

Visual Studio Ultimate

Visual Studio Premium

Visual Studio Professional 

Visual Studio Express

Topic applies Topic does not apply Topic does not apply Topic does not apply

You can create a custom content editor that lets you edit the string body content or the binary body content of a Web service request, for example, SOAP, REST, asmx, wcf, RIA and other Web service request types.

There are two types of editors that you can implement:

These interfaces are contained in the Microsoft.VisualStudio.TestTools.WebTesting namespace.

Create a Windows Control Library project

Create a user control by using a Windows Control Library project

  1. In Visual Studio Ultimate, on the File menu, click New and then select Project.

    The New Project dialog box is displayed.

  2. Under Installed Templates, select either Visual Basic or Visual C# depending on your programming preference, and then select Windows.

    Note

    This sample uses Visual C#.

  3. In the list of templates, select Windows Forms Control Library.

  4. In the Name text box, type a name, for example, MessageEditors, and click OK.

    Note

    This sample uses MessageEditors.

    The project is added to the new solution and a UserControl named UserControl1.cs is presented in the Designer.

  5. From the Toolbox, under the Common Controls category, drag a RichTextBox onto the surface of UserControl1.

  6. Click the action tag glyph (Smart Tag Glyph) on the upper-right corner of the RichTextBox control, and then select and Dock in Parent Container.

  7. In Solution Explorer, right-click the Windows Forms Library project and select Properties.

  8. In the Properties, select the Application tab.

  9. In the Target framework drop-down list, select .NET Framework 4.

  10. The Target Framework Change dialog box is displayed.

  11. Click Yes.

  12. In Solution Explorer, right-click the References node and select Add Reference.

  13. The Add Reference dialog box is displayed.

  14. Click the .NET tab, scroll down and select Microsoft.VisualStudio.QualityTools.WebTestFramework and then click OK.

  15. If View Designer is not still open, in Solution Explorer, right-click UserControl1.cs and then select View Designer.

  16. On the design surface, right-click and select View Code.

  17. (Optional) Change the name of the class and the constructor from UserControl1 to a meaningful name, for example, MessageEditorControl:

    Note

    The sample uses MessageEditorControl.

    namespace MessageEditors
    {
        public partial class MessageEditorControl : UserControl
        {
            public MessageEditorControl()
            {
                InitializeComponent();
            }
        }
    }
    
  18. Add the following properties to enable getting and setting the text in RichTextBox1. The IStringHttpBodyEditorPlugin interface will use EditString and the IBinaryHttpBodyEditorPlugin will use EditByteArray:

            public String EditString
            {
                get
                {
                    return this.richTextBox1.Text;
                }
                set
                {
                    this.richTextBox1.Text = value;
                }
            }
    
    public byte[] EditByteArray
            {
                get
                {
                    return System.Convert.FromBase64String(richTextBox1.Text);
                }
                set
                {
                    richTextBox1.Text = System.Convert.ToBase64String(value, 0, value.Length);
                }
            }
    

Add a class for to the Windows Control Library project

Add a class to the project. It will be used to implement the IStringHttpBodyEditorPlugin and IBinaryHttpBodyEditorPlugin interfaces.

Overview of the code in this procedure

The MessageEditorControl UserControl that was created in the previous procedure is instantiated as messageEditorControl:

private MessageEditorControl messageEditorControl

The messageEditorControl instance is hosted within the plug-in dialog that is created by the CreateEditor method. Additionally, the messageEditorControl's RichTextBox is populated with the contents in the IHttpBody. However, the creation of the plug-in cannot occur unless SupportsContentType returns true. In the case of this editor, SupportsContentType returns true if the ContentType in the IHttpBody contains "xml".

When editing of the string body is completed and the user clicks OK in the plug-in dialog box, GetNewValue is called to get the edited text as a string and update the String Body in the request in the Web Test Performance Editor.

To create a class and implement the IStringHttpBodyEditorPlugin interface code

  1. In Solution Explorer, right-click the Windows Forms Control Library project and select Add New Item.

  2. The Add New Item dialog box is displayed.

  3. Select Class.

  4. In the Name text box, type a meaningful name for the class, for example, MessageEditorPlugins.

  5. Click Add.

    Class1 is added to the project and presented in the Code Editor.

  6. In the Code Editor, add the following using statement:

    using Microsoft.VisualStudio.TestTools.WebTesting;
    
  7. Write or copy the following code to instantiate the XmlMessageEditor class from IStringHttpBodyEditorPlugin interface and implement the required methods:

        /// <summary>
        /// Editor for generic text based hierarchical messages such as XML and JSON.
        /// </summary>
        public class XmlMessageEditor : IStringHttpBodyEditorPlugin
        {
            public XmlMessageEditor()
            {
            }
    
            /// <summary>
            /// Determine if this plugin supports the content type.
            /// </summary>
            /// <param name="contentType">The content type to test.</param>
            /// <returns>Returns true if the plugin supports the specified content type.</returns>
            public bool SupportsContentType(string contentType)
            {
                return contentType.ToLower().Contains("xml");
            }
    
            /// <summary>
            /// Create a UserControl to edit the specified bytes.  
            /// This control will be hosted in the
            /// plugin dialog which provides OK and Cancel buttons.
            /// </summary>
            /// <param name="contentType">The content type of the BinaryHttpBody.</param>
            /// <param name="initialValue">The bytes to edit.  The bytes are the payload of a BinaryHttpBody.</param>
            /// <returns>A UserControl capable of displaying and editing the byte array value of the specified content type.</returns>
            public object CreateEditor(string contentType, string initialValue)
            {
                messageEditorControl = new MessageEditorControl();
                messageEditorControl.EditString = initialValue;
                return this.messageEditorControl;
            }
    
            /// <summary>
            /// Gets the edited bytes after the OK button is clicked on the plugin dialog.
            /// </summary>
            public string GetNewValue()
            {
                return messageEditorControl.EditString;
            }
    
            private MessageEditorControl messageEditorControl;
        }
    

Add a IBinaryHttpBodyEditorPlugin to the Class

Implement the IBinaryHttpBodyEditorPlugin interface.

Overview of the code in this procedure

The code implementation for the IBinaryHttpBodyEditorPlugin interface is similar to the IStringHttpBodyEditorPlugin covered in the previous procedure. However, the binary version uses an array of bytes to handle the binary data instead of a string.

The MessageEditorControl UserControl created in the first procedure is instantiated as messageEditorControl:

private MessageEditorControl messageEditorControl

The messageEditorControl instance is hosted within the plug-in dialog that is created by the CreateEditor method. Additionally, the messageEditorControl's RichTextBox is populated with the contents in the IHttpBody. However, the creation of the plug-in cannot occur unless SupportsContentType returns true. In the case of this editor, SupportsContentType returns true if the ContentType in the IHttpBody contains "msbin1".

When editing of the string body is completed and the user clicks OK in the plug-in dialog box, GetNewValue is called to get the edited text as a string and update the BinaryHttpBody.Data in the request in the Web Test Performance Editor.

To add the IBinaryHttpBodyEditorPlugin to the class

  • Write or copy the following code under the XmlMessageEditor class added in the previous procedure to instantiate the Msbin1MessageEditor class from IBinaryHttpBodyEditorPlugin interface and implement the required methods:

    /// <summary>
        /// Editor for MSBin1 content type (WCF messages)
        /// </summary>
        public class Msbin1MessageEditor : IBinaryHttpBodyEditorPlugin
        {
            /// <summary>
            /// 
            /// </summary>
            public Msbin1MessageEditor()
            {
            }
    
            /// <summary>
            /// Determine if this plugin supports a content type.
            /// </summary>
            /// <param name="contentType">The content type to test.</param>
            /// <returns>Returns true if the plugin supports the specified content type.</returns>
            public bool SupportsContentType(string contentType)
            {
                return contentType.ToLower().Contains("msbin1");
            }
    
            /// <summary>
            /// Create a UserControl to edit the specified bytes.  This control will be hosted in the
            /// plugin dialog which provides OK and Cancel buttons.
            /// </summary>
            /// <param name="contentType">The content type of the BinaryHttpBody.</param>
            /// <param name="initialValue">The bytes to edit.  The bytes are the payload of a BinaryHttpBody.</param>
            /// <returns>A UserControl capable of displaying and editing the byte array value of the specified content type.</returns>
            public object CreateEditor(string contentType, byte[] initialValue)
            {
                messageEditorControl = new MessageEditorControl();
                messageEditorControl.EditByteArray = initialValue;
                return messageEditorControl;
            }
    
            /// <summary>
            /// Gets the edited bytes after the OK button is clicked on the plugin dialog.
            /// </summary>
            public byte[] GetNewValue()
            {
                return messageEditorControl.EditByteArray;
            }
    
            private MessageEditorControl messageEditorControl;
            private object originalMessage;
        }
    

Build and Deploy the Plug-Ins

To build and deploy the resulting dll for the IStringHttpBodyEditorPlugin and IBinaryHttpBodyEditorPlugin

  1. On the Build menu, click Build <Windows Form Control Library project name>.

  2. Exit Visual Studio Ultimate.

    Note

    You must exit all instances of Visual Studio Ultimate to ensure that the dll file is not locked before you try to copy it.

  3. Copy the resulting .dll file from your projects bin\debug folder (for example, MessageEditors.dll) to %ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\WebTestPlugins.

  4. Start Visual Studio Ultimate.

    The .dll should be registered with Visual Studio Ultimate.

Verify the Plug-Ins Using a Web Performance Test

To test your plug-ins

  1. Create a Test Project.

  2. Create a Web performance test and enter a URL in the browser to a Web service, for example, http://dev.virtualearth.net/webservices/v1/metadata/searchservice/dev.virtualearth.net.webservices.v1.search.wsdl.

  3. When you finish the recording, in the Web Performance Test Editor, expand the request for the Web service and select either a String Body or a Binary Body.

  4. In the Properties window, Select either String Body or Binary Body and click the ellipsis (…).

    The Edit HTTP Body Data dialog box is displayed.

  5. You can now edit the data and click OK. This invokes the applicable GetNewValue method to update the contents in the IHttpBody.

Compiling the Code

  • Verify that the Targeted framework for the Windows Control Library project is .NET Framework 4. By default, Windows Control Library projects target the .NET Framework 4 Client framework which will not allow the inclusion of the Microsoft.VisualStudio.QualityTools.WebTestFramework reference.

    For more information, see Application Page, Project Designer (C#).

See Also

Tasks

How to: Create a Request-Level Plug-In

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

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

How to: Create a Load Test Plug-In

How to: Create a Coded Web Performance Test

How to: Create a Visual Studio Add-In for the Web Performance Test Results Viewer

Reference

IStringHttpBodyEditorPlugin

CreateEditor

SupportsContentType

GetNewValue

IBinaryHttpBodyEditorPlugin

CreateEditor

SupportsContentType

GetNewValue

IHttpBody

ContentType

UserControl

RichTextBox

Other Resources

Creating and Using Custom Plug-ins for Load and Web Performance Tests