Running CasperJs scripts from a C# console app

I recently came across the need to write a console application that can take a CasperJs script (or folder) and run it with console output on. I wanted to make this tool in order to integrate it with Visual Studio External tools, so that whenever I am editing a casperJs script, I can hit my keyboard shortcut to verify that all is ok.

Doing this in C# is mostly straightforward:

  • You recieve the CasperJs file or folder to run as an argument to your console app
  • Inspect the environment variables to find:
    • Where Python is installed (by inspecting your path variable)
    • Where CasperJs is installed
  • You start a Process.Process() and provide it with enough details to run the script

 

Challenge: Output

The tricky bit, is to grab the output, and display it in your own app. Here’s my go at it:

private static void ExecutePythonScript(DirectoryInfo workingDir, FileInfo pythonPath, 
string casperArguments)
{
    var p                              = new Process();
    p.StartInfo.WorkingDirectory       = workingDir.FullName;
    p.StartInfo.FileName               = pythonPath.FullName;
    p.StartInfo.Arguments              = casperArguments;
    p.StartInfo.UseShellExecute        = false;
    p.StartInfo.CreateNoWindow           = true;
    p.StartInfo.RedirectStandardError  = true;
    p.StartInfo.RedirectStandardInput  = true;
    p.StartInfo.RedirectStandardOutput = true;

    p.ErrorDataReceived += (s, e) => {
        if (!string.IsNullOrEmpty(e.Data))
            Console.WriteLine("e> " + e.Data);
    };

    p.OutputDataReceived += (s, e) => {
        if (!string.IsNullOrEmpty(e.Data))
            Console.WriteLine("->" + e.Data);
    };

    p.Start();
        p.BeginOutputReadLine();
        p.BeginErrorReadLine();
        p.WaitForExit();
    p.Close();
}

 

The tricky bit to figure out was that after you start the processs (p.Start()), you need to call the asynchronous methods BeginOutputReadLine() and BeginErrorReadLine(), otherwise, events will never be posted. Sarching the internet for invoking processes did not include this little gem, hence this post.

Happy coding!

Jeg bare tøyser

A good basis for unit testing

Over time, one developers techniques and habits that “just work”. In this post, I would like to share with you a set of base classes that I’ve been using and refining over time. They make my Unit Test writing a little more efficient and easy on the eyes.

The base of the base

I often start a new software project by setting up some form of base unit test class with functionality that I know I will be using, such as using embedded resource documents, various kinds of verifiers, test names etc. I usually name this class “BaseUnitTest”. This blog post will not be focussing on this base of bases, but to give you an idea, this is roughly how it starts out.

public class BaseUnitTest
{
    private Assembly _testAssembly;
    public EmbeddedResourceManger ResourceManager { get; internal set; }
    public IoVerificationManager IoVerifier { get; internal set; }

    public BaseUnitTest ()
    {
        _testAssembly = Assembly.GetCallingAssembly();
        ResourceManager = new EmbeddedResourceManger(_testAssembly);

        IoVerifier = new IoVerificationManager();
    }
}

I usually add on methods to this class in the form of Verfiers of different sorts. A handy base class for your Unit  Tests is not the scope of this blog though…

Wrapping AutoMocking and Instance creation with BasedOn<T>

Now follows the real nugget. A base class for your unit tests that uses StuctureMap’s AutoMocker to construct a nice, testable innstance of the class under test.

[TestClass]
public abstract class BasedOn<TInstance> : BaseUnitTest where TInstance : class
{
    public MoqAutoMocker<TInstance> AutoMocker { get; internal set; }
    public TInstance Instance { get; internal set; }

    [TestInitialize]
    public void RunBeforeEachTest() 
    {
        AutoMocker = new MoqAutoMocker<TInstance>();

        OverrideMocks();

        Instance = AutoMocker.ClassUnderTest;

        ExecuteBeforeEachTest();
    }

    public Mock<TInterface> GetMockFor<TInterface>() where TInterface : class
    {
        return Mock.Get(AutoMocker.Get<TInterface>());
    }

    /// <summary>
    /// Use this from your OverrideMocks() method so that all replacements are made before
    /// the Instance is created
    /// </summary>
    public void Replace<TInterface>(TInterface with) where TInterface: class
    {
        AutoMocker.Inject<TInterface>(with);
    }

    public virtual void OverrideMocks()
    {
    }

    public virtual void ExecuteBeforeEachTest()
    {
    }
}

How it works

The BasedOn<T> class defines an AutoMocker for the class that will be tested as well as a member named ‘Instance’ that is used in the unit tests. The method RunBeforeEachTest() is attributed with [TestInialize] so that it will execute prior to each unit test.

Note: Because this attribute is used in the baseclass, you cannot re-attribute any method in your derived unit test class. That is why there is a method named ExecuteBeforeEachTest() that you can override to accomodate your Unit testing needs.

If you need to prepare a special Mock or stub into the class you are testing, you can override the method OverrideMocks() in your unit test class – this method is executed just before the Instance member is created. The method Replace<T>(T with) is meant to be used for this very purpose.

Example of use

The class PersonManager is a business layer service that handles Person objects. In the following example, we are setting up a Unit test to ensure that person objects that do not have surnames cannot be updated. Using the BasedOn<T> base class:

[TestClass]
public class PersonManagerTests : BasedOn<PersonManager>
{
    [TestMethod]
    public void UpdatePerson_PersonHasNoLastName_DoesNotInvokeRepositoryUpdate()
    {
        // Arrange
        var testPerson = new Person();

        // Act
        Instance.UpdatePerson(testPerson);

        // Assert
        var repoMock = GetMockFor<IPersonRepository>();
        repoMock.Verify(o => o.Update(testPerson), Times.Never());
    }
}

In the next example, I am preparing the repository to throw an exception to verify
that an exception uses the logger interface:
 
[TestMethod]
public void Create_RepositoryThrowsException_ErrorMessageIsLogged()
{
    // Arrange
    var somePerson       = new Person();
    string exceptionText = "Something really bad just happened";
    var someException    = new Exception(exceptionText);
    var repoMock         = GetMockFor<IPersonRepository>();

    repoMock.Setup(o => o.Create(somePerson)).Throws(someException);

    // Act
    Instance.Create(somePerson);

    // Assert
    var loggerMock = GetMockFor<ILogger>();
    loggerMock.Verify(o => o.LogException(exceptionText), Times.Once());
}
 

The magic performed by AutoMocker combined with a relatively simple base class has helped me immensly in making my tests more readable – and for others to read! At the time of this writing, AutoMocker supports Moq, Castle, and TypeMock.

For those classes that you do not want automocked, you can always bypass the BasedOn<T> class and inherit the BaseUnitTest directly for access to the test helper methods and verifiers.

Smilefjes

Smart Reflection with dynamic type in .Net 4.0

A collegue of mine at my current project in NRK showed me how to use the dynamic keyword in C# to perform dynamic casting from a base type to a derived type. I just had to share!

The example starts with the following hierarchy of types:

public class Person{
}

public class Police : Person {
}

public class Fireman : Person{
}

The heart of the matter

Typically, you will have a controller/manager class that calls different workers based on the type of object at hand. In ASP.NET, for example, you may have a controller that will return a different rendering of a view based on the 
type of person to render. In its simplest form, the following 3 functions could form such a controller:
public void Draw(Person p) {
    Console.WriteLine("Drawing a generic person");
}

public void Draw(Police police)    {
    Console.WriteLine("Drawing the police");
}

public void Draw(Fireman fireman) {
    Console.WriteLine("Drawing a firefighter");
}
Easy peasy, you say and proceed to write some test code: 
// Arrange
var people = new List<Person>{
    new Person(),
    new Police(),
    new Fireman()
};
// Act
foreach (Person p in people)
    Draw(p);
Do you see the problem?

…the above code will issue 3 calls to the function that draws the base person type. The other two functions are never called. This is self-explanatory, since the foreach loop defines a Person reference. Using var has the same effect, because it is getting populated from a list of Person objects. The compiler simply does not see the Police or Firemen objects.

Now, if you change the foreach-loop to this:

// Act
foreach (dynamic p in people)
    Draw(p);

(use of the dynamic keyword requires that you add a reference to Microsoft.CSharp and System.Core)

The dynamic keyword is strongly typed, but, the compiler is told that the type of object, p will be determined at runtime, and not during compile time, thus each person object in the list will become a strongly typed police and fireman during runtime, similar to dynamic_cast in c++ (damned near identical if you ask me!)

Concerns

Any type of run-time-type checking will involve reflection in some form or another, however, Microsoft has gone a long way in ensuring that this is second-generation reflection, using a funky named tri-state cache to keep things fast. Another concern, of course, is that you can always cast any type to dynamic without compile-time checking, so you basically want to ensure 100% coverage by unit tests as well as ensure that the unit tests will catch type-mismatches.

Conclusion

Using the dynamic keyword will clean up the code considerably at the cost of having to keep in mind that you wil have no compile time checking of any lines of code that involve the dynamic keyword, so make sure you write those unit tests. Additionally, because this is run-time checking, you should consider other options if you depend on faster code, but for most of your code, the performance impact is non-measurable.

More on dynamic keyword here – Official documentation on MSDN

Dissecting the C# 4.0 Dynamic programming – Good article about the inner workings of dynamic

Watt’s a dog?

– “Yes he is”

My old blog server was sucking out roughly 700 Watts/Hour. By buying a new, modern, cheap PC, I’ve reduced the cost of hosting my own blog to 1/10th of what I was paying!

How much was the old server costing me?

700 watts on idle setting means 0.7 kWh, 24 hours a day, 365 days a year = ~6100 Kwh per year! This was an age-old Compaq Proliant Server with two power supplies and a series of cooling fans – not only did it pollute my power bill, it also made a serious amount of noise!

Given today’s power prices, that means roughly  3,000 NOK ( 368 EUR / 511 USD) per year just for having my blog available 24/7! Something had to be done, and fast!

Options

I started to look for a replacement by checking out some servers, but most of these use around 200W of power, and were fairly expensive compared to desktop PC’s (I wanted at least a dual core and 2GB RAM)

I looked into a few Windows Home Servers, but  frankly, I need IIS7, and dont want to limit myself to what I can do on that platform. It is a point, however, that Home Server CAN host WordPress, and HP MediaSmart is rumored to use around 80W/hour, which is not bad at all!

Cheap desktop PC to the rescue

I landed on a cheap-ish Packard Bell iMedia PC (image below) that looked like a match for the job. Priced at roughly the same as a year worth of server power, I got a dual-core AMD processor with 3GB of ram and some 300GB of drive space. plenty  for hosting IIS7 and WordPress on MySql, and no restrictions in case I want to deploy an ASP.NET application or two.

Packard Bell imedia Desktop PC

Once at home, I hooked it up to my watt meter and low and behold, the darned thing does not use more than 50W on balanced setting (around 62W on high performance setting)

50W is a number I can live with 24/7!! I’ve got lightbulbs that use more than that! New cost per year (in power):

238 NOK ( 29 EUR /  40 USD)

That is less than the monthly fee for using one of the cheaper web-hotels out there!

 

Conclusion

Investing roughly a year’s worth of electrical power to my old web-server, I was able to cut the power consumption of my blog to 1/10th. Since I don’t have that much traffic, the hardware is more than able to respond to my needs, and I got rid of that old, noisy huge box that was doing nothing but costing me money. Less electricity spent = more frogs to kiss somewhere…

You’re actually reading this on the new hardware right now!

Facelift

Young roses

Young roses

Change

Summer again and time for frantic attempts to upgrade oneself with a minimal effort.

I started on the atkins diet last week, having seen how effectively it has reduced my collegues at work in weight. The concept of denying the body carbohydrates in order to force it to use its own reserves is as simple as it is painfully obvious. The first couple of days were as expected, mood fell drastically, mostly due to lack of coffeine. Once I gave that one the finger, and started drinking coffee again, everything was cool. Plan is to weigh in every sunday, and measure progress. Tomorrow is the first week past with eating max 20g of carbs every day. If there is significant loss, I’ll let you in on it :)

Openly

My opensource project got a nice boost yesterday when Balsamiq mockups granted me a license to their no less than fabulous product. I am now able to sketch up user dialogs in zero time and discuss them almost real time with non-technical friends and family to plan the user experience way before I start coding. This saves me a bunch of time in redesign as well as giving everyone a chance to discuss and pitch ideas without feeling that they are imposing on a near-finished product. This product is, hands down, one of the best products for user interface design. I recommend it to anyone and everyone wanting to try out their design before they actually  make it. You’ll be surprised!

Mock me, Amadeus!

I hit a barrier at work yesterday with the otherwise super cool mocking framework moq as it does not offer any  stubbing functionality. This little feature lack results in that you actually have to decorate all your expectations with the virtual keyword, or extract an interface for the class that you’re mocking in order to let Moq override it. A nuisance, and one I hope that there will be a fix for in coming versions. I belive the proper term for what I want here is stub funcionality, but – I’ve been wrong on computer terminology before.

Press me over

Finally, just wanted to express how amazed I am with wordpress as a blogging tool. I hadn’t upgraded this software in over a year, and so much has happened since that  I feel like I got a brand new blogging tool with great ease of use and administration. WordPress is seriously an awesome blog server!

Peace out.

eDemocracy or eCircus?

True democracy can only exist if you leave a vast majority of the decision making to the people.

Today, this is not an option because of the logistics involved in asking the people for their opinion. Instead, we elect someone to represent the majority’s selection based on promises that are not always fulfilled.We elect them because we agree with most of what they say.

My dream is, that when we as people, have the technology to securely identify ourselves digitally, we are no longer bound to the whims and promises of politicians. We as a people, are then no longer technically impaired from expressing our opinion on all the matters where we do have an opinion.

In a dream I had, the politicians were promotors for change, advocates for their points of view and henchmen of the people, carrying out the people’s will, but all decisions were ultimately voted for by anyone and everyone who cared enough to cast a vote on the subject. Because doing so was effortless, and represented the will of the country’s people on every single matter.

But would this lead to circus?  Are we, as people, enlightened enough to be trusted with such power?

Primitive obsession

Primitive obsession is a developer’s reluctance to write small objects instead of using primitive data types. One such example could look like this:


// when I blog, I have no fantasy for function names
public long ShoesInStockByShoenumber( int shoeNumber )
{
var matches = from shoe in _datacontext.Shoes
where shoe.size == shoeNumber
return shoe;

…etc…

The parameter named “shoeNumber” is an integer, and, defies oop in that the function should really be named ShoesInStockByShoenumberAsInteger( … ) (yup, eww!)

What you really want is to objectify shoenumber:
public class ShoeNumber
{
// This is all we need for now
public int Size{ get; set; }
}

… you can quickly expand the functionality of the ShoeNumber class if and when you need to. One typical first could be to throw an exception whenver a value is given that could not possibly represent a valid shoenumber.

So:
Why should I bother with packaging small parameters like this into objects?

Answer:
Because it is semantically correct, because it is oop in practice and because it makes refactoring your code that much better. But, to me, most importantly:

Packaging your parameters into objects means you can save your functions from having to check them. Objects should be smart enough to validate themselves.

Dont be like me; avoid the obsessing with primitives, no variable is too small to be turned into an object. Overcome this, and a world of options will open up for your code.

Signs of life

After a fairly long period of nothingness, I find myself back again with renewed umpf as a shadow mysteriously lifted away from a long period of darkness (perhaps I’ve started loosing eyebrow hair!)

shot from the hip

A quick trip to Skeikampen rejuvenated my interest in photography and moving around. It’s lovely up there, and I cannot but praise Bergfrid’s workplace for allowing me and Quita to tag along up there for the whole week-end. Wonderbar! The image above was a hipshot using my D300 together with the inconspicuous 50mm f/1.8. Easy to fire when everything is a reflector.

Gadget Home

We bought a small printer to print out some photos with (10x15cm images) – For it’s price, the photos look amazing. Maybe now, Bergfrid will finalize that scrapbook that she’s been going on about for the past two years..

A Denon blu-ray player had to find it’s way as my DVD player stopped working. It’s doing ok, but I find that we’re getting ripped off more and more, taking 2 minutes to load a disc, how is that progress?

Wanted to get me the new Nikon D700, but the price went up with more than what a smaller Nikon DSLR costs. Nikon has decided to up their prices at a time where a financial crisis has scared everyone away from doing purchases. I should send them a postcard with a hint… People do not take well to prices going up when the finances are as they are..

So I figured, let me get a better satellite tuner! The one I have is sub-standard – downright pathetic. A few calls later,  I desisted. The remaining shops that sell these kinds of tuners have no concept of service-mindedness.

Instead, I ended up buying a satellite signal finder hoping for a better performance from my HD Satellite PVR, now, all I need is fine weather to climb up on the roof and connect it…

See you soon!