Wednesday, August 3, 2011

WATIR framework

What I am going to discuss here is a standard Page Object Pattern Framework for WATIR.

What each and every framework requires are, Driver Script, Object repository, Script to read data from Data source(EXCEL, CSV or any other), Files to generate and store reports and finally Test Files.

Download Project

The framework designed by me contains following things
Xunit framework test-unit
Data Sources: EXCEL and YAML
Reports: I am using ci_reporter with JUnit ANT target to generate HTML report
Driver Script: BATCH file which will run tests according to parameter passed and will generate the report.
Object Repository: Ruby class per page.

Now let's see everything in details.

Test Script:LoginTestWithYAML.rb inside Test Folder
require 'rubygems'

gem 'test-unit'
require 'test\unit'
require 'watir'
require_relative '../Libs/BaseClassInstanceMethods'
require_relative '../Libs/BaseClassClassMethods'
require_relative '../ObjRepository/LoginPage'
require_relative '../Libs/ReadYAML'

class LoginTestWithYAML < Test::Unit::TestCase
  include ReadYAML
  include LoginPage
  include BaseClassInstanceMethods
  extend BaseClassClassMethods
  def test_gmail
    username_textbox.set(readData("noPassword","username"))
    password_textbox.set(readData("noPassword","password"))
    login_button.click()
    assert(($browser.text.include? "Enter your password."),"Verify Error Messages")
  end
end
ReadYAML.rb inside Libs folder
require 'yaml'

=begin
Author: Gaurang
read the YAML file based on the given scenario name and fieldname
=end

module ReadYAML
  @@config = YAML.load_file("#{File.dirname(__FILE__)}/../data/gmail.yaml")
  def readData(scenarioName, fieldName)
    if (@@config[scenarioName][fieldName].nil?) then
      return ""
    else
      return @@config[scenarioName][fieldName]
    end

  end
end
Object repository: LoginPage.rb inside ObjRepository folder
=begin
Author: Gaurang
Contains all the elements on the login page
=end

module LoginPage
  def username_textbox
    return $browser.text_field(:id,"Email")
  end

  def password_textbox
    return $browser.text_field(:id,"Passwd")
  end

  def login_button
    return $browser.button(:id,"signIn")
  end
end
ANT Target to generate HTML report
http://qtp-help.blogspot.com/2011/07/watir-html-report.html

And finally our driver scripts: run.rb
require 'rubygems'
gem 'test-unit'
require 'test/unit'
gem 'ci_reporter'
require 'ci/reporter/rake/test_unit_loader.rb'
=begin
Author: Guarang
Driver file which would run all the tests inside Test Folder.
=end

class Run

  if (ARGV.length >= 1 ) then
    if (ARGV[0].downcase == "smoke") then
      p "smoke"
      folder= "Tests/Smoke/*.rb"
    elsif  (ARGV[0].downcase == "regression") then
      p "regression"
      folder= "Tests/Regression/*.rb"
    end
  else
    p "default"
    folder= "Tests/*.rb"
  end
 
  Dir[folder].each do |test|
    p test
    require_relative test
  end

end
rub.bat
If you want to run all the tests inside Tests\Smoke folder give enter the following command on command prompt
run smoke
for regression enter the following command
run regression
if you will not provide any argument it will run all the tests under Tests folder.
@echo off 
@echo "initializing..."
rd /s/q Test
@echo "Running Tests..."
ruby run.rb %1
@echo "Generating reports..."
start cmd /C ant
rem wait for 6 seconds
PING 1.1.1.1 -n 1 -w 6000 >NUL
@echo "Opening report..."
start test/reports/junit-noframes.html"

6 comments:

Jody said...

Can we use the frame work in Mac also?

Gaurang Shah said...

hey jody,
Yes we can use the same framework for MAC or Linux the only difference would be you can't use EXCEL for data driven, you will have to use only YAML.

Mahesh Siddireddy said...

Hey,

I have bunch of YAML files and I need to convert them into single CSV file I did research but didnt get anything can you help me on this?

Thank you in advance

Erna Tercero said...

How can i run all the test in the excel file?

Marissa said...

Is there a way to run defined scenarios in a non-aphalanumeric order?

Madhu said...

Hi,

how can i open this project and how can i run.Please tel me how can i open and run

Regards
Raghu

Post a Comment