Search code examples
visual-studioload-testingweb-performance

Webtest : Editing the context parameter during test run


I'm using Visual Studio 2015 Web Performance test (.webtest) and have an Extraction Rule in place to capture a 8-digit number that references a check number (via inner text) into a context parameter.

If the number only contains 6-digits, then it has two blank spaces in front of the check number. This causes an issue because I'm using the check number in a form parameter and those blank spaces need to be switch to zeroes (0).

My question is what's the best way to handle the comparison? Is there a way to edit the context parameter (named "CheckNBR"), or can I overwrite the Extraction Rule to manipulate the parameter? Maybe create a custom Extraction Rule instead? I'm kinda going in all directions on this and not sure what options work the best.

[Update] Instead of determining the best way, I'm re-directing the question towards the editing of the context parameter. Once I set the parameter from the Extraction rule, how can I edit it?


Solution

  • There are several possible approaches.

    You could write a custom extraction rule that finds the required text, modifies it as needed and then save it to a context variable. This is probably the most complicated version to write.

    You could write a custom extraction rule that uses the built in extraction rule and then modifies the result. Code based on the following (not tested, not compiled) should work. Of course you need to write your own version of ModifyTheTextAsNeeded. Then change the web test to use the extraction below instead of the original.

    public class ExtractAndModifyHtmlTagInnerText : ExtractHtmlTagInnerText
    {
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            base.Extract(sender, e);
    
            string extractedText = e.WebTest.Context[this.ContextParameterName].ToString();
            string modifiedText = ModifyTheTextAsNeeded(extractedText);
            e.WebTest.Context[this.ContextParameterName] = modifiedText
        }
    }
    

    Another approach is to put something similar to last three lines of the body of the method shown above above into a plugin. It might be a PreRequest plugin used on the next request after the one with the extraction rule.