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;
}
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
WebTestRequest request1 = new
WebTestRequest(“http://localhost:63278/CaminoWebSite/Default.aspx”);
request1.Method = “POST”;
FormPostHttpBody request1Body = new FormPostHttpBody();
request1.Body = request1Body;
ValidationRuleFindText rule1 = new ValidationRuleFindText();
rule1.FindText = (string)Context[“CaminoDB1.Houses.name”];
request1.ValidateResponse += new
EventHandler<ValidationEventArgs>(rule1.Validate);
yield return request1;
}
}
}
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 “CaminoDB1.Houses.name”.
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.
One comment
Nice.
Krste