Agile Testing, Cookies, Functional Testing, Services Testing, System Testing, Testing

Cookies are not that complex to test with Selenium..How??lets have a look!

If we talk in simple terms Cookies are some thing which parents always teach their child ‘Not to have’.
And same applies to some web sites .They can make life tougher of user when cookies are hiding in the web pages and no one can understand ‘what the heck is making my web page to react so weird!!!’

Let me first start why cookies are required and why/ when are not required. There are several types of cookies (of course not chocolate , strawberry , butter ,etc ) which can comes and lives on the web.
For an example, if I want to log into a system and want to stay in the system for ever until I explicitly log out from it , then to maintain this session go live I always requires some sort of cookies which will make this happen for me. This you can count as an advantage , with out a doubt !
But what about this , when I am working on a web page and trying to search for some one and it will navigate me to some stranger’s profile which has no connection with the name i searched for . It is all about the game which cookies does for us 🙂


Without a doubt such behaviour will cause multiple issues when we talk in terms of automation.
Lets talk more in terms of selenium , how we can get rid of extra unwanted cookies from web page which are not required at all while executing test cases.
Ofcourse there are some manual actions which we can do to remove cookies from browser but yeah its a tedious task and then it will not add any value to our test automation framework.
We can start making a new maven project (following the concepts of Page Object model(POM)) and make a class in POM format.

Now make a class which can test cookies in the application you are testing, or any sample application like
Now all you need to do is some basic set up require to make your application interact with selenium server.
You will require Junit to write test cases and class as a template to write test cases.
Start with instantiating the driver:

public static void startChrome() {
driver = new FirefoxDriver();
public static void closeFireFox() {

Now you need to open the application such that it will navigate user to the required URL. For this you need a method :

public void openTestURL() {
driver.navigate().to(testURL); // navigate to URl which is required.

And then what you need to do is to wait for page to get load and then delete all cookies which will come on run time on this web page

wait = new WebDriverWait(driver, 10); //Implicit wait appiled to driver
driver.manage().deleteAllCookies();// delete all cookies which is in browser for fresh start

Thats not all , what if the page has more cookies ?
For this you need to make a set of all such cookies and remove them like this :

public void searchAndCheckForCookies() {

queryBox = driver.findElement(By.cssSelector("input[title='Search']"));
queryButton = driver.findElement("btnG"));
queryBox.sendKeys("monkey buns");;

Set cookies = driver.manage().getCookies();
for (Cookie aCookie : cookies) {
if (aCookie.getName().contentEquals(
"seleniumSimplifiedSearchNumVisits")) {
assertEquals("should be my first visit", "1",
if (aCookie.getName().contentEquals(
"seleniumSimplifiedSearchLastSearch")) {
assertEquals("should equal 'monkey buns'", "monkey buns",

And if you want to remove any specific cookie/s from the web page then you can use this method :

public void getOneSpecificCookie() {
queryBox.sendKeys("monkey buns");;
queryBox.sendKeys("testing 123");;

Cookie cookie = driver.manage().getCookieNamed(
// %20 is a space in the raw data that is returned
assertEquals("should equal 'testing 123'", "testing%20123",


1 thought on “Cookies are not that complex to test with Selenium..How??lets have a look!”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s