Run Watir Tests from within Visual Studio

I have been having a bit of a play with the Watir Ruby web UI testing framework and after a bit of jiggery pokery I’ve managed to get it all going inside of Visual Studio. Here’s how…

You will need to install:

I had a bit of trouble getting Watir to install on Windows XP using the Windows installer but I think that was due to having installed Ruby using InstantRails. Once I installed it using the Ruby Windows installer it worked OK. On Vista however the installer wasn’t playing nicely at all. To install on Vista required the use of RubyGems which basically involves typing:

    gem install watir

… at the command line. Simple as that. One thing to note is that if you install Watir using the ‘gem’ approach you need to add the line:

    require ‘rubygems’

at the top of your test ruby files before adding:

    require ‘watir’

Thanks to my Ruby (tor)mentor Dave Verwer for that gem. (oh dear…)

Ruby in Steel (worst name ever competition winner 2007) will allow you to debug your Watir tests and edit them with all of the Visual Studio goodness in place.

Once you have all that in order you need a bit of code to hook your Ruby scripts into the usual NUnit/TestDriven way of things. You have two options (writing it yourself being a third). Either go ‘the whole hog’ and try and get this article on Code Project running or use the code below, adapted from Scott Hanselman’s efforts to do the same thing. (I say ‘adapted’. I mean added the ‘directoryPath’ argument as Scott’s version seemed to need the .rb files to be put in the bin, so to speak)

public class WatirAssert 
{ 
    public static void TestPassed(string rubyFileName, 
    string directoryPath) 
    { 
        string output = String.Empty; 
        using (Process p = new Process()) 
        { 
            p.StartInfo.UseShellExecute = false; 
            p.StartInfo.RedirectStandardOutput = true; 
            p.StartInfo.FileName = "ruby.exe"; 
            p.StartInfo.Arguments = rubyFileName; 
            p.StartInfo.WorkingDirectory = directoryPath; 
            p.Start(); 
            output = p.StandardOutput.ReadToEnd(); 
            p.WaitForExit(); 
        } 
        Console.Write(output); 
        Trace.Write(output); 
        Regex reg = new Regex(@"(?<tests>\d+)\stests,\s(?<assertions>\d+)\sassertions,\s(?<failures>\d+)\sfailures,\s(?<errors>\d+)\serror\s", RegexOptions.Compiled); 
        Match m = reg.Match(output); 
        try 
        { 
            int tests = int.Parse(m.Groups["tests"].Value); 
            int assertions = int.Parse(m.Groups["assertions"].Value); 
            int failures = int.Parse(m.Groups["failures"].Value); 
            int errors = int.Parse(m.Groups["errors"].Value); 
            if (tests > 0 && failures > 0) 
            { 
                Assert.Fail(String.Format("WatirAssert: Failures {0}", failures)); 
            } 
            else if (errors > 0) 
            { 
                Assert.Fail(String.Format("WatirAssert: Errors {0}", errors)); 
            } 
        } 
        catch (Exception e) 
        { 
            Assert.Fail("WatirAssert EXCEPTION: " + e.ToString()); 
        } 
    } 
}

Where you stick this is entirely up to you but you need to be able to reference it from your unit tests so you could put it in its own assembly or just add the class to your unit test project.

Once you have all that sorted out you can now add a .NET unit test such as the following:


[TestFixture] 
public class UITest 
{ 
    [Test] 
    public void LoginTests() 
    { 
       WatirAssert.TestPassed("login_tests.rb", @"C:MyAppWatirTestDirectoryUI Tests\"); 
    } 
}

In this example “login_tests.rb” is the name of the Ruby script file containing the Watir test (or tests) and the directoryPath argument is the directory containing said Ruby file.

Now, using TestDriven.NET we can right-click on the above code in VS and run the test. We can add a break point to the C# code and right-click to debug through the test. As we have Ruby in Steel installed we can also use Visual Studio to edit the Ruby file and whilst doing so we can add breakpoints in the Ruby and step into the executing Ruby with the debugger.

Oh joy…

Whilst I am on the topic I have to say that I think Ruby is rubbish. It is supposed to be the language of the Gods and it can’t even tell that when I add a semi-colon at the end of EVERY SODDING LINE, I didn’t mean to do it. Couldn’t it just ignore them for me if it is supposed to be so clever…

Advertisements

10 Responses to “Run Watir Tests from within Visual Studio”


  1. 1 scottgu January 21, 2007 at 12:43 am

    One project you might want to check out is WatiN, which is a .NET port of Watir: http://www.codeproject.com/useritems/WatiN.asp

    You can use it with NUnit or MBUnit and it allows you to write unit tests in C# or VB. For example:

    [Test]
    public void CheckIfNicknameIsNotUsed()
    {
    // create a new Internet Explorer instance pointing to the ASP.NET Development Server
    using (IE ie = new IE(“http://localhost:8080/Default.aspx”))
    {
    // Maximize the IE window in order to view test
    ie.ShowWindow(NativeMethods.WindowShowStyle.Maximize);

    // search for txtNickName and type “gsus” in it
    ie.TextField(Find.ById(“txtNickName”)).TypeText(“gsus”);

    // fire the click event of the button
    ie.Button(Find.ById(“btnCheck”)).Click();

    // parse the response in order to fail the test or not
    Assert.AreEqual(true, ie.ContainsText(“The nickname is not used”));
    }
    }

    I’ve been meaning to check it out – looks pretty cool.

    Hope this helps,

    Scott

  2. 2 Graham Pengelly January 21, 2007 at 2:33 am

    Cheers Scott, I’ll have a look at that.

  3. 3 I like colouring in June 17, 2007 at 1:02 am

    Does anyone have any experience of scripting Ruby in Google Sketchup? As an absolute beginer to scripting, but a regular user of Sketchups 3D modelling tool I am asking more out of curiosity….

    …How easy is it to pick up?

  4. 4 ftorres October 13, 2007 at 6:34 pm

    InCisif.net is an automation tool designed to implement client-side functional testing of web applications under Internet Explorer 6 or 7, using languages such as C#, VB.Net or IronPython.

    Tests can be developed within Microsoft’s Visual Studio 2005, 2008 or Express Editions.

    New features in the 2.0 release include:

    – Integration with Visual Studio environment
    – New Api members using anonymous method or Lamda Expression
    – Visual Studio 2008 test framework support

    To learn more and download an evaluation copy, go to http://www.incisif.net

  5. 5 niff May 10, 2009 at 3:36 pm

    the previous comment seems to be an automated ad…

  6. 6 fort lauderdale homes for sale January 11, 2013 at 12:03 am

    This is really fascinating, You’re an overly professional blogger. I have joined your feed and look forward to in quest of extra of your excellent post. Also, I have shared your web site in my social networks

  7. 7 Vero Beach Real Estate January 20, 2013 at 7:39 pm

    This is my first time go to see at here and i am truly happy to read
    everthing at one place.

  8. 8 http://metafrasiorg.blogspot.com/2008/04/just-browsin-lexipedia-unilang.html?showComment=1366025295582 April 19, 2013 at 10:58 pm

    In almost every application we used besides some of
    the online medical directories are good resources for choosing the right farmacia on line for you.

  9. 9 http://aprilianews.blogspot.com May 7, 2013 at 8:32 am

    Looking at the hardware from a purely philosophical standpoint, the
    iPad is equipped with 802. It is very much a nip-tuck job — just as the Internet is only the latest in a line of international.

  10. 10 Amy May 20, 2013 at 3:41 am

    What benefits are there to herbal farmacia on line? Reps from the company
    says in areleaseon its website,” help maintain endothelium-dependent vasodilation which contributes to normal blood flow to the penis, making an erection firmer.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: