Demystifying Keyword Driven Using WATIR
Inspiration:
While working for growing organization where QA budget is limited and cannot afford to spend thousand of dollars on commercial tool like WinRunner/QTP, a need was felt within QA team to use automated functional tool.
WATIR (Web Application Testing In Ruby) was selected as freeware tool for POC.
WATIR can be downloaded from http://watir.com/installation/
Baby Steps:
After downloading WATIR, QA team started reading through the documentation and quickly realized that it is extremely user friendly and does not take much time to write first functional automated test.
Soon we started creating automation test cases and as the count of test cases grew, the maintainability of WATIR code started to become a big problem. A need for framework was felt and since everyone in QA world talks about Keyword Driven framework, we decided to implement the same.
Roadblock:
QA team Google and found thousands of document related to Keyword Framework on internet. All of the documents talk about "WHAT" instead of "HOW" to create Keyword Framework.
QA Team took this up as challenge and decided to build Keyword Framework with minimum strings attached.
The following pages talk about "HOW" to implement Keyword Framework. We hope, this document will help demystify the Keyword Framework and knowledge will be accessible to everyone instead of fortunate few.
Step 1:
Identify and understand the fields available on Excel sheet
Excel sheet is use to separate the code logic and data. Test data and the operations/sequence of the test is planned in external data file.
| Keyword |
Object |
Prop Name |
Object Prop Value |
Expected Output |
Actual Output |
Parm_01 Screenshot_Name |
Column Name Explanation
• Keyword : They are special words which would be use to drive the code
• Object Prop Name : Each object on webpage can be identified by Property Name
• Object Prop Value : Each object on page layout can be identified by Property Vlaue
• Expected Output : This column contain the expected result
• Actual Output : The text step pass or fail
• Parm_01: Parameter use to enter/select data on webpage
• Screenshot_Name : Use to store the screenshot name of Actual result
Step 2:
Following are the high level test steps which would be automated using WATIR
• Open Browser and navigate to Google
• Enter "Astadia" in search text box
• Click on website for Astadia
• Verify text Astadia on website
Step 3:
Identify the Keywords to be use. Keywords chosen should be self explanatory as this will help to transition the work of creating testcases to manual testers allowing automation expert to concentrate on writing code.
Following Keywords were identified
OpenURL ---> Use to open IE browser and navigate to specific URL
SetText ---> Use to write text to textbox
ClickButton ---> Use to click on button
ClickLink ---> Use to click on link
Result ---> Use to compare expected and actual result.
Step 4 :
Write Pseudo Code and then we will convert it into WATIR
Open Excel File
Loop through each sheet
For each row in sheet
Use CASE statement and perform actions depending on Keyword
End Case
End For
End Loop
Close Excel File
Step 5:
Created Excel File as follows
| Keyword | Object Prop Name |
Object Prop Value |
Expected Output |
Actual Output |
Parm_01 |
Screenshot_Name |
|---|---|---|---|---|---|---|
| OpenURL | http://www.google.co.in/ | |||||
| SetText | name | q | Astadia | |||
| ClickButton | value | Google Search |
||||
| ClickLink | href | http://www.astadia.com/ | ||||
| Result | Astadia |
To get an object property and value, use Firebug (Add on for Firefox) or IE Developer Tool bar.Step 6:
Write code in WATIR and save the file with extension rb
#Include The Library require 'win32ole' require 'watir' require 'watir/IE' require 'D:\AKF\WorkingCode\Utility.rb' require 'watir/screen_capture' include Watir include Watir::ScreenCapture begin #Open Excel File excel = WIN32OLE::new('excel.Application') excel.DisplayAlerts = false workbook = excel.Workbooks.Open('D:\AKF\WorkingCode\POC.xls') #Loop through the worksheets for i in 1 .. workbook.Worksheets.Count worksheet = workbook.Worksheets(i) rowcount = worksheet.UsedRange.Rows.Count for j in 2..rowcount Keyword =worksheet.Cells(j, 1).value Object_Prop_Name = worksheet.Cells(j, 2).value Object_Prop_Value = worksheet.Cells(j, 3).value Expected_Output = worksheet.Cells(j, 4).value Parm_01 = worksheet.Cells(j, 6).value case Keyword when /^OpenURL/ @Browser=IE.start(Parm_01) @Browser.maximize when /^SetText/ @Browser.text_field(:"#{Object_Prop_Name}", Object_Prop_Value).set(Parm_01) when/^ClickButton/ @Browser.button(:"#{Object_Prop_Name}", Object_Prop_Value).click when/^ClickLink/ @Browser.link(:"#{Object_Prop_Name}", Object_Prop_Value).click when/^CloseURL/ @Browser.close when/^Result/ Actual_Output=verify_text(Expected_Output,"Pass","Fail") worksheet.Cells(j, 5)['Value']=Actual_Output time_stamp_s = Time.new.strftime('%m%d_%H%M_%S') screenshot_filename="#{time_stamp_s}_#{Expected_Output}"+"\.jpg" screen_capture(screenshot_filename,active_window_only=false, save_as_bmp=false) worksheet.Cells(j, 7)['Value']=screenshot_filename workbook.SaveAs('D:\AKF\WorkingCode\POC.xls') else "Exit" end end end workbook.Close() excel.quit() rescue workbook.Close() excel.quit() end
Step 7:
Following will be output of excel after executing above script.
| Keyword | Object Prop Name |
Object Prop Value |
Expected Output |
Actual Output |
Parm_01 |
Screenshot_Name |
|---|---|---|---|---|---|---|
| OpenURL | http://www.google.co.in/ | |||||
| SetText | name | q | Astadia | |||
| ClickButton | value | Google Search |
||||
| ClickLink | href | http://www.astadia.com/ | ||||
| Result | Astadia | Pass | 0521_1613_55_Astadia.jpg |
Author Biography:
Hirday is currently working as Sr. Manager (QA & CS) in Astadia. He has more than 9 years of IT experience. He is responsible for competency development of the Testing Group in Astadia.