WebTestPlugin 类

更新:2007 年 11 月

提供一种手段,可以在 Web 测试运行前和运行后,运行代码并访问 WebTest。必须继承此类。

命名空间:  Microsoft.VisualStudio.TestTools.WebTesting
程序集:  Microsoft.VisualStudio.QualityTools.WebTestFramework(在 Microsoft.VisualStudio.QualityTools.WebTestFramework.dll 中)

语法

声明
Public MustInherit Class WebTestPlugin
用法
Dim instance As WebTestPlugin
public abstract class WebTestPlugin
public ref class WebTestPlugin abstract
public abstract class WebTestPlugin

备注

使用 WebTestPlugin 和使用 WebTest 类的 PreWebTest/PostWebTest 事件在功能上是等效的。区别在于:只能在编码的 Web 测试中使用事件。

对继承者的说明:

当从 WebTestPlugin 继承时,必须重写下面的成员:PostWebTestPreWebTest

示例

下面的示例演示一个 Web 测试插件,此插件在运行 Web 测试之前将随机数添加到上下文。同样,您可以在运行了 Web 测试后重写 PostWebTest 并执行操作。例如,您可能希望将完成 Web 测试所用的时间以及在 Web 测试期间发出的请求数写入日志文件。

using System;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Windows.Forms;

namespace WebTestPluginNamespace
{
    public class MyWebTestPlugin : WebTestPlugin
    {
        public static string NewRandomNumberString(int size)
        {
            byte[] buffer = new byte[size];
            // Seed using system time
            Random random = new Random(unchecked((int)DateTime.Now.Ticks));

            random.NextBytes(buffer);
            return BitConverter.ToInt32(buffer, 0).ToString();
        }

        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {
            e.WebTest.Context["RandNum"] = NewRandomNumberString(4);
        }
        
    }
}

继承层次结构

System.Object
  Microsoft.VisualStudio.TestTools.WebTesting.WebTestPlugin
    Microsoft.VisualStudio.TestTools.WebTesting.WebTestRequestPluginConverter

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

另请参见

参考

WebTestPlugin 成员

Microsoft.VisualStudio.TestTools.WebTesting 命名空间

其他资源

如何:创建 Web 测试插件