Howto: use a DataSource to bind ValidationRuleFindText "FindText" property in a WebTest


One of the questions I received during one of my classes is if it is possible to bind the FindText property to a configured datasource. While this is not possible in a recorded webtest there is a way to make this work when you convert the test to a coded test. The only thing you need to know is that the Context that is available in your test class contains the values of the row from the datasource. You can access this data by just indexing the Context property. You can access the value from the dataset by using string index of the form “DataBindingName.TableName.FieldName”. You can assign this value to the FindText property to test if the text you have in the database is displayed on the webpage. Below you can find a code sample where I converted a recorded webtest to a CodedWebtest.


 


namespace FunctionalTests


{


 using System;


 using System.Collections.Generic;


 using Microsoft.VisualStudio.QualityTools.WebTestFramework;


 using Microsoft.VisualStudio.QualityTools.WebTestFramework.Rules;


   


   


 [DataSource(“CaminoDB1”, “Provider=SQLNCLI.1;Data Source=tfsdata;Integrated Security=SSPI;Initial Catalog=CaminoDB”,  Microsoft.VisualStudio.QualityTools.WebTestFramework.DataBindingAccessMethod.Sequential, “Houses”)]


[DataBinding(“CaminoDB1”, “Houses”, “name”, “CaminoDB1.Houses.name”)]


public class WebTest1Coded : WebTest


{


       


 public WebTest1Coded()


 {


  this.Proxy = null;


 }


       


&nbsp;public override IEnumerator<WebTestRequest> GetRequestEnumerator()


&nbsp;{


&nbsp;&nbsp; WebTestRequest request1 = new


&nbsp;&nbsp; WebTestRequest(“http://localhost:63278/CaminoWebSite/Default.aspx”);


&nbsp;&nbsp;


&nbsp;&nbsp; request1.Method = “POST”;


&nbsp;&nbsp; FormPostHttpBody request1Body = new FormPostHttpBody();


&nbsp;&nbsp; request1.Body = request1Body;


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


&nbsp;&nbsp; ValidationRuleFindText rule1 = new ValidationRuleFindText();


&nbsp;&nbsp; rule1.FindText = (string)Context[“CaminoDB1.Houses.name”];


&nbsp;&nbsp; request1.ValidateResponse += new


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EventHandler<ValidationEventArgs>(rule1.Validate);


&nbsp;&nbsp; yield return request1;


&nbsp;&nbsp;}


&nbsp;}


}


&nbsp;


I configured a DataSource that uses the CaminoDB database on my server TFSData. I also declared a DataBinding to the datasource that uses the Table Houses and the field name. I can use the value of this DataBinding from the context using the string &#8220;CaminoDB1.Houses.name&#8221;.


Of Course it would be great if Microsoft would also provide a way to do this way of databinding in a recorded WebTest without requiring us to convert to a coded test just for this kind of functionality. Hopefully they will improve on this feature before RTM.