Friday, April 24, 2009

Short Rant on StringBuilder and String

I’ve been running into lots of code which seems to be using Strings to build up HTML or Error Messages.  Unfortunately there is lots of inconsistency in the way it’s been done, but for anyone interested here is my take on it.  First off let me say any hard coding HTML into a code behind is generally a bad idea (don’t get me wrong I’ve done it myself lots of time) so try to avoid it at much as possible.  If however you are unable to avoid it, please review the examples below and try to keep to using the “Good” technique.

The Good

Here is a reasonably good example of code for building HTML in a code behind if you can’t avoid it.  Using StringBuilder is faster and better suited for concatenation than the primitive String type.  Also using the AppendFormat function allows you to use the {#} place mark which ensures your value goes into the correct location, making the string far more readable and maintainable.

StringBuilder sbTemp = new StringBuilder();
sbTemp.AppendFormat("<a href=\”mailto:{0}\”>{1}</a>",
                                                          ConfigurationManager.AppSettings["IT_EMAIL"],
                                                          ConfigurationManager.AppSettings["IT_EMAIL"]);
litITEmail.Text = sbTemp.ToString();

 

The Bad

Here is a bad example of the same code using String Builder.  although it will accomplish the same goal as above it requires more code and is much more difficult to read or maintain.
sbTemp.Append("<a href=\"mailto:");
sbTemp.Append(ConfigurationManager.AppSettings["IT_EMAIL"]);
sbTemp.Append("\">");
sbTemp.Append(ConfigurationManager.AppSettings["IT_EMAIL"]);
sbTemp.Append("</a>");
litITEmail.Text = sbTemp.ToString();

 

The Down Right Ugly (and wrong on so many levels)

Here is the wrong way to do it and anyone doing this should be taken out and shot.
string s = "<a href=\"mailto:";
s += ConfigurationManager.AppSettings["IT_EMAIL"];
s += "\">";
s += ConfigurationManager.AppSettings["IT_EMAIL"];
s += "</a>";
litITEmail.Text = s;

Saturday, April 4, 2009

Crazy Open Source project!

I found out about this project the other day, ReactOS (http://www.reactos.org/en/index.html ) which is an open source version of Windows!  Apparently it will “eventually” replace MS Windows on the Users desktop without Users ever noticing the change. Oh my God!, is this a law suit waiting to happen or not?

It’s only in Alpha at the moment, but apparently it works well……

Friday, April 3, 2009

Web Testing Applications

One of the great things about the unit testing is that it breaks down tasks into small meaningful chunks; sadly testing web brings up a whole set of new issues.  With web applications the testing is usually based on a script or number of steps which could be spread over any number of different screens.  I’ve used a number of different tools to do this an all have issues and limitations and they all cost heaps of cash.  then I’ve discovered Selenium a free open source tool for doing just that.

Installing Selenium on your development machine

Selenium is a java based application so you need to install the latest Java Runtime which can be downloaded from the Sun website.  Run the installer taking all the standard defaults and this should place everything needed into your C:\Program Files\Java\ folder.

Next you need to download a copy of Selenium RC (selenium-remote-control-1.0-beta-2-dist.zip) from the application share or from the Selenium website to your drive. Open this file using Window Explorer or any Zip file reader and extract only the \selenium-remote-control-1.0-beta-2\selenium-server-1.0-beta-2 folder.

image

Extract these file to a folder d:\applications\selenium-server.

To start the service running you need to go to the command prompt and typing:

    d:
    cd d:\Applications\selenium-server\
    java –jar selenium-server.jar

image

You should start to see the output above showing that you now have a server running on port 4444.

Installing the Selenium Extension

Open the Fire Fox browser and select tools/Add-ons from the menu.

image

Select Get Add-Ons from the menu and type Selenium into the search text box and hit return. You should see Selenium IDE appear as the first item in the list. Select this and click the “Add to Firefox” button.  After installation restart Firefox.

image

There should now be a new item added to your Tools menu called Selenium IDE. 

Recording a test

While on the webpage select Selenium IDE from the Tools menu in Firefox.

image

A dialogue box should appear and you can notice that the red circle in the top right hand side indicates that you are now in recording mode.

On the page dome some work, in this case I placed the word “Fred” into the search field and clicked Search.

image

Select the “Page” text (or any text for that matter) on the page and right click.  You should see a submenu stating “Show All Available Commands” and within this “AssertTextPresent Page”.  Assert is the test in this case, you are testing that “Page” text will exist following the search.

image

Going back to your Selenium IDE should present you with 4 commands, one for each of the steps you’ve taken.  Click the red button in to top right of the screen to stop recording your steps and Save by click on File/Save from the menu.

Running the test

Once you have a saved test, you can run it at any time in the future by opening if from disk using File/Open from the menu.

image

Click the green play button on the tool bar to run your test, ensuring that the Base URL is correct.

image

If all the steps turn green you have successfully run the tests.

Adding the Selenium to Integration Server

The great thing about Selenium is that you can build each of these tests into the continuous integration server. 

image

From the options menu select Format/C# – Selenium RC.

image

This will present your test in C# code which can be cut/paste into the standard UnitTest infrastructure.