Welcome to MSDN Blogs Sign in | Join | Help

How to upload a file in a Web test

From this post, I see many users are having trouble with file uploads.

Really this is not as easy as it should be.

To upload a file, first record the file upload. I have a simple site that I did that on to generate this web test.

The key parameter here is obviously the file upload parameter, which recorded the file as "MyImage.gif".

image

At runtime, the web test engine will look for a file with the name MyImage.gif in the test deployment directory.

If I just go ahead and run this the test fails with:

Request failed: Could not find file 'C:\Users\edglas.000\Documents\Visual Studio 2008\Projects\TestProject1\TestResults\edglas_EDGLAS-LT2 2008-08-05 08_57_58\Out\MyImage.gif'.

image

Of course the first thing you'll notice is the file path was not recorded in the web test. So how will this file be found? and why is it looking in that directory?

When you run tests in VS, the files required to run the tests are "deployed" to a directory for the test run (see my post on deployment), in this case it is edglas_EDGLAS-LT2 2008-08-05 08_57_58. You'll get a different directory every time you run your tests. Notice this directory is available in the web test context:

image

The best way to handle this in your test is to actually add the file to be uploaded to your test project, then add it as a deployment item on your test. That way it will be copied to the out directory prior to running the test, and will be available during execution. This also has the advantage that if you are working with others, the test will also run on their machine (hard-coded full paths in a test are bad, as these tests will fail on another person's machine if their drive isn't set up the same way).

Once you've added the file to your project, add it as a deployment item. There's two ways to do this, on the run config or on the test. Since this file is really associated with the test, I recommend putting it on the test. This is not discoverable at all. First, open test view (Test menu | Windows | Test View) and select the web test. Then set the Deployment Items property for the web test's test element.

image

Now add MyImage.gif as a deployment item by clicking ... next to the deployment items property. Since this is a property on the web test, the path is relative to the web test:

image

Now my test runs successfully:

image

Another approach is to create a folder in your project where you put all your file upload files.

Then specify the relative path in the deployment item properties (relative to the web test in the project). So if my file to upload is in the FileUpload folder

image

Now in deployment items specify the relative path, which again is relative to the web test. Note the path in the file upload web test parameter is not relative since it will be published "flat" with no subdirectories (no changes required):

image

Now the test runs successfully again.

Another option is to deploy the files or entire directory using the run config settings.

For the run config, go to the Deployment tab and use the Add Directory to add your folder with files to upload. Note that this path is solution relative, since the run config is in the solution directory. The <Solution Directory> macro is automatically inserted after you select the file or directory.

image

Now if I go to the deployment dir using Explorer (easiest way to find it is from the web test context parameter), I see that both of my images were deployed, and my test still runs successfully. Also any new files I want to upload I can just drop them in this folder in my solution and don't have to add it as a deployment item to my test. Note that all files are published "flat", which means you can't have two different deployment files in different folders with the same name.

Hope this helps,

Ed.

Published Tuesday, August 05, 2008 7:24 PM by edglas

Comments

# re: How to upload a file in a Web test

Thanks for going over this in detail Ed.  it seemed like an issue that came up over and over on the forums.. I know I must have answered it a bunch of times when I was more active there.  (and I expect you folks ended up pointing other people to my responses)

it's great to have a step by step on this for process.

Tuesday, August 05, 2008 9:59 PM by ChuckvdL

# re: How to upload a file in a Web test

Hi,

When I build my test solution into "Release" folder and try to run my OrderdTest from the command line using "MSTest.exe" the webtest fails with an Exception:

"Request failed: Exception in PreWebTest event: Could not load file or assembly 'AutomaticTesting.Utils"

The assembly was placed by the build in my "Solution\Release" folder, and I am calling "MSTest.exe" from that exact folder.

How can I expand the search path for "MSTest.exe"?

Thank you

p.s.

When ruining the tests from VSTS it executes as expected.

Wednesday, August 06, 2008 6:30 AM by Amitai

# re: How to upload a file in a Web test

Thanks Chuck, good to hear from you again!

Ed.

Wednesday, August 06, 2008 6:49 AM by edglas

# re: How to upload a file in a Web test

Amitai, mstest will look in the current directory for the dll. Are you in the release folder when you run mstest?

Ed.

Wednesday, August 06, 2008 6:51 AM by edglas

# re: How to upload a file in a Web test

Ed,

Thanks for the quick answer.

Yes I am in the release folder when running mstest.

Any idea how I can proceed investigating this problem?

Wednesday, August 06, 2008 7:29 AM by Amitai

# re: How to upload a file in a Web test

Hi Ed,

I used the option using the run config. I am also performing load testing uploading large files. My asp page has the Telerik upload control. One problem I have is with a query string parameter the telerik control inserts into the url to uniquely identify the file being uploaded as the control is streaming the file to the server. VS did not recognise the query string as a dynamic parameter. I need to assign a guid to the query string parameter for each run of the test script. Your thoughts on how to do this would be appreciated.

Cheers

Peter

Wednesday, August 06, 2008 7:45 AM by peterka

# re: How to upload a file in a Web test

Peter, do you know how the guid gets generated?

If it really is a GUID you need, write a request plugin that generates a guid into a context param in the PreRequest call using Guid.NewGuid().ToString(), then attach the plugin to your request and bind the value of the context parameter to the query string parameter.

Ed.

Wednesday, August 06, 2008 12:08 PM by edglas

# re: How to upload a file in a Web test

Amitai, I would expect this to work.

So to get this straight, from the command line you cd to the bin\release folder for your project, then run

mstest /testcontainer:yourorderedtest.ordertedtest

The ordered test contains a web test, which references a custom plugin in the AutomaticTesting.Utils.dll.

Are the web test, ordered test, and dll all in the directory?

If you just run the web test, does that work?

Ed.

Wednesday, August 06, 2008 12:13 PM by edglas

# re: How to upload a file in a Web test

Ed, I have a solution with more than 20 test projects, I have defined a Solution Release folder and all the projects files are placed there by the build.

When I open  the "Visual Studio Command Prompt" and from there

cd "MySolution\Release"  and then:

MSTest.exe /testcontainer:"MyTests.orderedtest" /runconfig:"..\MyConfig.testrunconfig"

Both" MyTests.orderedtest" and "MyConfig.testrunconfig" are found but  "MyFirstWebTest.webtest" is referencing  a custom plugin defined in AutomaticTesting.Utils.dll  and this .dll file is in "MySolution\Release".

The orderedtest and the dll are in the same directory and the webtest is in a sub directory of it.

I am getting the same error if I try to run "MyFirstWebTest.webtest" directly.

Cheers

Amitai

Thursday, August 07, 2008 6:37 AM by Amitai

# re: How to upload a file in a Web test

Hi Ed,

From investigations it appears that as the asp page is rendering, the Telerik upload control inserts a javascript function.

Sys.Application.add_init(function() {

   $create(Telerik.Web.UI.RadProgressManager, {"ajaxCallUrl":"/SchoolsGold/Telerik.RadUploadProgressHandler.ashx","clientStateFieldID":"ctl00_DefaultContent_RadProgressManager1_ClientState","formId":"aspnetForm","pageGUID":"7a457cf2-2802-4352-947b-d04ee6238522","shouldRegisterForSubmit":false}, null, null, $get("ctl00_DefaultContent_RadProgressManager1"));

The client side javascript of the telerik controls adds the PageGUID into the querystring.

Would writing an extraction rule to and then use the context parameter to build the querystring be an good approach?

Cheers

Peter

Thursday, August 07, 2008 8:08 PM by peterka

# re: Telerik file upload

Peter, yes extracting the GUID from the ajax call response is the right way to go. I would have expected dynamic parameter detection to pick this up, but it missed it?

Ed.

Monday, August 11, 2008 9:18 AM by edglas

# re: How to upload a file in a Web test

Ed, unfortunately there was no dynamic parameter detected.

Peter

Monday, August 11, 2008 9:47 PM by peterka

# re: How to upload a file in a Web test

Hi Ed,

I am now able to run my webtests from the release folder :)

I have added the Release folder to the Deployment section of my testrunconfig file, so all the Release folder content is copied to the Out folder under the Release\TestResults folder.

This looks to me to be kind of a "Hack" :(

Is this the only way to run MSTest.exe from a Release Build?

Thank you very much

Amitai

Tuesday, August 12, 2008 6:21 AM by Amitai

# re: How to upload a file in a Web test

Hi Ed

We are using VSTS for Peformance Testing of Sharepoint application.

In the file upload scenario, i have added the file upload parameter & also deployed the file as a deployment item. However the upload does not happen successfully. I have the below queries:

1. Can u pls elaborate on the file upload parameter. How to identify to which request it should be added? what should be entered in the the "Name" field of the file upload parameter?

2. If the file is available in the "out" folder of the results directory, can we assume that we have correctly added the file as a deployment item to the test?

Friday, February 20, 2009 7:05 AM by Jamuna
Anonymous comments are disabled
 
Page view tracker