I’m using Cucumber+Watir to write few tests on google url. Following is
my folder structure:
├── features
│ ├── pages
│ │–shaik_home_page.rb
│
│ ├── step_definitions
│ │ └── example_steps.rb
│ | │—example2_steps.rb
│ │
│── support
│ │ ├── hooks.rb
│
│–example.feature
│–example2.feature
Contents of hooks.rb as follows:
require ‘win32ole’
#require “watir”
Before do |scenario|
puts “BEFORE START hooks”
puts “scenario name is #{scenario.name}”
closeAllOpenBrowsersExclusions
environmenturl()
end
=begin
After do |scenario|
puts “aFTER END”
@@googlehomepageobject = nil
puts “object value is #{@@googlehomepageobject}”
@defaultscreenshotpath = File.dirname(FILE)+’/screenshots’
time = Time.now.strftime("%Y-%m-%d %H%M%S")
if scenario.failed?
#@browser.screenshot.save
(times.year).to_s+(times.hour).to_s+(times.day).to_s+(times.min).to_s+(times.sec).to_s+’.png’
#@browser.screenshot.save time+’.png’
@browser.screenshot.save “#{@defaultscreenshotpath}/#{time}.png”
#embed
(times.year).to_s+(times.hour).to_s+(times.day).to_s+(times.min).to_s+(times.sec).to_s+’.png’,
‘image/png’
embed time+’.png’, ‘image/png’
embed “#{@defaultscreenshotpath}/#{time}.png”, ‘image/png’
end
closeAllOpenBrowsersExclusions
end
=end
def closeAllOpenBrowsersExclusions()
#Load libraries
require ‘win32ole’
puts “In closeAllbrowsers”
#Initialize variables
objWindow = nil
numWindowFlag = 1 #1 will be used to indicate that the window has not
been found, while 0 will indicate that it has
#Instantiate the Windows Shell
objShell = WIN32OLE.new(“shell.application”)
#Get a Collection of All IE Windows (this also includes Windows
Explorer windows)
objWinShell = objShell.windows
#Get a Count of All Windows in the Collection
numWindowCount = objShell.windows.count
#Loop through each Window in the Collection
for i in 0…numWindowCount - 1
objWindow = objWinShell.item(i)
#Exclude the Windows Explorer windows, to prevent a script
failure when using the title property.
#This will ensure only Internet Explorer windows are evaluated.
Also, if an error occurs with a strange Shell Window the Begin contruct
will allow the script to continue
begin
if (objWindow.name != “Windows Explorer”)
objWindow.quit
end
rescue
objWindow = nil
end
end
end
def environmenturl()
puts “IN ENVURL”
@URLS = {‘qa’ => “nothing.com”,
‘staging’ =>
“https://www.google.co.in/setprefs?sig=0_Y9tlMqE3AAWvpyuYouocCDK3YWI%3D&hl=en&source=homepage”}
if @URLS[ENV[‘URL’]].nil?
puts “qa”
@url = @URLS[‘qa’]
else
puts “not qa”
@url = @URLS[ENV[‘URL’]]
end
@browser=Watir::Browser.new
@browser.goto @url
@browser.maximize
@browser.bring_to_front
@@googlehomepageobject = ShaikHomePage.new()
puts “onject value is env is #{@@googlehomepageobject}”
end
Contents of example_steps.rb as follows:
require “rubygems”
require “watir”
#require ‘watir-webdriver’
require ‘rspec’
Given /^that I have gone to the (.+)$/ do |page_name|
puts “IN Given”
puts “------------ #{@@googlehomepageobject.googlesearchbutton}”
@browser.button(@@googlehomepageobject.googlesearchbutton).wait_until_present
end
When /^I add “(.*)” to the search box$/ do |item|
@browser.text_field(@@googlehomepageobject.searchfield).set(item)
end
And /^click the Search Button$/ do
@browser.button(@@googlehomepageobject.googlesearchbutton).click
end
Then /^"(.*)" should be mentioned in the results$/ do |item|
@browser.text.should include (item)
puts “THE END”
end
Contents of example2_steps.rb as follows:
require “rubygems”
require “watir”
#require ‘watir-webdriver’
require ‘rspec’
When /^I click a “(.*)” link$/ do |language|
case language
when ‘Hindi’
@browser.link(@@googlehomepageobject.hindilinkclick).click
when ‘Bengali’
@browser.link(@@googlehomepageobject.bengalilinkclick).click
else
raise ‘Unknowm’
end
end
Then /^I see the respective “(.*)”$/ do |buttontoclick|
@browser.button(@@googlehomepageobject.googlesearchbutton).wait_until_present
end
Contents of shaik_home_page.rb as follows:
require “rubygems”
require “watir”
class ShaikHomePage
attr_accessor :search_field, :google_search_button, :sb
attr_reader :search_field, :google_search_button, :sb
attr_reader :shaik, :googlesearchbutton, :hindilinkclick,
:bengalilinkclick, :searchfield
def initialize()
puts “in INITIALIZE”
@googlesearchbutton = {name: “btnK”}
@hindilinkclick = {href:
“https://www.google.co.in/setprefs?sig=0_Y9tlMqE3AAWvpyuYouocCDK3YWI%3D&hl=hi&source=homepage”}
@bengalilinkclick = {href:
“https://www.google.co.in/setprefs?sig=0_Y9tlMqE3AAWvpyuYouocCDK3YWI%3D&hl=bn&source=homepage”}
@searchfield = {name: “q”}
end
end
Contents of shaik_home_page.rb as follows:
require “rubygems”
require “watir”
class ShaikHomePage
attr_accessor :search_field, :google_search_button, :sb
attr_reader :search_field, :google_search_button, :sb
attr_reader :shaik, :googlesearchbutton, :hindilinkclick,
:bengalilinkclick, :searchfield
def initialize()
puts “in INITIALIZE”
@googlesearchbutton = {name: “btnK”}
@hindilinkclick = {href:
“https://www.google.co.in/setprefs?sig=0_Y9tlMqE3AAWvpyuYouocCDK3YWI%3D&hl=hi&source=homepage”}
@bengalilinkclick = {href:
“https://www.google.co.in/setprefs?sig=0_Y9tlMqE3AAWvpyuYouocCDK3YWI%3D&hl=bn&source=homepage”}
@searchfield = {name: “q”}
end
end
My questions : 1. I’ve created an object (@@googlehomepageobject =
ShaikHomePage.new())of class ShaikHomePage in hooks.rb, how am I able to
create the object without requiring the file shaik_home_page.rb in which
the class is defined?
2. How am I able to use the object created (@@googlehomepageobject =
ShaikHomePage.new()) in hooks.rb in all other files like example_steps
and example2_steps.rb without requiring either hooks.rb or
shaik_home_page.rb files.
Without requiring I’m able to run the Cucumber scenarios successfully,
could somebody help me understand this happens?
Thanks,
#M