Writing Watir unit tests with Eclipse and RDT

by Klaus Graefensteiner 31. May 2010 04:41

Introduction

This is the tenth article of a series of blog post called the WIMPinator Chronicles that describe how to setup a PHP development environment for Windows 7 and IIS 7.5.

So far we covered how to install a Wordpress blog and all its dependencies using the WPI (Web Platform Installer). Then we installed additional features and extensions to the PHP deployment on Windows for IIS. I explained how to get PEAR setup and how to download and deploy the PHPUnit unit testing framework using PEAR. In the next two posts I described how to install Eclipse PDT and make it work with Subversion source control using Subclipse. I showed how to create an IIS 7.5 Fast CGI web site from scratch and in a more recent tutorial I covered how to use ANT build tasks to deploy your PHP project from Eclipse to your IIS test server using custom build steps. I demonstrated how to use XDebug with PDT and IIS.

This blog post is a follow up on the last article that described how to install and configure the de facto Web UI test automation standard Watir. I am taking the Watir environment to the next level and use Eclipse and the Ruby Development Tools (RDT) to write and run Watir Web UI tests as unit tests.

The Series

  1. Getting a Wordpress blog installed in a jiffy on Windows 7 using the Web Platform Installer
  2. Adding additional features to PHP
  3. Installing PEAR and PHPUnit
  4. Installing Eclipse PDT
  5. Configuring Eclipse PDT to work with Subversion source control
  6. Creating a new IIS 7.5 fast CGI web site
  7. Configuring Eclipse to work with Ant build tasks
  8. Setting up XDebug with Eclipse and IIS 7.5
  9. Configuring Ruby and Watir
  10. Writing Watir unit tests with Eclipse and RDT
  11. Moving a Wordpress blog from GoDaddy shared hosting to my local debugging system.
  12. NEW: Setting up the CodeIgnitor MVC framework with PDT and IIS 7.5
  13. NEW: Configuring IIS 7.5 Rewrite rules to exclude index.php from the URL
  14. NEW: Configuring SSL on IIS 7.5
  15. NEW: Writing unit tests with PHP Unit

References

Here are the links that I used to research the topic of this blog post:

Using the Ruby Development Tools with Eclipse
http://www.ibm.com/developerworks/opensource/library/os-rubyeclipse/

Watir Tutorial:
http://wiki.openqa.org/display/WTR/Tutorial

Unit Test Examples:
http://svn.openqa.org/svn/watir/trunk/watir/unittests/

Installing the Ruby Development Tools for Eclipse

The two prerequisites for this tutorial are to have Eclipse and Watir installed. If you haven’t done this yet, then use the following two tutorials:

Installing Eclipse PDT

Configuring Ruby and Watir

First open Eclipse using the Run as administrator option:

WindowClipping (5)

Figure 1: Run Eclipse as Administrator

Note: I am using Eclipse Build id: 20100218-1602 for this tutorial

About Eclipse

Figure 2: Eclipse Build id: 20100218-1602

After you launched Eclipse go to the Help->Install New Software.. menu

PHP - Eclipse

Figure 3: Open the Install New Software wizard in Eclipse

From the Work with: drop-down box select the “Galileo – http://download.eclipse.orb/releases/galileo” option. This will fill the list view underneath with a list of available software extensions for Eclipse.

Install

Figure 4: Select Work with: Galileo…

Then scroll to the Programming Languages node and expand it.

 Install (2)

Figure 5: Expand the Programming Languages

You can also type “ruby” into the filter text box. This will take you straight to correct the Dynamic Languages Toolkit package.

Install (3)

Figure 6: Filter for ruby

Select the Dynamic Languages Toolkit node that is the result of the ruby filter.

Install (5)

Figure 7: Select the Dynamic Languages Toolkit that the ruby filter displays

Click next to get to the Install Details dialog.

Install (6)

Figure 8: Install Details dialog

Click next and pick the “I accept the terms of the license agreement” radio box.

Install (7)

Figure 9: Accept the license agreement

Click Finish to start the download of the Dynamic Languages Toolkit for Ruby package.

Install (8)

Figure 10: The package is downloading

Click on the Checkbox and then OK to trust the package’s certificate.

Selection Needed

Figure 11: Trust the certificate

At the end of the installation a message box asks you to automatically restart Eclipse. Say No and restart manually.

Software Updates

Figure 12: Say No and restart Eclipse manually

At this point the Dynamic Language Toolkit for Ruby in Eclipse should be successfully installed.

Configure a Ruby Interpreter for Eclipse

In the next section I am going to describe how to bind Eclipse with the already pre-installed Ruby executable / Interpreter. The quickest for this is to create a new Ruby project. The new project wizard will guide you through the process of hooking up the Ruby runtime.

Go File->New->Project in Eclipse.

PHP - Eclipse (3)

Figure 13: File New Project

This will open the New Project’s Select a wizard dialog. Expand the Ruby folder and select the Ruby Project node.

New Project

Figure 14: Select the Ruby project wizard

In the Create a Ruby project wizard specify a Project name. In my example I am calling the project WatirTests. Since the default interpreter is currently undefined (see the execution section of the wizard) we need to click on the blue hyperlink that says: Configure interpreters…

New Ruby Project

Figure 15: Specify project name and then configure an interpreter

In the interpreter Preferences dialog click the Add… button on the right to add a new interpreter.

Preferences (Filtered)

Figure 16: Add a new interpreter

Next click the Browse… button and navigate to the Ruby\bin folder.

Add interpreter

 Figure 17: Browse to your ruby.exe file

I installed Ruby under C:\Ruby and therefore I am going to pick C:\ruby\bin\ruby.exe.

Add interpreter (2)

Figure 18: Interpreter system libraries get added after the executable has been selected

Next press OK to add the ruby interpreter to the list. and select it.

Preferences (Filtered) (2)

Figure 19: Press OK to add the new interpreter

Create a Ruby project

At this point we continue to actually create a project. Select the interpreter (See Figure 19) and then click OK.

New Ruby Project (2)

Figure 20: With the interpreter selected provide a Project name

Click Next to review your choices. Then click Finish.

New Ruby Project (3) 

Figure 21: Review your project choices

The next dialog asks you whether you would like to change your perspective to Ruby.

Open Associated Perspective

Figure 22: Change perspective to Ruby

Writing your first Watir unit test

With the new project active go to File->New->Ruby Class menu to create a new *.rb ruby file.

Ruby - Eclipse

Figure 23: Create new ruby class

The new ruby class will be a unit test class. I call it GoogleTest.

New Ruby Class

Figure 24: Call the test class GoogleTest

Our GoogleTest unit test class will open a browser, navigate to Google, search for Watir and click on the first search result to get to the Watir home page. Then it will look for a specific string and pass, if the string matches an expected value.

Google - Windows Internet Explorer

Figure 25: Testing Google with Watir

Copy the following source code snippet into the GoogleTest.rb text editor.

Unit Test for Eclipse 
require 'test/unit' 
require 'watir' 

class GoogleHomePage < Test::Unit::TestCase 
  def test_there_should_be_text_About_Google 
    browser = Watir::IE.start "http://www.google.com" 
    assert(browser.text.include?("About Google")) 
  end 
end 

require 'test/unit' 
require 'watir 
class GoogleHomePage < Test::Unit::TestCase 
  def test_there_should_be_watir 
    ie = Watir::IE.new 
    ie.goto("http://www.google.com") 
    ie.text_field(:name, "q").set "watir" 
    ie.button(:name, "btnG").click 
    ie.link(:href, "http://watir.com/").click 
    assert(ie.text.include?("Why Watir?")) 
  end 
end

require 'test/unit' 
require 'watir' 
class GoogleHomePage < Test::Unit::TestCase 
  def test_there_should_be_watir 
    ie = Watir::IE.new 
    ie.goto("http://www.google.com") 
    ie.text_field(:name, "q").set "watir" 
    ie.button(:name, "btnG").click 
    ie.link(:href, "http://watir.com/").click 
    assert(ie.text.include?("Why Watir?")) 
  end 
end

Executing Ruby unit tests in Eclipse

To execute the unit test just push the Run (green triangle) button in eclipse. Eclipse will ask you whether you want to run this class as Test or as Script. Select Ruby Test and click OK.

Run As

Figure 26: Select Ruby Test

And Voila, if everything goes well your first Watir unit test got executed by Eclipse and passed successfully.

Ruby - WatirTestsGoogleTest.rb - Eclipse (2)

Figure 27: Watir unit test passed in Eclipse

Ausblick

At this point, after producing this tutorial, I can’t wait to actually write some Watir scripts. Here are some ideas I would like to work on:

  1. Submit blog posts to DZone automatically using Watir
  2. Running load tests using Watir
  3. Monitor my website response times with Watir

I hope this and the previous tutorial will help you with the development of Watir tests.

Tags: , , , , , , ,

WIMPinator Chronicles | Tips & Tricks | Test Automation | Ruby | Eclipse

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2012, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2012 by Klaus Graefensteiner

Rendertime:
Page rendered at 2/4/2012 6:22:31 AM (PST Pacific Standard Time UTC DST -7)