Labels

Sunday, 14 April 2013

Discovering Selenium and creating a Test Suite


Recently I have been thinking quite a lot about testing...The reason is that I have never been that confident in JUnit and I have always had testers do that side of stuff for me. Well enough is enough, Testing is one of the most important/if not the most important part of development and it's about time I learned how it is done.

Currently at work my job seems to fluctuate between web developer and back-end Java developer. Now I know that Java can use JUnit for unit testing (testing if a method returns what it's supposed to, and what not) but I wasn't sure how to test the web front end stuff.
Then I found Selenium.

On Selenium's home page the question is asked "What is Selenium?"
And the answer is given "Selenium automates browsers."
That's really enough to understand what it is they do. After reading that line I thought "This is exactly what I'm looking for".
I downloaded the Selenium RC server and started messing around with it. The RC edition is useful because it means testing can be done remotely, we can set up the server hub on one machine and create a number of nodes for other machines. This means developers can test there own work before committing it (if they choose to). if this is not for you try looking into the WebDriver edition.
Selenium supports a number of languages including JUnit 4, which is perfect for us. To generate the tests Selenium has an IDE which is a plug-in for Firefox found here. Using this simple IDE you can create scripts (much like a macro) which can be run straight from the IDE or exported into JUnit. Selenium supports a number of browsers (all the major ones) so no need to worry about that, tests can be run in each browser to check for cross platform issues.

So anyway...I decided I would write a whole new Test Suite for the web-based testing. This would save the company a lot of time and make the testing procedure more accurate and less time-consuming.
Before using Selenium all our test cases for our software where in the form of documents. For example we would have a document called "Vehicle Test" and in the document would be the basic testing procedure for adding/editing/deleting a vehicle on our system. This was all done through the web browser and would take roughly 10-15 minuets to complete, this had to be done before every release so as you can imagine, quite laborious.

So I started reading through the test documentation and converting it into JUnit using the Selenium IDE, I was surprised how simple it all was, I didn't have any issues, just click record and then run through the test they way I always had done.

Once completed I just exported the Data to JUnit 4 and slapped it into the test project I had created in my Java IDE. I did have to manually add in assertions to check that validation text was appearing on the page, for example text such as "Please Use Another Name" or "IP address Already Exists". I don't know if there is a way to do that in the IDE just yet, I'm sure there will be. There was also an issue with the FireFox IDE not picking up all the WaitForPageToLoad options, some I needed to add some of these in also.

I would say when working with selenium, don't rely to much on the Firefox IDE Plug-in, just use it as a rough guide to get you started. You will realise you start to learn a lot more about JUnit as you go and eventually you wont use the Plug-in IDE at all.

I will update this post with more info as I learn more about browser automation.

Google Maps JavaScript API V2 to V3

Currently (at work) I have been given the task of upgrading our Google maps API from version 2 to version 3. This is because the JavaScript API that we use is becoming deprecated as of May 19th 2013, as seen here.

Now I know you're thinking, why as a Java Developer would I be given a web development task? Well the reason is that I currently work in a very small company which has a "lets all muck in" attitude, this suits me just fine as it's good to get a bit of experience in everything.

The company I work for provides real time traffic information so we use the Google maps to pin-point locations.
Upgrading from version 2 to version 3 wasn't really that hard. The first thing to do is make sure you're using the correct JavaScript file:

http://maps.google.com/maps/api/js?v=3&sensor=set_to_true_or_false&key=your_api_key

As you can see they have added a "sensor" parameter to the request. From what I understand the sensor parameter is set to true if you have a GPS sensor,this is used to determine the user's location. (So I'm guessing this is more for mobile applications). The things to remember are the following;


Drop the G's

Google seems to have replaced the old object 'GMap2' for an object referenced by a namespace 'google.maps.Map'. Most of the objects seems to be the namespace followed by the original object minus the 'G' for eaxmple:
  • 'GMarker' becomes 'google.maps.Marker'
  • 'GEvent' becomes 'google.maps.Event'
    You get the idea..

    Now I'm not a JavaScript guru but I would think the reason for doing this is to stop the overuse of global variables. I've found that using global variables in JavaScript can cause some pretty nasty problems, namespaces seem to offer an almost object orientated approach which I think would work a lot better.

    Check for deprecated Objects

    Just renaming a few things isn't going to work...
    Version 2 has quite a few objects which are no longer supported in version 3. For example if you wanted to use custom overlay types (as we do at work) then instead of inheriting from GOverlay version 3's custom overlays are inherited from OverlayView.

    EDIT
    When you update an API you should document all the changes you have made, kind of a no brainer really, although Google seems to have missed a few things. In version 2 of the API we have access to the 'getBoundsZoomLevel' object. This object does exactly what it says and it's quite useful when searching for specific icons on the map. Version 3 does not have this option and annoyingly they don't provide a way around this when upgrading from version 2. I have written a function which basically does what the old object used to, it seems to me that Google should really have provided a way them selves rather then not even mentioning it. Maybe somewhere on-line they do mention it, if you find anything please let me know...until then, the blood runs hot.

    There's quite a few little irritating things like this knocking about. For a slightly more detailed explanation try looking at the Removing Obsolete Code section of the API documentation.