Problem:
I need dates in my WebTest to always be in the future.
Solution:
Create a WebTest Plug-in to generate the dates and set
Context Parameters to hold the values.
Code:
using System;
using
System.ComponentModel;
using
Microsoft.VisualStudio.TestTools.WebTesting;
namespace
DLBRacingTest.Custom
{
[Description("Stores four dates in the test context
[ContextParameterName], [ContextParameterName]Month,
[ContextParameterName]Year, [ContextParameterName]Day")]
public class GenerateDates
: WebTestPlugin
{
[Description("The number of days to add.")]
public double Days { get; set; }
[Description("The number of Hours to add.")]
public double Hours { get; set; }
public string ContextParameterName { get; set; }
public override void
PreWebTest(object sender, PreWebTestEventArgs e)
{
DateTime
temp = DateTime.Now.AddDays(Days).AddHours(Hours);
e.WebTest.Context.Add(this.ContextParameterName, temp.ToString("ddd, MMM d, yyyy"));
e.WebTest.Context.Add(this.ContextParameterName + "Month",
temp.Month.ToString());
e.WebTest.Context.Add(this.ContextParameterName + "Year",
temp.Year.ToString());
e.WebTest.Context.Add(this.ContextParameterName + "Day",
temp.Day.ToString());
}
}
}
Explanation:
Add the code above to any test project or
create a class library and reference the library from your test project. Doing so will make this WebTest Plug-in
available to be added to your web test.
To add the WebTest Plug-in to your test simply
right click root of your web test and select “Add WebTest Plug-in...” You will be shown a dialog to select the
desired plug-in.
There are three parameters with this plug-in.
The first two are days and hours to be added to the current date. These values can be negative if you want a
date in the past or 0 if you want todays date.
The third parameter is the context parameter name you want assinged to
the context parameter. In the screen
shoot above we request a date 7 days in the future to be assinged to a context
parameter named MyDate. Note that this
plug-in actually creates 4 context parameters as explained in the description
area of the plug-in dialog. If todays
date is Jan 20, 2009 the resulting context parameters would be:
MyDate = Tue, Jan 27, 2009
MyDateMonth = 1
MyDateYear = 2009
MyDateDay = 27
Note that unlike Context Variables generated by
Extraction Rules the Context Variables added by WebTest Plug-ins do not appear
in the Value drop down in the properties window of a Form Post Parameter. There are two methods to
assigning the Context Parameters to Form Post Parameters in your web test. The first method is to simply type the name
of the Context Parameter between double open and double close curly braces i.e.
{{MyDate}}. This method is quick and
easy but is suspectable to errors if you miss type the variable name. This method can also become tedious if you
have to use this Context Variable in many locations of your web test.
The second method is to add Context Variables
(as place holders) to your Web Test.
This will place the Context Variables in the Values drop down in the
properties window. This ensures the
correct spelling is used and makes updating the test very easy. To add a Context Variable simply right click
on the root of your web test and select "Add Context Variable" from the context
menu. Select the Context Variable and
update the Name property to the name of your context parameter i.e.
MyDate. You can leave the context
variables or delete them once you have them bound to all the request in your
test. If you delete them however, click
No when asked if you would like to clear the bindings.
Now everytime you execute this web test the
dates will be 7 days in the future of the date you execute the test.