Creating Dynamic Uris For Visual Studio Web Tests

This post is part of the F# Advent Calendar in English 2015 project. Check out all the other great posts there! And special thanks to Sergey Tihon for organizing this. (Also, thanks to Scott W from whom I copy/pasted the prior sentences.)

One of the cooler features built into Visual Studio 2015 is the ability to create web tests and load tests. I had blogged about customizing them here and here but those posts did not cover the scenario where I need to dynamically create a uri. For example, consider the following web test that is hitting a web api 2 controller with some very rpc syntax:

image

Notice that the ContextParameters are setting the uri so I can move the test among environments.  Also, notice the dynamic part of the uri called {{friendlyName}}.

One of the limitations of the out of the box web test is that context parameters cannot be data bound but can be appended as part of the uri. Also, query string parameters can be data bound cannot be appended as part of the uri. So if we want to go to a database and get a series of friendly names for our chickens to pass into our test, we are stuck.  Grounded really.

Enter web test plug ins.  I added a F# project to the solution and added a .fs file called UriAdjuster like so:

image

I then added references to:

Microsoft.VisualStudio.QualityTools.WebTestFramework and FSharp.Data.TypeProviders

image

I then added the following code to the UriAdjuster file:

namespace ChickenSoftware.ChickenApi.LoadTests.PlugIns open System open System.Text open Microsoft.FSharp.Data.TypeProviders open Microsoft.VisualStudio.TestTools.WebTesting type internal EntityConnection = SqlEntityConnection<"myConnectionString",Pluralize = true> type public UriAdjuster() = inherit WebTestPlugin() let context = EntityConnection.GetDataContext() override this.PreRequestDataBinding(sender:Object, e:PreRequestDataBindingEventArgs) = let random = new Random() let index = random.Next((context.Chickens |> Seq.length) - 1) let chicken = context.Chickens |> Seq.nth(index) e.Request.Url <- e.Request.Url.Replace("{{friendlyName}}",chicken.FriendlyName) base.PreRequestDataBinding(sender,e) ()

So I am going to the database on every request (using the awesome of type providers), pulling out a random chicken and updating the url with its friendlyName.

So that’s it.  We now have the ability to create valid uris that we can then dump into our load test.  Since our load test is running so fast, I guess we can say it is flying. So perhaps chickens can fly?

Happy holidays everyone.

3 Responses to Creating Dynamic Uris For Visual Studio Web Tests

  1. Pingback: F# Advent Calendar in English 2015 | Sergey Tihon's Blog

  2. Pingback: Dew Drop – November 30, 2015 (#2141) | Morning Dew

  3. Good post. I learn something new and challenging on websites I stumbleupon every day. It will always be helpful to read through articles from other authors and use something from their websites.

Leave a comment