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

Advertisement