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.