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”.
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’.
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:
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.
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:
Now my test runs successfully:
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
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):
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.
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,