* Download Complete Source Code
In an attempt to be a Pragmatic Programmer I make an effort to learn a new programming language yearly. This year the language of choice is Ruby, including Rails. About a month ago I started the Build Your Own Blog :: Rails series at TekPub. In this series Rob Conery instructs on creating a blog engine using Rails and focusing on Behavior Driven Development. Rob introduces some amazing tools, such as Cucumber and Pickle, that make BDD on Rails easy and dare I say…fun!
If you have been following along at home with my previous serious of blog posts you know that in my day job I am currently architecting a large WPF MVVM composite application using Prism. My development methodology for .Net development is TDD and not BDD, because I just don’t feel the tools are there yet in the .Net space for efficient BDD. I am at the point in the project where I am starting work on the UI and being fresh of learning the wonderful BDD tools available in the Rails stack it was really annoying me that I had no way to TDD my UI. Grant it, using MVVM I was able to get much closer to the actual UI than I otherwise would, but it still wasn’t the same. Finally I got to the point where I just had to research the idea of Cucumber style testing in WPF. What I found out was it is possible using IronRuby. Don’t be mistaken, it does not offer the same ease and simplicity as it does on the Rails stack but it is certainly a leap in the right direction.
The tool that is going to make this all possible was written by Nathaniel Ritmeyer and is called Bewildr!
Here are the steps to getting your environment setup…
- Follow the steps in my previous blog post to get Ruby, IronRuby, and Cucumber installed.
- Install Bewildr.
- Install UISpy.
I am going to be testing the UI created in my previous series: Rolling Your Own Module Level Security for a WPF MVVM Composite Application
Detailed information on Cucumber can be found here. But, basically Cucumber allows you to write plain-text functional descriptions as automated tests. Cucumber code consists of two main pieces; Features/Scenarios and Step Definitions. This is not meant to be a Cucumber tutorial so if you are not familiar with Cucumber I would suggest reading up on it prior to continuing.
OK. So, now that your familiar with Cucumber you are probably asking yourself how is this going to allow me to do automated UI testing? This is where Bewildr enters the party. Bewildr is a Ruby GEM that uses the MS UI Automation Framework to gain programmatic access to a WPF UI. Basically, I can spin up a WPF application and manipulate the UI, click buttons, enter text in text boxes, etc, all via code.
So, let’s start with a simple scenario. (I apologize for the formatting/highlighting…need to find a good formatter plug-in for Cucumber code.
application.feature
- @application
- @window
-
- Feature: Application Interaction
-
- Scenario: The Log On window should be displayed on application start
- Given I ensure that there are no instances of "CompositeAppPoc.Shell.exe" running
- When I start "C:\\development\\research\\wpf\\CompositeAppPoc\\CompositeAppPoc.Shell\\CompositeAppPoc.Shell\\bin\\Debug\\CompositeAppPoc.Shell.exe"
- Then 1 window is displayed with name "Log On"
- And Terminate the app
If we go ahead and run this feature Cucumber is going to puke on us because we haven’t created our steps yet. But, when Cucumber pukes…it pukes in a helpful way. It is going to give us the shell for the steps we need to create in order to execute this feature.

As you can see Cucumber lets us know that we need to define 4 steps in order to get our Feature and Scenario running. So let’s do that. Since 3 of the steps deal with the application itself and 1 deals specifically with a window I am going to create 2 step files.
application_steps.rb
- require 'bewildr'
-
- Given /^I ensure that there are no instances of "([^"]*)" running$/ do |app|
- Bewildr::Application.kill_all_processes_with_name(app)
- end
-
- When /^I start "([^"]*)"$/ do |app|
- @application = Bewildr::Application.start(app)
- end
-
- When /^Terminate the app$/ do
- @application.kill
- end
window_steps.rb
- require 'bewildr'
-
- Then /^ (\d+) window is displayed with name "([^"]*)"$/ do |count,name|
- @window = @application.wait_for_window(name)
- @application.windows.size.should == count.to_i
- @window.name.should match(name)
- end
In the steps coded above you can begin to see what Bewildr allows us to do. Now if you run this feature you should see the output below as well as the app physically spinning up and closing.
And as you can see our scenario passed.
This is really just scratching the surface of what you can do. I would suggest downloading the code and get an understanding of some of the more advanced scenarios and then start creating your own.
* Download Complete Source Code