Automating a WPF UI using ruby, win32ole and White

UPDATE (17/2/2010): It *can* be done, see here: How to test a WPF app using IronRuby and White. The solution doesn’t use win32ole though… just IronRuby

—–

Short story: it doesn’t work (…well, not out-of-the-box).

Long story: I’ve finally had the opportunity to try something that I’ve been meaning to do for a while now… automated testing of a .Net app with a WPF UI by using ruby, its win32ole library and White – the C# library for testing Win32/WPF UIs on windows.

Well, it turns out that it can’t be done because COM does not support static methods. Which is a pain, because White uses static methods all over the place – just to launch the app you want to test requires calling the static Application.Launch(string path_to_exe) method, so you can’t even start it, never mind interact with it!

It can be made to work though… but a thin layer of custom C# would be required (a gap in the open source test tools world – anyone?). A colleague and I proved that if we modified the White.Core.Application class by adding a instance method that calls the pre-existing static Launch method and returns an application instance, our method *can* be called by ruby’s win32ole library and we got an instance (back in rubyland) of the WPF app we wanted to test.

But a custom Launch method is only the beginning. Each of the static methods in White that would need to be called in ruby would need to be wrapped somehow in an instance method. There are quite a lot of them – the methods in classes like SearchCriteria, etc. So it’d be possible, but painful.

If you’re lazy, give up and stop reading here. If you want to have a play with this, here are the steps that I took just to get to the stage where I could create a White.Core.Application object in ruby. None of this is documented anywhere (well, not that I could find in the 2 days that I was playing with this), so I hope it’s of help to someone…

Compile White in such a way that it’s accessible to win32ole

1) Open the White solution in Visual Studio
2) Open the properties for the Core project, select the Application tab, click the Assembly Information button and check the ‘Make Assembly Com-Visible’
3) In the properties window again, select the Compile tab and check the Register for COM Interop checkbox
4) Compile (Release, not Debug)

Register the White.Core.dll Assembly

Buried inside \bin\Release will be the White.Core.dll file. We need to register it…

1) regasm /tlb:mytlb.tlb White.Core.dll (creates the type library)
2) regasm /codebase White.Core.dll (registers the assembly)

Playing with stuff in ruby

require 'win32ole'

WIN32OLE_TYPE.new('White_Core', 'Application') #returns an application instance (note the underscore)

WIN32OLE_TYPE.ole_classes( 'White_Core').collect {|c| c.to_s}.sort.each {|s| puts s} (lists all available white classes)

WIN32OLE.new('White.Core.Application') (note… no underscores!)

Not great, I know. But it’ll get you over the first few hurdles at least.

Useful links:
http://stackoverflow.com/questions/265879/can-ruby-import-a-net-dll
http://rubyonwindows.blogspot.com/

One thought on “Automating a WPF UI using ruby, win32ole and White

  1. Hi,

    I want to know whether we can automate the WPF UI using RFT 8.1.
    Because I am not able to recorgnise all the objects in WPF UI such as labels, tabs, some icons etc…

    Thanks
    Krishna

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>