Testing ASP.NET Application Using Watir And Ruby

Wednesday, January 02, 2008 9:27 AM

Now, this is freaking awesome. Testing your ASP.NET pages using Ruby code. I used to test my ASP.NET pages using NUnitASP which was pretty decent. But, I am not sure what happened after I installed VS 2008 the NUnitASP simply stopped working. Anyway, I needed a tool to test my ASP.NET pages. These pages typically include DropDownList, TextBoxes, Checkboxes and stuff. If you had to test them manually then it would take you a long time plus it is really boring. So, I came across this interesting post by Scott Hanselman, Integrating Ruby And Watir with NUnit. Scott tests some Http requests and uses NUnit with Ruby and Watir.

In this post I will explain how you can test simple ASP.NET pages. First, you need to download Ruby on Rails. I use. You also need to download and install  (Web Application Testing in Ruby). To install Watir simply typed the following at Ruby command prompt.

gem install watir

Anyway, lets cut to the chase and see the ASP.NET page which we are about to test. Check out below the HTML of the ASP.NET page.

 

        Please read this agreement and then check the checkbox and press the button            
       
        <asp:CheckBox ID="chkSelect" onclick="enableSubmitButton" runat="server" />    
    Enter your name:     <asp:TextBox ID="txtName" runat="server"></asp:TextBox>         <asp:Button ID="Btn_Submit" runat="server" OnClick="Submit_Agreement" Text="Submit Agreement" />        
        <asp:Label ID="lblMessage" runat="server" />            

This is a simple user agreement page. The user has to write his name in the TextBox field then click the checkbox and then press the button. I wrote some JavaScript code so that the button is only enabled when the checkbox is clicked.

And here is the Ruby code:

require 'test/unit' require 'watir'

class PageTest < Test::Unit::TestCase   def test_can_accept_agreement_when_checkbox_is_clicked    ie = Watir::IE.start "http://localhost:2334/Demo.aspx"       ie.text_field(:id,"txtName").set "Mohammad Azam"   ie.checkbox(:id,"chkSelect").click     assert ie.checkbox(:id,"chkSelect").checked?    ie.button(:id,"Btn_Submit").click    assert_equal "accepted",ie.span(:id,"lblMessage").text     end     end

The line ie = Watir::IE.start "http://localhost:2334/Demo.aspx"  gets the browser object. And the rest automate the behavior.

Check out the result below. The animation effects are produced automatically by Watir and yes it looks super cool!

!

Why Ruby?

First, I think Ruby and Rails is kick ass. Yeah the syntax is a little weird but then if I can perform the task in few lines then it is still cool. I think Ruby and Watir can be a great tool to test your ASP.NET pages. You will write less code in unit tests then you used to write when using NUnitASP.

This article is part of the GWB Archives. Original Author: Mohammad Azam

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.