Comments & Bad Comments

You should always strive to write your code so that it does what it looks like it does.  Have you heard of the phenomenon called the “write-only code”? This is code that made some kind of sense to whoever wrote it at some point in time, but it is difficult to understand for anyone trying to read it a later date, even if the person reading is the author. This kind of problem can be resolved by comments in the code.

Although comments can be very useful but sometimes it can also go wrong. I saw a common mistake today, which is this:

//Updating all records

Sanitiser.Update(“All”);

//Updating 20 records

Sanitiser.Update(20);

As you can see, these comments are just repeating what the code already said. This is just a waste but surprisingly, this is very common. One of the reason for this is mainly because these developers have been told that comments are good, but they have no idea as to what makes a good comment. A comment should say something that is not obvious from the code and which is likely to be useful to anyone trying to understand the code.

Another common fault is this:

//Updating 30 records

Sanitiser.Update(50);

Here, the comment contradicts the code which is not very helpful. This kind of thing usually happens when someone modified/refactored the code without updating the comment. A quick review of the comments after a code change is always worth doing.

Getting Started with Webdriver and CSharp (C#)

Tools you will need:

  1. An IDE – I’m using Visual Studio 2010 but you can use any IDE. For example Visual Express, which is free
  2. Nunit – Download it from here
  3. Webdriver C# Clent Drivers – Download them from here

 

Open Visual Studio and create a New Project (Class Library):

 

In your project references, add the references for Nunit.Framework.dll, Webdriver.dll & Webdriver.Support.dll

 

 

You are now ready to write some code. Here is what we will be doing.

  1. We will run our tests in firefox browser
  2. The test will navigate to www.yahoo.co.uk
  3. Type “selenium hq” into the search box
  4. Clink the “Search” button
  5. On the results page we will Assert on the title of the page.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace GettingStartedWithWebdriver
{
[TestFixture]
public class WebDriverDemo
{
private IWebDriver _driver;
private string _baseUrl;

[SetUp]
public void SetupTest()
{
_driver = new FirefoxDriver(); // we want our tests to run in Firefox
_baseUrl = “http://www.yahoo.co.uk”;

}

[TearDown]
public void TeardownTest()
{

_driver.Quit(); // quits webdriver and closes any associated windows
}

[Test]
public void NavigateToTrainLineHomePageAndAssertOnTitle()
{
_driver.Navigate().GoToUrl(_baseUrl);
_driver.FindElement(By.Id(“p_13838465-p”)).Clear(); // clear the text field before entering text
_driver.FindElement(By.Id(“p_13838465-p”)).SendKeys(“selenium hq”); // type into the text field
_driver.FindElement(By.Id(“search-submit”)).Click(); // click on the search button
Assert.AreEqual(“selenium hq – Yahoo! Search Results”, _driver.Title); // Assert using nunit on the window title
}

}
}

Incase you are wondering where did I get the ID of the fields from, you can use a tool like firebug in firefox to inspect elements on a broweser. See below

 

 And there you have it, your first test in Webdriver and CSharp.

Moving from Selenium RC to Webdriver

Two years ago, while working at the The Trainilne, I was part of the Automated Refunds project and the main QA/Developer for its automated test framework. The aim was to replace the existing mainly manual system and provide a transactional and fully automated refund processing system.

As this application was mainly to be used in Internet Explorer, we decided to build an automated testing framework using Selenium RC, NUNIT and C#. This testing framework was a success, with 30,000 lines of code (including coding of 120 tests) and 1650 Assertions.

My Selenium journey started way back in September 2006, while I was still at University and was doing my work placement at Volantis. The selenium RC version back then was 0.8.1 and Selenium IDE was 0.8.2. Selenium has come a long way since then and became a popular and well established testing framework that worked with a large number of browsers, allowed you to write your tests in almost any language, it was Open Source and as it was written in JavaScript, it was very easy and quick to add support for new browsers that might be released.

But it was not perfect. I took a lot of shortcuts, cut corners and did stuff in ugly/dirty ways to get things done. JavaScript being selenium’s strength was also its weakness. Browsers impose a very strict security model on any JavaScript that they execute in order to protect a user from malicious scripts. Examples of where this security model makes testing harder are when trying to upload a file (IE prevents JavaScript from changing the value of an Input file element) and when trying to navigate between domains (because of the single host origin policy problem). Selenium API has also grown over time and with that, it’s become harder to understand how to best use it. I personally found the API difficult to navigate.

To solve these problems, I said Hi to Selenium 2 also know as Webdriver! Webdriver takes a different approach to solve the same problem as Selenium. Rather than being a JavaScript application running within the browser, it uses whichever mechanism is most appropriate to control the browser. For Firefox, this means that WebDriver is implemented as an extension. For IE, WebDriver makes use of IE’s Automation controls. According to Selenium HQ:

The primary new feature is the integration of the WebDriver API. This addresses a number of limitations along with providing an alternative, and simpler, programming interface. The goal is to develop an object-oriented API that provides additional support for a larger number of browsers along with improved support for modern advanced web-app testing problems.

Let’s hope that is the case. I am starting to switch over from Selenium  RC to Webdriver. Along the way, I will be sharing my findings with you. The aim is to one day, change the beast (Automated Refunds Testing Framework) from Selenium RC to Webdriver. I will also be using Automated Refunds as my guinea pig while learning Webdriver. Watch this space!

My New Site is finally Up & Running !

My new site is finally up and running yay !

I’ve changed the look and feel, bought a new domain name and also changed my hosting company. So hopefully this site will be a alot more stable as compared to before.

I now have the boring task of migrating all previous articles/posts from the old site to the new one. I would have been happy if it was a straightforward copy & paste, but in few cases it involves modifying the posts or formatting the data so that they don’t look out of place in the new site.

Its also been a while since I wrote anything new for my site. Hopefully I’ll get time to write something soon. Watch this space 🙂