How to: Create a custom HTTP body editor for the Web Performance Test Editor

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can create a custom content editor that enables you to 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.

Note

Web performance and load test functionality is deprecated. Visual Studio 2019 is the last version where web performance and load testing will be fully available. For more information, see the Cloud-based load testing service end of life blog post. To reduce the impact on users some minimum support is still available in Visual Studio 2022 Preview 3 or later. Users should also note web performance and load test functionality only supports Internet Explorer which is not available on Windows 11 and some versions of Windows 10. Read more on Internet Explorer Lifecycle policy.

You can implement these kinds of editors:

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

Create a Windows Control Library project

  1. In Visual Studio, create a new Windows Forms Control Library project. Name the project MessageEditors.

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

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

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

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

  5. In the Properties, select the Application tab.

  6. In the Target framework drop-down list, select .NET Framework 4 (or later).

  7. The Target Framework Change dialog box is displayed.

  8. Choose Yes.

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

  10. The Add Reference dialog box is displayed.

  11. Choose the .NET tab, scroll down, and select Microsoft.VisualStudio.QualityTools.WebTestFramework and then choose OK.

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

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

  14. (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();
            }
        }
    }
    
  15. 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 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.

Create a class and implement the IStringHttpBodyEditorPlugin interface

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

    The Add New Item dialog box is displayed.

  2. Select Class.

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

  4. Choose Add.

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

  5. In the code editor, add the following using statement:

    using Microsoft.VisualStudio.TestTools.WebTesting;
    
  6. Paste in the following code to implement the interface:

    /// <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

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

  2. Close all instances of Visual Studio.

    Note

    Closing Visual Studio makes sure that the .dll file isn't locked before you try to copy it.

  3. Copy the resulting .dll file from your project's bin\debug folder (for example, MessageEditors.dll) to %ProgramFiles%\Microsoft Visual Studio\2017\<edition>\Common7\IDE\PrivateAssemblies\WebTestPlugins.

  4. Open Visual Studio.

    The .dll is now registered with Visual Studio.

Verify the plug-ins using a Web Performance Test

  1. Create a test project.

  2. Create a web performance test and enter a URL in the browser to a web service.

  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 choose the ellipsis (…).

    The Edit HTTP Body Data dialog box is displayed.

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

Compile the code

Verify that the Targeted framework for the Windows Control Library project is .NET Framework 4.5. By default, Windows Control Library projects target the .NET Framework 4.5 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