File Upload In A Web Test: The Case Of Submitting A Form With JavaScript

 Alik Levin    This post documents steps we have taken to simulate file upload with Web Test when the form is submitted via JavaScript [vs. simple postback]. Credits go directly to Shai Raiten, MVP, brilliant young man, and the mind behind the workaround.

Quick Resource Box

The technique described in the post exploits fantastic Custom Extraction Rule extensibility options in Visual Studio 2008. It also proves once more endless usefulness of Fiddler.

The Challenge – Web Test Does Not Record File Upload

Our main scenario required to upload files to the web server. Quick search on the web revealed Ed Glas’ post How to upload a file in a Web test. The future looked great. When we followed the steps outlined in the post our Web Test kept failing in recording the file to be uploaded preventing the automation of the load test.

After quick research we identified the difference in our situation – the form was submitted by JavaScript’s Form.Submit(). That is why Visual Studio wasn’t able to record it. We used Fiddler – it captured everything just fine. Next was to save Fiddler’s capture as Visual Studio Web Test:


and then add the Web Test to the solution in Visual Studio (right click on the Project in Solution Explorer-> Add-> Existing Item…).

Creating Custom Extract Rule

When we inspected the capture in Visual Studio we observed the following String Body header that actually was the uploaded file’s contents:


We decided to create custom Extraction Rule that would first extract the user name – the name of the files included user name – and based on it look up related file, then read its contents into context parameter. Later on we used the context parameter to dynamically add the file contents to the request:


The rule itself looked similar to this:

public class FileContentExtractorData : ExtractionRule


{     private const string Folder = \@\\YOUR FILE PATH GOES HERE;     public string UserName { get; set; }     private const int Len = 9;     public override void Extract(object sender, ExtractionEventArgs e)     {         if (e.Response.HtmlDocument != null)         {             //USER NAME WAS PART OF THE FILE NAME             string file_path = Folder + FullName(UserName) + ".txt";             e.WebTest.Context.Add(this.ContextParameterName, File.OpenText(file_path).ReadToEnd());         }     }     //WELL KNOWN FEATURE BY DESIGN, VS REMOVES //LOOK FOR ‘Leading zeroes dropped from datasrouces //values bound to a CSV file’ //IN Performance Testing Guidance How-To's     //LEADING ZEROS BUT WE NEEDED THEN ALL     public string FullName(string name)     {         if (name.Length < Len)         {             StringBuilder new_name = new StringBuilder(Len);             int delta = Len - name.Length;             for (int i = 0; i < delta; i++)                 new_name.Append("0");                 new_name.Append(name);             return new_name.ToString();         }         else             return name;     } }



Related Books