Saturday, February 26, 2011

Review of - The Selenium Testing Tools - Beginner's Guide

As mentioned in one of previous post, I got an opportunity to review a book on Selenium call - The Selenium Testing Tools - Beginner's Guide from PACKT Publishing.

Selenium 1.0 Testing Tools: Beginner’s Guide

I had used Selenium in lot of my projects, I thought, I knew selenium quiet well before reading the book.  After reading the book, I realized that, I had only partial knowledge about Selenium.  I knew how to do things, but the book taught me, how to do them in many different ways and what was the best way in a given situation.

The book, taught me lot of new things about Selenium.  For example, I always thought that, using Selenium IDE, one cannot write maintainable tests, but after reading the book, I realized that if one follow the patterns mentioned in the book, one can easily write maintainable tests simply using Selenium IDE.

Moreover, non-programmers can convert the Selenium IDE tests to programming API tests in a matter of few clicks!

There are may good things that, I could take away from the book, here are a few that stood out:

  • Starts of from the very basics.  The book start with installation of Selenium IDE, Firebug and Firefinder add-ons to Firefox.  Because of this, someone who has never worked with selenium before, can quick start and write their first selenium tests in a matter for minutes
  • While reading some books, I get lost and have to figure out how to do a certain step.  But things are different with this book, there are screenshots for almost every thing.  For every step that book shows there is a Screenshots.  This helps us understand the concepts in a much better way.
  • Book encourages the use of best practices.  For example, book clearly explains why the test cases should be independent of each other and how one could achieve that.  Book encourages the use of Firebug and Firefinder.  These are two tools which every web-develop/web-QA should know for sure!
  • Explains how one can convert Selenium IDE test to programming tests using just the Selenium IDE.  I did try it and it does work!
  • Book Explains how we could use Selenium Grid to parallelize our tests.  Using Selenium Grid we could target our tests to run on a certain browser on a certain OS.  For example, we could configure that Test1 should run on "Firefox on Mac" or "IE on Windows".  Using this feature you can test your application on lot of platforms and browsers without a lot of pain!
  • Demonstrates advanced techniques like Cookie handling using Selenium API
  • A full chapter dedicated to Selenium 2
  • At various steps, the book presents the user with Popup Quizzes, these are helpful to test what we have learned!
  • Explains the Page Pattern.  While writing the Selenium tests using the Programming API, its extremely important that we do not repeat code for every test.  Book explains how we could achieve maximum reuse of code.  End result, more maintainable and easy to write/read tests!
  • Explains various techniques like xpath, regexp, css and javascript to locate an element.  Its extremely necessary that we learn how to locate the element efficiently.  Using the right technique at the right place could result in faster and easy to read tests.  For example using xpath at a place where css could have been used would result in slower tests!
  • Explains the use of user extensions.
Those are some of the take away's for me from the Book.  

The book has much more to offer than, what I have described here.  Take my advice, do not waste any time and grab a copy of the book from here!

Saturday, February 19, 2011

Android - Fixing the no internet connection issue on emulator

Recently, while developing an Android Application, I was faced with a situation that wasted 2 hours of my time.  The issue was simple, my app was not able to access internet from the Android Emulator.

Initially I thought fixing the issue should have been straight forward, but life is not always that simple.

So what was the real issue because of which my app was not able to access internet?  There are multiple reasons because of which this issue could occur.  Hence, I decided to document my findings so that other could benefit from it.

There are two main symptoms of no internet connection on android emulator
  • Only your app is not able to access the internet
  • None of the apps are able to access the internet
Lets look at what are the reasons behind each of them.

Only your app is not able to access the internet

If only your app is not able to access the internet on the emulator, check if any other apps are able to access the internet or not.  For e.g. you could open up the browser application, visit http://news.google.com/.  If the page opens up correctly then problem lies in your app itself and its simple to fix.

Basically, your app needs the Permission to access the internet.  This can be done by adding the following line just before the end of <manifest> tag in AndroidManifest.xml file of your application.

A sample AndroidManifest.xml file with the added permission would look like

Compile and re-install the app in the emulator and try to access the internet from your app. It should work!

What is the use of uses-permission tag:


Android application can request certain permissions so that they can function properly.  Some examples of permissions are, get users location, make a call, access the internet etc. App has to explicitly specify this in the AndroidManifest.xml.

When end users install such an app on their device, the android OS will notify the user that, app is requesting certain permissions.  If users are fine with that, then only the app will be installed.  Else users can deny the permission and the app will not be installed.

This mechanism is Androids way of implementing security and users privacy!

None of the apps are able to access the internet

Now this situation is tricky.  There are two reasons because of which this could happen
  • Proxy server is not configured on the emulator
  • Incorrect DNS used by the emulator


Configuring the proxy server on the emulator:

If you access internet via a proxy server, then you will need to configure the proxy server on the emulator as well.  This is done via the following steps.

Open up the Settings application on the Emulator 
Settings

Select the Wireless & Network settings
Wireless and Networks
Select the Mobile networks 
Mobile Networks
Select the Access Point Names
Access Point Names
Select the Access Point listed there.  In my case its Telkila
Select the access point
In the Edit access point section edit the value of Proxy and Port.  These should point to your Proxy server and Port that you use.
Set the proxy and port values
Thats it!  After doing these settings try to access the internet from the browser application on the emulator.  It should work!

If you are not behind a proxy (like me) then the problem is because of an incorrect dns server.  Lets look at how to fix the dns issue.

Incorrect DNS Used by the Emulator:

In my case none of the above things fixed the issue.  The real issue was that the DNS used by the Emulator was incorrect.  Fortunately there is a way in which we can specify which DNS server to use.  

Android Emulator takes a command line parameter -dns-server using which you can specify which DNS server to use.  The sample command to specify DNS server would look like

-dns-server takes a comma separated list of IP address to use as DNS server.

That did the trick for me!  I was finally able to access internet from my app on the android emulator!

Monday, February 7, 2011

Learning good things about Selenium - The Functional Testing Tool

Recently, I have been learning a lot of good things about Selenium. All thanks to this book, Selenium 1.0 Testing Tools - Beginner's Guide.

Selenium 1.0 Testing Tools: Beginner’s Guide

I got an opportunity to review this book from PACKT Publishing. I have just started reading the book, but I can tell you one thing, it gives great start at working with Selenium.

What is Selenium?

Selenium is an opensource automated testing tool for testing web application.  Using Selenium we can write test cases to test the web application in a black box environment.

Quoting from the book:

Test automation has grown in popularity over the years because teams do not have the time or money to invest in large test teams to make sure that applications work as they are expected to. Developers want to make sure that the code they have created works as they expect it to.


As time has passed developers and QA's have realized that automated functional tests provide a great safety net for catching bugs early in development life-cycle.  Selenium makes writing/executing functional tests extremely easily.

We can record and playback the tests directly in firefox using the Slenium IDE!  Selenium also provides programming api to write functional tests in Java, C#, Ruby , PHP, Perl and Python.

Once the tests are written (using the programming API or Selenium IDE) they can be execute as often as one wants using the Selenium RC (remote control) or Selenium Grid!  Over and above it the test can be executed in variety of browsers including IE, Firefox, Opera and Safari!

Selenium can be used to simulate any work flow.  I have been in situations where, because the team had written Selenium tests, a bug was caught early in the development life-cycle, which would otherwise be caught very late.  End result lot of time and money was saved because of Selenium!

The things I am blabbering (about Selenium) in this post are covered in much greater detail in the book.  Simply by reading the first few pages I was able to write a functional test using Selenium IDE.

Some of the good things I have noticed about the book so far:
  • Starts from basics.  The book starts of by guiding users through the installation process of Selenium IDE.  Goes on to explain how to write a simple functional test (using Selenium IDE), with an example.
  • Easy to read.  Its very important that books are written in simple language so that everyone can read it easily.  People with/without programming background can read this book with equal ease and extract useful information out of it.
  • All concepts are explained using examples and lots of screen shots.
These are just initial impressions about the book.  I am sure, I will discover many more good things about the book, as I continue reading further!

Stay tuned folks!  Full review of the book coming up very soon!

Wednesday, February 2, 2011

The deadly Deadlock - Fixing the issue with java.util.Calendar and java.util.TimeZone

All computer engineers learn about a condition called Deadlock in college.

What is deadlock?

This is what Wikipedia has to say about Deadlock:

"A deadlock is a situation where in two or more competing actions are each waiting for the other to finish, and thus neither ever does.  In computer science, Coffman deadlock refers to a specific condition when two or more processes are each waiting for each other to release a resource, or more than two processes are waiting for resources in a circular chain"

The article gives quiet some information about deadlock.  No! don't worry I am not going to explain all that.  There are lots of resources that will explain what is deadlock and why it occurs in a much better way.

We have all learned about the deadlock and know why does it occurs.  But have we ever faced a deadlock situation in our code?

The technology, enterprise stack, databases, we use have matured so much, that we hardly ever face a deadlock situation in production code.  But, I was fortunate enough to face a deadlock situation in one of my recent projects.

Of course the deadlock I faced happened when the app went live i.e. in production environment.  

I don't know about others, but its not a good situation to be in.  Since the deadlock was only detected in production environment, situation becomes very critical.  Numerous mails are exchanged (some of them ugly), everyone wants to know what is causing the deadlock and Me (poor soul) had no idea why it occurred :)

Moving on, how did we know that there was a deadlock?

Symptoms of deadlock
  • Performance of the app degrades quite a bit
  • CPU usages shoots up to 100% and stays there!
  • Users complain that the site is not responding and they are not able to do a certain action.
  • To get things back to normal servers have to be restarted!
When we noticed these symptoms in production for the first time, we thought it was an one off case, we restarted the server and decided we would try and find a reason for this performance problem.

I tried to find all possible causes of this performance issue but without any luck.  All possible causes were ruled out, because we were using all possible best practices, we were using a standard J2EE stack, spring, hibernate, lazy loading, optimized queries etc.  

I was not read to believe myself but, I suspected that the app might be a victim of deadlock!  

Our app was deployed in a Weblogic cluster.  We had configured Weblogic to throw an exception when it detects that a thread is running for more than 10 min (We had not long running background processes which would take more than 600 sec to finish).  And bang!  After a few days, Weblogic did throw an exception stating that a certain thread was running for 666 sec which is more than the threshold of 600 sec!

Following is the part of stack trace

Looking at the stack trace, I was a bit surprised, I was expecting a deadlock situation, when app tries to interact with the database. Thats what my impression about deadlock was! It occurs mostly when we interact with the database.

But, I was proved wrong by my own code!  In our app the deadlock occurred in memory!

We nailed it down to a function called DateRange.countNumberOfFullDays.  As the name suggests this functions calculates the number of full days between two dates!

The code was not complex at all.  And this was the least expected place where a deadlock could occur.

As you can see the actual code of DateRange.countNumberOfFullDays does not do anything special!  Then what was causing the deadlock?

Googling a little, I found that there are various issues with the Java Calendar and TimeZone classes in Java 1.5.  Visit http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6199320 and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6232022 for more information.

These bugs were fixed in Java 1.6 but, unfortunately our production servers use Java 1.5.  Damn!  Why why why, production server always lag behind the pace at which technology evolves.

The solution:

There is an excellent open source library which provides a parallel and better implementation of Calender and TimeZone api's.  Its called Joda Time

I used this library, changed the code of countNumberOfFullDays to look like

As we can see in this simple example, Joda time provides a very simple and intuitive Date and Calendar api.

Since I implemented this change (on 04/01/2011) we have not faced a deadlock situation yet! Fingers crossed!

OK, enough about the deadlock situation that I faced.  I want to know from you guys, have you ever faced a deadlock situation in production code?  If yes then, what was the situation and how did you fix it?
Have some Fun!