Monitoring Typical User Transactions with Ruby and SiteScope

21 February, 2008 – 11:26 am

A colleague asked me the other day if it was possible to setup a workstation with just LoadRunner installed and have it automatically carry out a typical user transaction to determine the ‘health’ of the target server. After explaining to him that it would be a clunky approach at best, and currently lacking an installation of HP’s Business Availability Center which could achieve such a thing (Bah, humbug to that) I suggested we write a simple Ruby script, using the ‘watir’ gem which can be periodically called by the existing SiteScope installation.

If you didn’t have SiteScope installed, you could just as easily run this script as a scheduled task within Windows or via cron within a Unix environment, no sweat …

Watir (pronounced ‘water’) stands for Web Application Testing In Ruby and IMHO it really is starting to kick the pants off heavily licensed tools such as Quick Test Pro. I mean, if you’re only testing web apps, then why fork out $$$ for licenses? Just pay some decent money for someone to write you a script …
;)

But anyway, a simple Ruby script that uses watir looks like this:

require 'watir'
include Watir
require 'test/unit'
 
class CustomerSearch < Test::Unit::TestCase
 
  def test_search
    ie = IE.new
    ie.goto("https://targetwebsite.com.au")
    
    # Login
    ie.frame(:name, "WORK_SPACE").text_field(:name, "inf_user").set
    ("foxmulder")
    ie.frame(:name, "WORK_SPACE").text_field(:name, "inf_password").set
    ("scully")
    ie.frame(:name, "WORK_SPACE").button(:value, "Submit").click
    
    # Work Mode
    ie.frame(:name, "WORK_SPACE").select_list(:name, "mode").set
    ("Some Drop Down Menu")
    ie.frame(:name, "WORK_SPACE").button(:name, "submit_button").click
    
    #Product Search
    ie.frame(:name, "WORK_SPACE").text_field(:name, "NUM_advertiserId").set
    ("123456789")
    ie.frame(:name, "WORK_SPACE").button(:name, "submit_button").click
    
    #p ie.frame(:name, "WORK_SPACE").text
    assert(ie.frame(:name, "WORK_SPACE").text.include?
    ("Queried by: Advertiser ID: 123456789"))
  end
end

As you can probably read from above, this script logs into a target website, provides user credentials, searches for a product ID then checks (with an assert statement) that some form of expected text exists in the result.

If you ran this script from the command line with
ruby myScript.rb

You would see ruby fire up an actual Internet Explorer browser and drive the screen, just like you’re probably familiar with in Quick Test Pro.

If you drive it with the -b parameter, it will hide the browser which is what I recommend.

Now to get it running in SiteScope, you just need to add a custom script monitor. I wrote a simple batch file and placed it in the <installdir>/scripts directory of the SiteScope server (so that it appears in the drop down list when adding a script monitor). The batch file looked like this:


echo ##### ruby_myScript.bat called ##########
ruby "D:\SiteScope\scripts\myScript.rb" -b && exit 0 || exit 1

The reason I do that is so that I can exit with the appropriate return code. That way, if any of the test components/assertions inside the Ruby script don’t pass, they will pass on an exit code of 1 to the batch file, which then feeds that info back to SiteScope so that you get a little red traffic light for script failures.

The script monitor properties in SiteScope ends up looking like this:
Site Scope Setup

The dashboard will end up looking like this (I’ve demostrated an error, note how it also shows you the script round trip which also equates to the time that it takes for the end-to-end transaction to take place including launching the broswer)
Site Scope Error

Share it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • Slashdot
  • Technorati
  • YahooMyWeb

Post a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image