A Visual Studio web performance test required the current date and time to be written to a context parameter for inserting into a request. Writing the current date and time to a string is straightforward and it is simple to write a string into a content parameter within the web test plugin.
using Microsoft.VisualStudio.TestTools.WebTesting;
namespace Plugins
{
[System.ComponentModel.DisplayName(
"Save current date time to context parameter")]
[System.ComponentModel.Description(
"Saves date time of call to a context parameter")]
public class DateTimeToContext : WebTestRequestPlugin
{
[System.ComponentModel.DisplayName("Parameter name")]
[System.ComponentModel.Description("Name of context parameter.")]
public string ParameterName { get; set; }
[System.ComponentModel.DisplayName("Date-time format")]
[System.ComponentModel.DefaultValue("yyyy-MM-dd HH:mm:ss.fff")]
public string DateTimeFormat { get; set; }
public override void PreRequest(object sender, PreRequestEventArgs e)
{
string now
= string.IsNullOrWhiteSpace(DateTimeFormat)
? System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")
: System.DateTime.Now.ToString(DateTimeFormat);
e.WebTest.AddCommentToResult(string.Format(
"Setting context parameter '{0}' to '{1}'", ParameterName, now));
e.WebTest.Context[ParameterName] = now;
}
}
}
I like to add calls of
e.WebTest.AddCommentToResult(...)
at significant places in plugins, particularly when developing a plugin or a web test. In this case it simply reports the values that are set into the web test log.
No comments:
Post a Comment