In the last article we talked about how to unit test your ASP.NET pages using WatiN. You can view the article here. In this article we will see how to use Ruby and Watir to unit test your ASP.NET pages.

Introduction:

In the last article we talked about how to unit test your ASP.NET pages using WatiN. You can view the article here. In this article we will see how to use Ruby and Watir to unit test your ASP.NET pages.

What is Watir?

Watir (Web Application Testing in Ruby) is a Ruby framework used to test web pages. Watir allows the developer to write unit tests using Ruby code and by creating assertions. The developer can playback the user expectations in the browser creating a real live browsing experience.

Installing Ruby and Watir:

Before you get started with Watir you need to install Ruby. Simply, download the InstantRails package and install it on your machine. Once, installed you can download Watir and use the following command to install it.

gem install watir

Setting Up Ruby Environment:

After installing Ruby you need to run the Apache server which will host the rails application. Simply, go to the InstantRails directory and click on the big “I” sign. This will start the Apache server.

Now, open the Ruby console window as shown in the image below:

 


Create the work folder on your machine. Go inside the folder and issue the following command.

rails depot

This will create the rails project named “depot”. If you go inside the “depot” folder you will see a long list of folders. All these folders are automatically created by the Rails framework.

 

We are interested in the test folder which will contain all the unit tests. Let’s take a look at the scenario which we will be testing.

The Scenario:

The scenario is very simple; we have a page which allows the user to accept the agreement and submit the page using the button. The page consists of a TextBox, CheckBox and a button control. Below you can see the screen shot of the page.

 


Creating Unit Test in Ruby Code:

All the unit tests in Ruby are run from the test/unit/ folder. So, go ahead and create a new .rb file inside the unit folder. You can use any editor to implement the Ruby code. I use InType since it provides color coding and keyword highlighting features. Let’s take a look at the Ruby unit test file (PageTest.rb).

require 'test/unit'
require 'watir'
require 'watir/close_all'

class PageTest < Test::Unit::TestCase
 
  def test_can_accept_agreement_when_checkbox_is_clicked

 ie = Watir::IE.start "http://localhost:1950/Default.aspx"  

 assert_equal 0, ie.text_field(:id,"txtName").value.length

 ie.text_field(:id,"txtName").set "Mohammad Azam" 
 ie.checkbox(:id,"chkAgree").click

 assert ie.checkbox(:id,"chkAgree").checked?()

 ie.button(:id,"Btn_Agree").click

 assert_equal "accepted",ie.span(:id,"lblMessage").text

  end
  
end

The first thing to note is the class name PageTest which inherits from the Test::Unit::TestCase class. This makes the PageTest class a unit test file. The name of the unit test is test_can_accept_agreement_when_checkbox_is_clicked. It is always a good idea to use description names for your unit tests. This will increase the readability of the tests.

The line ie = Watir::IE.start http://localhost:1950/Default.aspx opens the Default.aspx 
page. Make sure that the ASP.NET server is running on port 1950 (You can run ASP.NET on other port). This can be done by simply running your ASP.NET application which will automatically pop out the ASP.NET default Web Server.

After starting the internet explorer process we make sure that the text field “txtName” is initially empty. This is performed using the following line of code.

assert_equal 0, ie.text_field(:id,"txtName").value.length

Next, we type the name in the text field. Check the checkbox and submits the button. After we submit the button we check that the label “lblMessage” text property is changed to “accepted”.

ie.text_field(:id,"txtName").set "Mohammad Azam" 
 ie.checkbox(:id,"chkAgree").click

 assert ie.checkbox(:id,"chkAgree").checked?()

 ie.button(:id,"Btn_Agree").click

 assert_equal "accepted",ie.span(:id,"lblMessage").text

Running Unit Test Using Ruby:

Now, let’s try to run our unit test. Use the following command to run the tests. 

This will pop up the Internet Explorer and run the tests. You will notice that Watir automatically selected the input which you wrote the test. It will give you the experience that if some user is browsing the page (I think it is pretty cool!).

If you notice in the above screen shot the test failed. This is because the label “lblMessage” expected the text property to be “accepted” but it was “valid”. I will leave this exercise for the reader to pass this test.

Conclusion:

In this article we learned how to unit test our application using Watir. The article described a meaningful merge between the .NET framework and the Ruby framework which resulted in testing the application.

I hope you liked the article, happy coding!