Yesterday, I talked about the basics of creating a new web test.  Today, I will talk about how to customize a web test.  When you build a standard web test is uses fixed values that would have to be changed manually every time the test was run.  For example, you would have to change the pickup date to make sure the dates were valid.  If you create a coded web test, this is no problem since you can have the pickup date based off of today's date.
 
Although you can create a coded web test by inheriting from the WebTest class, the easiest way to create one is by clicking the Generate Code button.  Once you click that button a new class will be created that inherits from WebTest and overrides the GetRequestEnumerator() method.  It is this method where all the code goes. 
 
In this example, I am going to show how to make a simple test to verify that a rate shop was successful.  We'll look at this a block at a time.  Remember this code is autogenerated.  The first test will verify that the home page loaded successfully by looking for the string GET RATES (next to the go button).
WebTestRequest request1 = 
new WebTestRequest("https://tweb46/reservations/index.aspx");
request1.ThinkTime = 5;
ValidationRuleFindText rule1 = new ValidationRuleFindText();
rule1.FindText = "Get Rates";
rule1.IgnoreCase = true;
rule1.PassIfTextFound = true;
request1.ValidateResponse 
+= new EventHandler<ValidationEventArgs>(rule1.Validate);
ExtractHiddenFields rule2 = new ExtractHiddenFields();
rule2.ContextParameterName = "1";
request1.ExtractValues 
+= new EventHandler<ExtractionEventArgs>(rule2.Extract);
yield return request1;
A new WebTestRequest object is created for every postback that occurs.  The ThinkTime parameter is the amount of seconds that the test should wait before proceeding to the next step (this value is set by recording the test).  Typically, I change this to 0 or 1, so that the test will execute as fast as possible.  The section after that creates a validation rule that looks for the text Get Rates.  The section after that creates the code that automatically extracts the viewstate parameters.  Lastly yield return request1, sends the results back to the web test client in Visual Studio.
WebTestRequest request2 = 
new WebTestRequest("https://" + serverName + "/reservations/index.aspx");
request2.Method = "POST";
BindHiddenFields request2BindHiddenFields = 
new Microsoft.VisualStudio.QualityTools.WebTestFramework.BindHiddenFields();
request2BindHiddenFields.HiddenFieldGroup = "1";
request2.PreRequest 
+= new EventHandler<PreRequestEventArgs>(request2BindHiddenFields.PreRequest);

FormPostHttpBody request2Body = new FormPostHttpBody();
request2Body.FormPostParameters.Add("LocationTime_ascx1:PickupLocationTextBox", 
ConfigurationSettings.AppSettings["DefaultLocationCode"]);
request2Body.FormPostParameters.Add
("LocationTime_ascx1:PickupMonthYearDropDownList", 
DateTime.Now.AddDays(1).ToString("MM/yyyy"));
request2Body.FormPostParameters.Add("LocationTime_ascx1:PickupDayDropDownList", 
DateTime.Now.AddDays(1).Day.ToString());
request2Body.FormPostParameters.Add("LocationTime_ascx1:PickupTimeDropDownList", 
"09:00");
request2Body.FormPostParameters.Add
("LocationTime_ascx1:ReturnMonthYearDropDownList", 
DateTime.Now.AddDays(2).ToString("MM/yyyy"));
request2Body.FormPostParameters.Add("LocationTime_ascx1:ReturnDayDropDownList", 
DateTime.Now.AddDays(2).Day.ToString());
request2Body.FormPostParameters.Add("LocationTime_ascx1:ReturnTimeDropDownList", 
"09:00");
request2Body.FormPostParameters.Add("LocationTime_ascx1:VehicleTypeDropDownList", 
"FullSize");
request2Body.FormPostParameters.Add
("LocationTime_ascx1:CorporateDiscountNumberTextBox", 
"");
request2Body.FormPostParameters.Add
("LocationTime_ascx1:PromotionCodeTextBox", "");
request2Body.FormPostParameters.Add("LocationTime_ascx1:BlueChipNumberTextBox", 
"");
request2Body.FormPostParameters.Add("LocationTime_ascx1:SubmitButton.x", "11");
request2Body.FormPostParameters.Add("LocationTime_ascx1:SubmitButton.y", "10");
request2.Body = request2Body;

ValidationRuleFindText rule3 = new ValidationRuleFindText();
rule3.FindText = "Total Base Rate";
rule3.IgnoreCase = true;
rule3.PassIfTextFound = true;
request2.ValidateResponse += new 
EventHandler<ValidationEventArgs>(rule3.Validate);
yield return request2;
This next block of code is similar.  Here you can see the value of each asp.net control being posted back to the server.  Lastly, a validation rule looks for the text Total Base Rate to verify that the rate was successful.

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=108