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.


Comments (27)

  1. ChuckvdL says:

    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.

  2. Amitai says:

    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 "SolutionRelease" 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.

  3. edglas says:

    Thanks Chuck, good to hear from you again!

    Ed.

  4. edglas says:

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

    Ed.

  5. Amitai says:

    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?

  6. peterka says:

    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

  7. edglas says:

    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.

  8. edglas says:

    Amitai, I would expect this to work.

    So to get this straight, from the command line you cd to the binrelease 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.

  9. Amitai says:

    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 "MySolutionRelease"  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 "MySolutionRelease".

    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

  10. peterka says:

    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

  11. edglas says:

    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.

  12. peterka says:

    Ed, unfortunately there was no dynamic parameter detected.

    Peter

  13. Amitai says:

    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 ReleaseTestResults 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

  14. Jamuna says:

    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?

  15. Alaa Gabr says:

    Hello there

    I am trying to record a web test on a MOSS application, for uploading an image, when I run the script it fails and gives me the following error:

    Request failed: Context parameter ‘$HIDDEN1.MSOShowUnapproved_Xml’ not found in test context

    Request failed: Context parameter ‘$HIDDEN2.MSOShowUnapproved_Xml’ not found in test context

    Request failed: Context parameter ‘$HIDDEN3.MSOShowUnapproved_Xml’ not found in test context

    Although I have added the image in the [Deployment Items] property for this webtest, from the class view.

    Can anyone please advise me what else to do to solve this?

  16. Vegard says:

    Any suggestion on how I can make this work in the VS 2012 edition?

  17. Murali says:

    Hi I have uploaded an image but its actually taking some alphanumeric value instead of image file name which I had uploaded. Need your halp on this.

  18. Nari says:

    I also had this error  and I fixed as you say, but still error. I used Visual Studio 2013. How to fix?

  19. Affi says:

    How big files could be uploaded using VSTS? Can we upload a 5 GB file if the network supports or there is any limitaions? Please let us know?

  20. Pragati says:

    Hi Ed…Thank You for the stepp by step description. It really helps a core manual tester like me.

    I am running a web test scenario where the file uploaded is uniqu each time its uploaded. Now if I add multiple files to the Deployment item relative to web test, how do i ensure that when I do the load test for this scenario, it picks up a different file each time?

  21. Kaustubh says:

    How to do file upload in 2012 version? I can't see option 1 and 2 doable in 2012. I can't see the test view section even though I followed the steps mentioned in customized your tool via tool->Customize->Add new tool bar etc…are there any setting I'm missing to do it? Looks like easy way 2008..

    Awaiting the response…

  22. Isaac Abensur says:

    Tuve el mismo problema pero en Visual Studio 2013. Traté de seguir los pasos que se indican en ésta página pero al parecer en ésta versión las cosas han cambiado. Sea como fuere, lo resolví ejecutando una prueba de mi aplicación en el ambiente local (http://localhost/Proyecto/) y se copio el archivo de carga automáticamente al proyecto, cosa que no sucedió cuando ejecute la prueba sobre el proyecto publicado (http://www.pagina.com/Proyecto). Luego que terminó la prueba, modifiqué todos los enlaces reemplazando http://locahost por la URL de mi proyecto publicado.

  23. HTML5 Upload: Unable to upload files with multiple chunk with this approach. says:

    My HTML5 uploader uploads files with multiple chunk,  if I add the file in deployment item then VSTS is not able to devide the file in multiple different chunk , it sends the duplicate chunk body  so I get  corrupted files. Can you please solve the problem.

  24. Ashu says:

    hi, I am working with VSTS 2012 to check in a file from my browser. I tried the options provided by Ed above but in my case they are not working. I am having 4 dynamic parameters in my query string such as Link ID, hwnd, DocID and DocName. I tried to add extraction rule but I am not finding these values anywhere in the previous request. I did not get any errors while running the test but file is not uploaded after the test run. Please suggest!!!!

  25. viswa_npr@yahoo.com says:

    Hi Ed,

    Thanks for such great  information.  Out of two options you provided, I have choose the second option,  by Test Setting –> Deployment –> add File.  The reason I choose this is I am using Visual Studio 2013 ultimate where I couldn't see "Test View".    But I see "File attachment failed" error.  would you please help me why this happened and how to attach a file during webtest run.  

    When I see the request  content-Type  :   Multipart / form-data; boundary = ——————————–7d42893280670

    #ICOrigFileName    05022015040041786UpLoad_LoadTest.docx    

    But orignal path is  C:Usersmynamevisual studio 2013projectsfilesToUpload

    filename is :  FileToUpload.docx

  26. Satyajit321 says:

    Hardcoding is bad, but as a quick workaround it can be handly. Not able to get around the above steps on VS2015

    This one works:

    Update the 'File Upload Parameter' of the WebTest highlighted in the article to hardcoded filepath.

    WebTest->Expand the URL->Form Post Param->File Upload Param

    FileName: Upload.txt

    Change it to Fullname C:UploadUpload.txt

    NOTE:- Set 'Generate Unique Name' to False, supports versioning or overriting

  27. viswa_npr@yahoo.com says:

    Hi Satyajit321.

    Thanks for information.  The method you said worked.  

    -Viswa