Ruby Gem – Rasta with watir

Rasta  (Ruby Sporeadsheet Test Automation)

Installation:

>gem install rasta

dependencies :

>gem install rspec -v 1.2.9

Note: The latest version 1.3.0. have some issues with rasta. So please install the version 1.2.9

What is Rasta ?

Rasta is a keyword-driven test framework using spreadsheets to drive testing.
It’s loosely based on FIT, where data tables define parmeters and expected
results. The spreadsheet can then be parsed using your test fixtures.

Rasta  with Watir:

you’re more likely to use Rasta with another tool like Watir so let’s start
with an example doing some automation with Google’s web site.

Let’s say we wanted to create test cases for testing a search phrase to see if
it qualifies as a Googlewhack. Googlewhacks are two word queries on Google
that produce one and only one result. The best place would be to start
with your spreadsheet.

search_term is_googlewhack?()
cornball logocentrisms
squirreling dervishes
quantum quizzer
despotism fluidics
megalomaniacal dipsomaniac

So now we have a few data elements and we’re ready to create the test fixture.
Let’s call the class ‘Googlewhack’ so name the tab in your spreadsheet
should also be ‘Googlewhack’ – that’s how Rasta knows which class to call.
First we’ll put in the watir plumbing.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end
end

When you include the Rasta fixture in a class you can use before_all(),
after_all(), before_each() and after_each() to do any setup or teardown you
need. Just for grins, let’s go ahead and run our script and see what we get.
I’m assuming here that you have a directory examples\fixtures where you
placed googlewhack.rb.

rasta -f examples\fixtures googlewhack.xls

The IE browser starts like we’d expect and you can see it go to the Google
home page with each row, but we got an error in the cells for the search terms. To
see the error, run your mouse over the cell and it should show something like:

method: search_term()
expected no Exception,
got #<NoMethodError: undefined method `search_term’ for #>

This is telling us that search_term is not recognized by our test fixture. We
should include it as a class attribute.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  attr_accessor :search_term
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end

end

Give it another run. There are now no more errors in the spreadsheet but on the
commandline we see:

Finished in 0.0 seconds
0 examples, 0 failures
Test results are available in C:/workspace/rasta-trunk/rasta_test_results

So no tests were run. That must be because we never created the is_googlewhack?()
method. We’ll add support for the search box and hitting the button first.
To know how to call the HTML elements you can either view the source or use the
handy IE Developer Toolbar.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  attr_accessor :search_term
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end
  
  def is_googlewhack?
    @browser.text_field(:name, 'q').value = @search_term
    @browser.button(:name, 'btnG').click
  end

end

Try running the script again.

Finished in 0.0 seconds
0 examples, 0 failures
Test results are available in C:/workspace/rasta-trunk/rasta_test_results

Still no tests run. One thing to remember is that if there is no data in the
spreadsheet (or if the cell is italicized) then nothing happens. The fixture
is correctly setting the attributes, in this case setting search_term, but
there’s no method being called. We need a return value for is_googlewhack?().
It’s not clear that the method returns anything so for now put ‘nil’ in one
of the cells.

search_term is_googlewhack?()
cornball logocentrisms nil
squirreling dervishes
quantum quizzer
despotism fluidics
megalomaniacal dipsomaniac

If you run it again, we’re closer. Now the function is getting run for the
first row but the test is still failing because the button click is
returning a decimal instead of nil. That’s OK because we still need to
check the results and give a good return.

require 'watir'
require 'rasta/fixture/rasta_fixture'

class Googlewhack
  include Rasta::Fixture::RastaFixture
  attr_accessor :search_term
  
  def before_all
    @browser = Watir::IE.new
  end
  
  def before_each
    @browser.goto('www.google.com')
  end
  
  def is_googlewhack?
    @browser.text_field(:name, 'q').value = @search_term
    @browser.button(:name, 'btnG').click
    resulttext = "Results 1 - 1 of 1 for #{@search_term}"
    @browser.contains_text(resulttext) ? true : false
  end

end
search_term is_googlewhack
cornball logocentrisms TRUE
squirreling dervishes TRUE
quantum quizzer TRUE
despotism fluidics TRUE
megalomaniacal dipsomaniac TRUE

Running the script now should look a lot better. It’s very likely that none of
these pass because googlewhacks don’t stay that way for long but you can visit the
site to see some current phrases that should qualify. In addition to the spreadsheet,
the commandline output should show you the details for any failures and how the
test run went.

rasta -f examples\fixtures examples\googlewhack.xls
…F.F…F

1)
‘Googlewhack[B3] is_googlewhack?() should == true’ FAILED
expected: true,
got: false (using ==)

2)
‘Googlewhack[B4] is_googlewhack?() should == true’ FAILED
expected: true,
got: false (using ==)

3)
‘Googlewhack[B6] is_googlewhack?() should == true’ FAILED
expected: true,
got: false (using ==)

Finished in 8.547 seconds
10 examples, 3 failures
Test results are available in C:/workspace/rasta_test_results

10 examples? That’s odd because we only set up 5 tests. What’s happening here
is that every test case actually creates two tests. One to run the method,
and the other to report any exceptions found while the command was run. This
is more apparent when you look at the RSpec html output in
rasta_test_results\results.html.

What’s probably a better test though is to instead have the method return the
number of results found so that when the test fails we can have a little more
information on why it failed. You could instead create a method like google_hits()
and parse the results to return the number of hits. Something like this should
work and you can change the spreadsheet from true/false to reflect the number
of hits you expect.

def google_hits 
  @browser.text_field(:name, 'q').value = @search_term
  @browser.button(:name, 'btnG').click
  google_results =
  /Results \d+ - \d+ (of|of about) ([\d,]+) for #{@search_term}/
  @browser.contains_text(google_results)[2].to_i
end


Reference -- http://rasta.rubyforge.org/
Advertisements

4 thoughts on “Ruby Gem – Rasta with watir

  1. Hi Mapla, when you write a code snippet here in blog it seems too small. Using code in wordpress will do such things. So next time when u r abt to write code use the following to wrap the code on each line ur code here

  2. Mohammed Suhail says:

    Hi raveendran,

    I am a newbie for WATIR m just now started to do automation using WATIR. I would be more thankful to u if u help me in the question behind.

    I work on a school automation tool where i use a lot of Text fields. I would like you to explain about using EXCEL for the data driven process for those text_fields.

    I would provide u that link

    http://malli.sandbox.fedena.com/library

    username – admin
    password – admin123

    i would like you to please say how to test this LOGIN page with different data using excel.

    Thank u
    Regards
    Mohammed Suhail

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s