質問

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;

他のヒント

It is a little ugly but I was able to find it while debugging a plugin that uses the PreRequestDataBinding method. Putting my mouse over 'e', expand request -> body -> [Microsoft.VisualStudio.TestTools.WebTesting.FormPostHttpBody] -> FormPostParameters. But due to the protection level you won't be able to edit the value.

What I ended up doing was convert a web test into a coded web test. You can do this by right-clicking on the top level of the web test designer view, then choosing Generate Code. Looking at the coded web test, will allow you to modify the form post parameters. Look for something like this...

request1Body.FormPostParameters.Add("YourFormPostName", "value");

You can add form post parameter like this in PreRequest method of WebTestRequestPlugin -

     FormPostHttpBody form = new FormPostHttpBody();
     FormPostParameter par = new FormPostParameter();
     par.Name = "name";
     par.Value = "1";
     form.add(par);
     e.Request.Body = form;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top