Using Visual Studio 2012 to create web tests.
How can I access form post parameters from a web test recorder plugin? Most aspects of a recorded webtest are visible via Visual Studio's intelisense or from MSDN, but I cannot find the form post parameters.
I am testing a web site that uses form post parameters in an "interesting" manner. So far we have hand-edited the XML in the .webtest file, but that is error prone. So I would like to modify them in a web test recorder plugin.
Form post parameters can be accessed in a web test recorder plugin via the body field of a web request, but the body needs to be cast to the correct type. A recorder plugin provides has the recorded web test as a (field of a) parameter, the Items
of the test include the individual requests. They also include comments and so on. An Item
that is a WebTestRequest
may have a Body
field that, after casting, provides the form post parameters. This code shows a plugin that displays some details of the form post parameters via a WriteLine
method that is not shown here. The inner loop can be replaced with code to modify, or delete or add new form post parameters.
public override void PostWebTestRecording(object sender, PostWebTestRecordingEventArgs e)
{
foreach (WebTestItem wti in e.RecordedWebTest.Items)
{
WebTestRequest wtiwtr = wti as WebTestRequest;
if (wtiwtr != null)
{
FormPostHttpBody formBody = wtiwtr.Body as FormPostHttpBody;
if (formBody == null)
{
// no formBody.
}
else
{
WriteLine("Have {0} form post parameters", formBody.FormPostParameters.Count);
foreach (FormPostParameter fpp in formBody.FormPostParameters)
{
WriteLine("FPP '{0}' = '{1}'", fpp.Name, fpp.Value);
}
}
}
}
}
Several other parts of the recorded web test can be accessed via these casts of wti
in the code.
Comment wtic = wti as Comment;
IncludedWebTest wtiiwt = wti as IncludedWebTest;
SharepointInformation wtispi = wti as SharepointInformation;
TransactionTimer wtitt = wti as TransactionTimer;
WebTestConditionalConstruct wtiwtcc = wti as WebTestConditionalConstruct;