How to verify number of cookies while automating a test in Test Studio

Learn more about Test Studio here

Yes these kind of questions are very simple but have a sustainable impact while automating a test. Recently I got a question that,

I want to verify number of cookies in web application while automating test. How Test Studio can help me in that

This test scenario could be very important. Test Studio surprisingly simplify these kind of requirement while automating test. So to verify number of cookies in web application,

  1. Add Coded Step
  2. Add reference of MS Unit Test
  3. Read number of cookies
  4. Assert number of cookies with expected value.

To Add Coded Step and reference of MS Unit Test follow this post

Once you have added reference of MS Unit Test and added a coded step start writing following scripts in coded step,

Read cookies of web application being in test as following. Do not forget to change URL with URL of web application you are testing.

image

Now in browserCookies all cookies are being read. You can read count of cookies as below,

image

Once you have cookies count in variable count you can verify its exact value and assert result using MS Unit Test Assert. This can be done as following

image

Where baseCountValue could be any constant value. Putting all together script of coded step would like following,

 


[CodedStep(@"New Coded Step")]

 public void WebTest_CodedStep()
 {
 int baseCountValue = 2;
 var browserCookies = ActiveBrowser.Cookies.GetCookies("URL of your App");
 var count = browserCookies.Count;
 Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(count, baseCountValue);

}

On running you can see that below test is verifying that number of cookies associated with URI is zero.

image

In this way you can verify number of cookies in Test Studio. I hope you find this post useful. Thanks for reading.

I hope you find this post useful. To speedup automated testing do not forget to download trial of Test Studio from here

image

Advertisements

How to work with MS Unit Test Assertion as step in Test Studio

Learn more about Test Studio here

More you meet people more questions you get. I love this part of my job, meeting and answering people queries. I usually talk to Test Professional who are doing manual testing. So some of their questions are very basic and this is expected because they are not coming from coding or programming background.

Recently in one of seminar I came across a question

How can I use MS Unit Test Assertion in a Test Step while automating test with Test Studio?

Answer of this questions is in three steps,

Step 1:

Add reference of Microsoft.VisualStudio.QualityTools.UnitTestFramework dll in Test Project

Step 2:

Create a coded step

Step 3:

Write Assertion in test

Add Reference

There are in two ways you can work on Test Studio. Either on Visual Studio plugin or on Test Studio standalone version.

Adding reference in Visual Studio is simpler. Right click on test project and from context menu select Add Reference. Browse to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll library and add reference in test project.

Even though you have added reference in Visual Studio make sure that you have added reference in test project opened in standalone version else you will get exception.

image

To add Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assembly in Test Studio standalone version you need to follow following steps,

Select Project option in top menu

image

Click on Show Settings in project tab

clip_image001

On clicking on Show Settings you will get Project Settings dialog box. In dialog box click on Script Options. To add assembly reference click on Add Reference.

clip_image003

When you clock on Add Reference Test Studio will open File Chooser dialog box. In that browse Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll assembly reference location in file system and select that to add as reference in current Test project.

Create a coded step

To create Coded Step in select Test and from menu

image

You will find coded step added as below,

clip_image001[6]

Write Assertion in Test

Once Coded step is added you can write any assertion. To validate I am writing a very simple assertion as below,

You can view below code in Visual Studio,


[CodedStep(@"New Coded Step")]

 public void WebTest_CodedStep()
 {
 Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual("TestStudio", "TestStudio");

}

In Test Studio standalone version codes step will look like as following,

clip_image002

Now go ahead and execute test. You should able to get test passed successfully as given below,

clip_image003

I hope you find this post useful. To speedup automated testing do not forget to download trial of Test Studio from here

clip_image004