Friday, October 22, 2010

Web Test & Data Source

One of the benefits of web tests in VS that they can be combined into the scenario. For example, Scenario includes two web tests and each of them uses the same data from data source.  I guess, it will be more elegant to have such data source in Scenario and do not add it to the each of test separately. Magic property of data source “Select all columns” will help us to do this.


After that, all columns will be accessible in the Context. Each column will be automatically binded to the corresponding context parameter with name “DataSourceName.TableName.ColumnName”.

For example, we need to check response and find value from the data source. Custom Validation rule will help.
[Description("Validate text from data source column at the response")]
public class ValidateData : ValidationRule
{
    [Description("Binded to the DataSource Context parameter name = DataSourceName.TableName.ColumnName")]
    public string DataSourceName { set; get; }

    public override void Validate(object sender, ValidationEventArgs e)
    {
        if (DataSourceName == null)
            return;
        if (e.Response.BodyString == null)
            return;

        var searchData = e.WebTest.Context[DataSourceName].ToString();
        e.IsValid = e.Response.BodyString.Contains(searchData);
        e.Message = e.IsValid
            ? string.Format("{0} found", searchData)
            : string.Format("Did not find  {0}", searchData);
    }
}

Validation rule can be added by the right click on the request that should be validated. Fill DataSourceName value and run the test.
Validation result will be at the “Details” tab of the web test result file.

No comments:

Post a Comment