Search code examples
c#visual-studioweb-testing

How to pass a .cs parameter to Visual Studio WebTest QueryStringParameter?


I have created a webtest in visual studio to perform a test on a Rest API.I have some code that generates a random serial number that i need to pass into my QueryString parameter is there anyway to do this? I noticed that i can bind xml, csv and a database to it for Dynamic values. Would i have to change my code to wright to a xml file or is it possible to perform a direct call to the method inside the .cs file? C# Langauge.

I want to do something like in the below method but i want the sessionToken.sessionToken() to be the method that was called from the .cs file.

QueryString Parameters
     generatedToken=sessionToken.sessionToken()

Solution

  • Write your code within the PreRequest method of a Web Test Request plugin, or call your method from the plugin. One of the last statements of the plugin should write the generated value to a context parameter, with code something like:

    e.WebTest.Context["YourContextParameter"] = TheGeneratedValue.ToString();
    

    The value of the query string parameter can then be in one of these styles

    {{YourContextParameter}}
    

    or

    Some text{{YourContextParameter}}more text
    

    With a little more effort you can pass the context parameter name as a parameter of the plugin.

    It can be useful to add diagnostics from the plugin into the log; to showit has been called and to record its output. This can be done with a statement of the form:

    e.WebTest.AddCommentToResult("Plugin result is " + TheGeneratedValue.ToString());
    

    Another approach is to convert the entire webtest to a coded test, by using one of the command icons just above the webtest. That produces C# that you can edit as much as you want. However, that conversion is a one-way process. After doing it you cannot use the webtest editor to further change the test. Hence I recommend the plugin route.