Sep 26, 2013

How to POST (upload) a file in a Visual Studio web test

So, I've came up with a task of performing a basic load test of a web service. For that purpose I've chosen the WebTest of Visual Studio 2010 Ultimate Test Project. For the most part as long as only GET requests were performed everything was pretty straightforward.

But there was one POST request that requred a file upload. A little googling led to this blog entry which describes the situation in detail. The only problem was that recording of the file upload didn't work. It didn't record anything at all, to be more correct. I guess that's because I'm in the end of 2013 and using Windows 8 and IE 10, and this breaks it somehow (you know, Windows 8 breaks a lot of things).

So basically situation is pretty dumb: you can't add "Form Post Parameters" to your POST request with a right click menu. Why (it's a rhetorical for Microsoft, I know)? And the only semi-official solution doesn't work, and it's pretty dumb solution, I might add.

The solution is quite simple however. A .webtest file is a XML, which utilises a http://microsoft.com/schemas/VisualStudio/TeamTest/2010 schema, which is located at %VsInstallDir%\Xml\Schemas\vstst.xsd. Using that schema it wasn't hard to find required tags. To create "Form Post Parameters" create a new POST request ("Add Web-Service Request"), save, and reopen .webtest as XML file. In the tag <Request> insert child elements:

<FormPostHttpBody>
<FormPostParameter Name="dummy" Value="test1" RecordedValue="" CorrelationBinding="" UrlEncode="True" />
</FormPostHttpBody>


save and close. Now you can reopen webtest with it's standard editor, and voila, you have the parameters, you can now right click on it and two new options apper in menu, inluding "Add File Upload Parameter" which is that we need. Everything else is already described in blog entry mentioned above.

2 comments: