Better structured Selenium Tests

Posted on May 28, 2007
Selenium is a fantastic tool developed by ThoughtWorks for automated web acceptance tests.

If you're a web developer and you've never used this tool, go check it out immediately!

How I've been generally Selenium is by using the Selenium IDE Firefox Plugin to record my tests - and then copying the automatically generated C# into my test classes.

This is a really quick and easy approach, however the test code get's REALLY ugly REALLY fast.

Then I saw Erik Doernenburg's excellent presentation on Selenium best practices, and heard a great tip that's so simple and elegant I felt stupid for not thinking about it before.
Create a class for each of your web pages and use static methods to wrap the Selenium code. This allows you to create nice, human readable tests that are also far less brittle.
MainPage.cs (C#)
public class MainPage
{
    public static void Open()
    {
      Selenium.Open(@"/");
    }

    public static void SearchFor(string searchString)
    {
      // Ugly brittle code nicely wrapped up
      Selenium.Type("txtSearch", searchString);
      Selenium.Click("btnSubmit");
      Selenium.WaitForPageToOpen("30000")
    }
   
    public static int NumberOfSearchResults()
    {
      // This function uses regex to return the number of results on the page
      ..
    }
}
And then in your unit test class:
MainPageUnitTests.cs (C#)
   [Test]
   public void TestForSearch()
   {
     // Nice readable test logic
     MainPage.Open();
     MainPage.SearchFor("bananas");
     Assert.AreEqual(1, MainPage.NumberOfSearchResults(), "Wrong number of results");
   }

Batch Insert of Records in .Net

Posted on May 27, 2007
So, I've got a DataTable behind the scenes practically packed with data (1000s of rows) - and I want to insert this into a table on sql server using a stored procedure. The wrong way:
//The dsSourceDataSet is a populated dataset with1 table with tonnes of rows
foreach (DataRow row in dsSourceDataSet[0].Rows)
{
  SqlCommand command = new SqlCommand("sp_my_proc", connection)
  command.Parameters.AddWithValue("@param1", row["field1"].Value);
  command.Parameters.AddWithValue("@param2", row["field2"].Value);
  //Yada yada
  command.ExecuteNonQuery();
}
I mean, let's face it - this works so I thought "Hey, that's good enough for me!" But man was it sloooow! Then my boss showed me the CORRECT way of doing things: The right way:
SqlCommand command = new SqlCommand("sp_my_proc", oConnSource);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
SqlParameter param1 = dataAdapter.InsertCommand.Parameters.Add("@param1", SqlDbType.VarChar);
param1 .SourceColumn = "field1";
param1 .SourceVersion = DataRowVersion.Original;
dataAdapter.Update(dsSourceDataSet);
So what's happening? Instead of looping through the table like a crazy gooloot - everything gets crammed into the dataset and fired off to the database! And it is SOOO much faster! This process took a little over an hour before, now it takes a few minutes! Hooray!

Automatically load the Asp.net Web Server

Posted on May 27, 2007
When I'm debugging my Asp.net applications, I find myself just wanting a quick and easy way of starting the asp.net web server. You can do this from the command line like this:
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\WebDev.WebServer.exe" /path:[PATH OF YOUR WEB APP] /port:[WEB PORT]  /vpath:[/mywebapp]

You can also use the funky explorer plug-in designed by Robert McClaws here. This lets you run the webserver just by right clicking on a folder path.