为 WebTest.PreWebTest 事件提供数据。
继承层次结构
System.Object
  System.EventArgs
    Microsoft.VisualStudio.TestTools.WebTesting.PreWebTestEventArgs
命名空间:  Microsoft.VisualStudio.TestTools.WebTesting
程序集:  Microsoft.VisualStudio.QualityTools.WebTestFramework(在 Microsoft.VisualStudio.QualityTools.WebTestFramework.dll 中)
语法
声明
Public Class PreWebTestEventArgs _
    Inherits EventArgs
public class PreWebTestEventArgs : EventArgs
public ref class PreWebTestEventArgs : public EventArgs
type PreWebTestEventArgs =  
    class
        inherit EventArgs
    end
public class PreWebTestEventArgs extends EventArgs
PreWebTestEventArgs 类型公开以下成员。
属性
| 名称 | 说明 | |
|---|---|---|
| .gif) | WebTest | 获取将要启动并引发了 PreWebTest 事件的 WebTest 对象。 | 
页首
方法
| 名称 | 说明 | |
|---|---|---|
| .gif) | Equals | 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。) | 
| .gif) | Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。) | 
| .gif) | GetHashCode | 用作特定类型的哈希函数。 (继承自 Object。) | 
| .gif) | GetType | 获取当前实例的 Type。 (继承自 Object。) | 
| .gif) | MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) | 
| .gif) | ToString | 返回表示当前对象的字符串。 (继承自 Object。) | 
页首
备注
这专门提供对调用了 PreWebTest 事件的 WebTest 的引用。
示例
以下示例显示了 Web 性能测试插件,此插件在运行 Web 性能测试之前将随机数添加到上下文。 在 Web 性能测试运行之后,此插件显示最后的 WebTestResponse 的长度。
请注意使用 PreWebTestEventArgs 提供对 WebTest 的引用。
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);
        }
        public override void PostWebTest(object sender, PostWebTestEventArgs e)
        {
            MessageBox.Show(e.WebTest.LastResponse.ContentLength.ToString());
        }
    }
}
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
请参见
参考
Microsoft.VisualStudio.TestTools.WebTesting 命名空间