Monday, April 28, 2014

Test 'me' ..err cookies are not that complex to test with Selenium.

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) and make a class in such format:

Now make a class which can test cookies in the application you are testing, or any sample application like http://www.compendiumdev.co.uk/selenium/search.php.
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:
@BeforeClass
public static void startChrome() {
driver = new FirefoxDriver();
}
 
@AfterClass
public static void closeFireFox() {
driver.quit();
}
Now you need to open the application such that it will navigate user to the required URL. For this you need a method :
@Before
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 :
@Test
public void searchAndCheckForCookies() {
queryBox = driver.findElement(By.cssSelector("input[title='Search']"));
queryButton = driver.findElement(By.name("btnG"));
queryBox.clear();
queryBox.sendKeys("monkey buns");
queryButton.click();

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

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

@Test
public void getOneSpecificCookie() {
queryBox.clear();
queryBox.sendKeys("monkey buns");
queryButton.click();
refreshPageObjects();
queryBox.clear();
queryBox.sendKeys("testing 123");
queryButton.click();

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


No comments:

Post a Comment