Search code examples
visual-studiowebperformance-testing

Visual Studio: Use csv file for “Find Text” validation in web performance test not working


I am using csv file for posting the data and checking the response back, where in find text, i want to use the same csv files.

In string body im using :{{DataSource1.Table#csv.objectId}} which is working fine for posting request. same thing i passed in find text in validation rule, it doesn't work and i am getting failure for this validation rule.

In validation rule, i am using "Find Text" and passing {{DataSource1.Table#csv.objectId}}

The "Details" tab show this for the validation rule :

Find Text Validation The required text '{{DataSource1.Table#csv.objectId}}' did not appear in the HTML response. FindText={{DataSource1.Table#csv.objectId}}, IgnoreCase=True, UseRegularExpression=True, PassIfTextFound=True

Tried using custom validation rule and tried passing context parameter.Same issue.Validation rule for it is below:


Solution

  • Many places where a ".webtest" allows a context parameter (CP) have a "bind" facility on the property. For example, look at the Value property of a "Header" to a request, it has a drop-down list giving access to CPs.

    Some properties of a ".webtest" allow CPs to be embedded in the text. For example the Url property of a request allows things like text{{CpOne}}moretext{{CpTwo}}etc.

    Unfortunately there are some properties of a ".webtest" that only allow text and do not expand embedded CPs. I have not found any clear statement of which places allow bound CPs, which support embedded CPs and which allow neither. Also I have not found any built in support for CPs in user-written plugins or extraction rules or validation rules. My approach has been to try using CPs wherever I need them and, when they do not work, write my own code that does what is needed.

    You might write your own validation rule as described in this Microsoft page. The code could be based on the following code which has not been tested.

    public class FindCpValueInResponseBody : ValidateResponseUrl
    {
        public string CpWithTextToFind { get; set; }
    
        public override void Validate(object sender, ValidationEventArgs e)
        {
            string TextToFind = e.WebTest.Context[CpWithTextToFind].ToString();
    
            e.WebTest.AddCommentToResult("Looking for '" + TextToFind +"' (from CP '" + CpWithTextToFind + "').");
    
            if (e.Response.BodyString.Contains(TextToFind))
            {
                e.IsValid = true;
                e.Message = "Text '" + TextToFind + "' found.";
            }
            else
            {
                e.IsValid = false;
                e.Message = "Text '" + TextToFind + "' not found.";
            }
        }
    }
    

    It should be a simple matter to enhance the above method to add the other properties of the built-in FindText validation rule; properties such as Pass if text found and Ignore case etc, should they be needed.

    The above validation rule takes the context parameter name. So if the value is from a data source then the value should be something like DataSource1.Table#csv.objectId. Note that there are no curly braces (i.e. no { or }) and no leading or trailing whitespace.