如何:为 Web 性能测试编辑器创建自定义 HTTP 正文编辑器

本主题适用于:

Visual Studio 旗舰版

Visual Studio 高级专业版

Visual Studio 专业版 

Visual Studio 学习版

主题适用 主题不适用 主题不适用 主题不适用

您可以创建自定义内容编辑器,通过它来编辑 Web 服务请求的字符串主体内容或二进制主体内容,例如 SOAP、REST、asmx、wcf、RIA 和其他 Web 服务请求类型。

可以实现两种编辑器类型:

这些接口包含在 Microsoft.VisualStudio.TestTools.WebTesting 命名空间中。

创建 Windows 控件库项目

使用 Windows 控件库项目创建用户控件

  1. 在 Visual Studio 旗舰版中的**“文件”菜单上,单击“新建”,然后选择“项目”**。

    将显示**“新建项目”**对话框。

  2. 在**“已安装的模板”下,根据您的编程喜好选择“Visual Basic”“Visual C#”,然后选择“Windows”**。

    提示

    此示例使用 Visual C#。

  3. 在模板列表中,选择**“Windows 窗体控件库”**。

  4. 在“名称”文本框中键入名称,例如 MessageEditors,然后单击**“确定”**。

    提示

    此示例使用 MessageEditors。

    项目将添加到新解决方案中,设计器中将显示一个名为 UserControl1.cs 的 UserControl

  5. 从**“工具箱”“公共控件”**类别下,将 RichTextBox 拖动到 UserControl1 的图面上。

  6. 单击 RichTextBox 控件右上角的操作标记标志符号 (智能标记标志符号),然后选择**“在父容器中停靠”**。

  7. 在解决方案资源管理器中,右击 Windows 窗体库项目,然后选择**“属性”**。

  8. 在“属性”中,选择**“应用程序”**选项卡。

  9. 在**“目标框架”下拉列表中,选择“.NET Framework 4”**。

  10. 将显示“目标 Framework 更改”对话框。

  11. 单击**“是”**。

  12. 在解决方案资源管理器中,右击**“引用”节点并选择“添加引用”**。

  13. 将显示**“添加引用”**对话框。

  14. 单击**“.NET”选项卡,向下滚动,选择“Microsoft.VisualStudio.QualityTools.WebTestFramework”,然后单击“确定”**。

  15. 如果视图设计器仍未打开,请在解决方案资源管理器中右击**“UserControl1.cs”,然后选择“视图设计器”**。

  16. 在设计图面上,右击并选择**“查看代码”**。

  17. (可选)将类和构造函数的名称从 UserControl1 更改为有意义的名称,例如 MessageEditorControl:

    提示

    此示例使用 MessageEditorControl。

    namespace MessageEditors
    {
        public partial class MessageEditorControl : UserControl
        {
            public MessageEditorControl()
            {
                InitializeComponent();
            }
        }
    }
    
  18. 添加以下属性,以允许在 RichTextBox1 中获取和设置文本。 IStringHttpBodyEditorPlugin 接口将使用 EditString,IBinaryHttpBodyEditorPlugin 将使用 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);
                }
            }
    

向 Windows 控件库项目中添加类

向项目中添加类。 该类将用于实现 IStringHttpBodyEditorPluginIBinaryHttpBodyEditorPlugin 接口。

此过程中的代码概述

在前面过程中创建的 MessageEditorControl UserControl 将实例化为 messageEditorControl:

private MessageEditorControl messageEditorControl

messageEditorControl 实例承载在 CreateEditor 方法创建的插件对话框中。 此外,messageEditorControl 的 RichTextBox 将由 IHttpBody 中的内容进行填充。 但是,除非 SupportsContentType 返回 true,否则无法创建插件。 对于此编辑器,如果 IHttpBody 中的 ContentType 包含“xml”,则 SupportsContentType 返回 true。

当完成字符串主体的编辑并且用户在插件对话框中单击**“确定”时,将调用 GetNewValue 以获取字符串形式的已编辑文本,并在 Web 测试性能编辑器的请求中更新“字符串主体”**。

创建类并实现 IStringHttpBodyEditorPlugin 接口代码

  1. 在解决方案资源管理器中,右击 Windows 窗体控件库项目并选择**“添加新项”**。

  2. 显示**“添加新项”**对话框。

  3. 选择**“类”**。

  4. 在**“名称”**文本框中,为类键入有意义的名称,例如 MessageEditorPlugins。

  5. 单击**“添加”**。

    Class1 将添加到该项目中,并在代码编辑器中显示。

  6. 在代码编辑器中,添加以下 using 语句:

    using Microsoft.VisualStudio.TestTools.WebTesting;
    
  7. 编写或复制以下代码以从 IStringHttpBodyEditorPlugin 接口实例化 XmlMessageEditor 类并实现所需方法:

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

向类中添加 IBinaryHttpBodyEditorPlugin

实现 IBinaryHttpBodyEditorPlugin 接口。

此过程中的代码概述

IBinaryHttpBodyEditorPlugin 接口的代码实现与前面过程中介绍的 IStringHttpBodyEditorPlugin 类似。 但是,二进制版本使用字节数组来处理二进制数据而不是字符串。

在第一个过程中创建的 MessageEditorControl UserControl 将实例化为 messageEditorControl:

private MessageEditorControl messageEditorControl

messageEditorControl 实例承载在 CreateEditor 方法创建的插件对话框中。 此外,messageEditorControl 的 RichTextBox 将由 IHttpBody 中的内容进行填充。 但是,除非 SupportsContentType 返回 true,否则无法创建插件。 对于此编辑器,如果 IHttpBody 中的 ContentType 包含“msbin1”,SupportsContentType 返回 true。

当完成字符串主体的编辑并且用户在插件对话框中单击**“确定”时,将调用 GetNewValue 以获取字符串形式的已编辑文本,并在 Web 测试性能编辑器的请求中更新“BinaryHttpBody.Data”**。

向类中添加 IBinaryHttpBodyEditorPlugin

  • 在前面过程中添加的 XmlMessageEditor 类下编写或复制以下代码以从 IBinaryHttpBodyEditorPlugin 接口实例化 Msbin1MessageEditor 类并实现所需方法:

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

生成和部署插件

为 IStringHttpBodyEditorPlugin 和 IBinaryHttpBodyEditorPlugin 生成和部署产生的 dll

  1. 在“生成”菜单上,单击“生成 <Windows 窗体控件库项目名称>”。

  2. 退出 Visual Studio 旗舰版。

    提示

    为了确保在尝试复制 dll 文件之前该文件没有锁定,必须退出 Visual Studio 旗舰版的所有实例。

  3. 将产生的 .dll 文件(例如,MessageEditors.dll)从项目 bin\debug 文件夹复制到 %ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\WebTestPlugins。

  4. 启动 Visual Studio 旗舰版。

    应向 Visual Studio 旗舰版注册该 .dll。

使用 Web 性能测试验证插件

测试插件

  1. 创建测试项目。

  2. 创建 Web 性能测试并在 Web 服务的浏览器中输入 URL,例如,http://dev.virtualearth. net/webservices/v1/metadata/searchservice/dev.virtualearth. net.webservices.v1.search.wsdl。

  3. 记录完成后,在 Web 性能测试编辑器中展开对 Web 服务的请求,然后选择**“字符串主体”“二进制主体”**。

  4. 在“属性”窗口中,选择“字符串主体”或“二进制主体”,然后单击省略号 (…)。

    将显示**“编辑 HTTP 主体数据”**对话框。

  5. 现在即可以编辑数据,然后单击“确定”。 此操作会调用适用的 GetNewValue 方法以更新 IHttpBody 中的内容。

编译代码

  • 验证 Windows 控件库项目的目标框架是否为 .NET Framework 4。 默认情况下,Windows 控件库项目以 .NET Framework 4 客户端框架为目标,不允许包含 Microsoft.VisualStudio.QualityTools.WebTestFramework 引用。

    有关更多信息,请参见“项目设计器”->“应用程序”页 (C#)

请参见

任务

如何:创建请求级插件

如何:为 Web 性能测试创建自定义提取规则

如何:为 Web 性能测试创建自定义验证规则

如何:创建负载测试插件

如何:创建编码的 Web 性能测试

参考

IStringHttpBodyEditorPlugin

CreateEditor

SupportsContentType

GetNewValue

IBinaryHttpBodyEditorPlugin

CreateEditor

SupportsContentType

GetNewValue

IHttpBody

ContentType

UserControl

RichTextBox

其他资源

为负载和 Web 性能测试创建和使用自定义插件

如何:为 Web 性能测试结果查看器创建 Visual Studio 外接程序