Geeks With Blogs

AzamSharp Some day I will know everything. I hope that day never comes.

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 InstantRails. You also need to download and install Watir (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.

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

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.

Posted on Wednesday, January 2, 2008 9:27 AM | Back to top


Comments on this post: Testing ASP.NET Application Using Watir And Ruby

# re: Testing ASP.NET Application Using Watir And Ruby
Requesting Gravatar...
You should check out WatiN (http://watin.sourceforge.net/). Basically the same thing as Watir but you can write your tests in .NET and use all the common .NET unit test frameworks.

Having gone the Ruby + Watir for ASP.NET testing before I would highly recommend skipping it and using WatiN. Unless you really love the Ruby syntax and want to re-create your test infrastructure and abandon all the .NET tools.

Just my 2 cents :)
Left by Justin on Jan 02, 2008 11:01 AM

# re: Testing ASP.NET Application Using Watir And Ruby
Requesting Gravatar...
Thanks guys!

I will check out Watin :)

Looks promising :)

Left by Mohammad Azam on Jan 02, 2008 3:31 PM

# re: Testing ASP.NET Application Using Watir And Ruby
Requesting Gravatar...
It is nice and helping a lot and know about latest needs and requirements
Left by subbu on Jan 03, 2008 9:49 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net