Category Archives: VisualStudio

How to parameterize tokens in URL for web service requests in VisualStudio webtest

While back I had blogged about automating testing of your REST API using VisualStudio webtest and one of the questions I got was how do you parameterize tokens in URL for REST API endpoints. If you have not seen that post see this link. Let’s take a look at our northwind API as an example.

If you want to retrieve a single customer entity and the REST API endpoint looks something like this http://api.northwind.com/customers/{customerID}

By default if you add a web service request to your webtest you will need to hardcode the “customerID” portion in the above API endpoint. This may not be necessarily bad if those IDs never change additionally if you have multiple web service requests added in your webtests for different scenarios, if you hard code the ID in URL and when the IDs change you will have to make sure you go and update all the URLs for each web service requests in your webtest which can quickly become a nightmare. So lets see how we can solve that problem.

Approach 1:

Add a context parameter to your webtest called “customerIDParam” and set the value to a customer ID that you will use as test data for the webtest. Follow steps to add context parameter.

Assuming the webtest is open in VisualStudio, right click on context parameters node and select “add context parameter”

SNAGHTML2d57df3

enter name you want to use for the parameter and for value enter the customer ID you will use as test data.

Once the context parameter is setup, you can now use this in the URL for the web service request as shown below. In the highlighted request you can see the API host portion and customer ID portion using context parameters.

SNAGHTML2db43cb

Approach 2:

This approach we are going to use some custom code to get the parameters for the webtest. First thing we’ll need to do is add a data source to the webtest project, datasource will contain the the test data that we want to use, in this case api host and customer ID values. Next thing we’ll need to do is convert the webtest into a coded webtest. One thing to keep in mind here is that the VisualStudio designer doesn’t allow binding URLs to datasource, it would have been nice if we could do that as that would eliminate having to write custom code.

To convert the webtest into a coded webtest, assuming you’ve opened the webtest inside of VisualStudio, click on “Generate Code” toolbar option as shown in screen capture below

SNAGHTML2e3c9fc

You could also just simply add a class and inherit from “WebTest” class defined in “Microsoft.VisualStudio.TestTools.WebTesting” namespace. I’m not sure why there is no option to added coded web test within VisualStudio that would automatically do this.

Assuming you have the coded webtest class added to the project we can get apihost and customer id values from the datasource by adding below code to the coded webtest and use it when we construct the “WebTestRequest”

   this.Context["CustomerTestDS.CustomerTestData.ApiHost"].ToString()
   this.Context["CustomerTestDS.CustomerTestData.CustomerID"].ToString()

Hope that helps

</Ram>

Error connecting to Azure Subscription from VisualStudio

I wanted to make this post with the hope of saving others time. Last two days I have been troubleshooting issue connecting to my Azure Dev/Test subscription. I had developed a custom resource group template and was getting ready to test the template by doing a sample deployment to my azure subscription. Unfortunately I simply could not get past the sign-in in “deploy to resource group” dialog in Visual Studio 2015. Since I had multiple subscriptions on my laptop as most of us do when I pick the Microsoft account from drop down in this case “rprakashg@hotmail.com” I would get the visual studio sign in page where I enter “rprakashg@hotmail.com”, STS would then redirect me to live and I’m able to login there and would come back to the “deploy to resource group” dialog but all the fields remained read only. I had no idea why the fields were still read only. Unfortunately what had happened was my sign-in process had failed (not that I used invalid login or anything), it would have been helpful if VisualStudio had shown some error message to me, additionally there was nothing visually in the dialog that would have given me some indication that there was some problem with sign in, as you can see from the screen shot below.

image

So I popped back to Visual Studio 2013 and opened the same resource group template project and tried to deploy and here is what I found. In the deploy to resource group dialog looks like below in VS 2013, when I tried to sign in to my azure Dev/Test subscription I basically got the same behavior as VS 2015 but the difference is I could visually see that I was not signed in even though I successfully signed in to live. If the sign in was successful the button text changes to Sign out and all the fields become enabled.

image

Either way there should be some sort of error message shown to user when there is a problem with sign in. At this point I needed to find out what is really happening so I tried creating a new ASP.NET project in Visual Studio 2015 and selected “Host in the Cloud” Project was successfully created but no cloud resources were provisioned in my azure subscription and there was no error messages as well, VisualStudio 2015 had decided to fail gracefully. I attempted to do the same thing in Visual Studio 2013 and right after click ok in project creation dialog, got the dialog shown below

image 

So I clicked on “Sign In” in the above dialog signed in using my rprakashg@hotmail.com which is associated with my Azure Dev/Test subscription and ended up getting error below from Visual Studio 2013 after the sign in.

image

At this point I ‘m still not sure exactly what the heck is going on. I can successfully login to both Azure Portal (old and new) using my rprakashg@hotmail.com but Visual Studio was still crapping all over the place. What is up with the error messages VisualStudio? What ever happened to good user experience Smile 

So as a last resort I tried connecting to my Azure Subscription from Server explorer and got following error

image

Error above got me confused even more, other thing I noticed was in VS 2015 under account settings page it was showing one account for rprakashg@hotmail.com and another one for ram.gopinathan@marviewsolutions.com and both as Microsoft account. That did not make any sense to me. So I switched to PowerShell and ran Get-AzureAccount and surely there was two accounts, see below

image

What was interesting here was the second user had no subscription associated, it was just associated with a different tenant, keep in mind ram.gopinathan@marviewsolutions.com is not even an organization account, its just my business email hosted in google apps for business. What I noticed is that visual studio was adding this account when I was signing in using my rprakashg@hotmail.com. Really bizarre stuff. At this point I knew something was up with my rprakashg@hotmail.com live account, to validate I added another live account rprakashg@outlook.com to my existing dev/test azure subscription as co administrator and attempted to sign in using this account instead of the rprakashg@hotmail.com and everything worked as expected, this really confirmed my assumption.

I focused my full attention on trying to figure out what could be wrong with rprakashg@hotmail.com account, checked visual studio service settings, Azure AD side, couldn’t really figure out what could be the root cause. As a last resort I started go back like weeks and try to remember all the changes I might have done from my memory any thing that could have potentially caused this. Suddenly it dawned on me, my client had granted access to their office 365 SharePoint site to my business email ram.gopinathan@marviewsolutions.com and once I received the invitation email I clicked on the link and logged into the site using my rprakashg@hotmail.com. Now my Microsoft account rprakashg@hotmail.com account is linked to my clients azure ad tenant for office 365 under ram.gopinathan@marviewsolutions.com email. I’m not 100% clear what is happening under the hood after you sign in to azure subscription from visual studio, but my guess is there is multiple bugs in that code per the behavior I’m seeing.

Basically after that point it completely broke the connect to Azure functionality from VisualStudio. I was able to successfully repro this using another live account that was working before. Steps to reproduce this issue are quite straight forward, see below.

  1. Login to an office 365 SharePoint site using administrator account and share site to an external email address, it can be anything, assuming you can check the email.
  2. Once you receive the invite email click on the link in email to access the SharePoint site (Make sure you clear cache and that there are no cookies left from previous logins). This will bring up a realm selection page where you will see two options Microsoft account and Organization account. Depending on how you login to your azure subscription, if you use a Microsoft account then you choose Microsoft account otherwise you’ll select Organization account and login.
  3. After you are successfully logged in, open VisualStudio, (2013 or 2015) and try to connect to azure subscription using the same account that you used to login to Office 365 SharePoint site. You can try connecting via server explorer, create a asp.net project and select host in cloud option, deploy resource group etc. nothing will work.

Once I removed the account from Azure AD tenant for Office 365 SharePoint site, everything will start working again. Hopefully this saves some time for others, been pulling my hair on this for last two days.

Cheers,

</Ram>