Understand Keyword Driven Framework by using UFT – (Part 2)

After our basic overview of Keyword Driven Framework in Part -1 , now we would implement and understand its individual components. Part3 contains the keyword sheet creation and run results.

Test Scenario:- 
We would be using the following scenario to exemplify our Keyword Framework
1.Launch web browser 
2.Search “uftHelp” on “Google”
3.Open the first link of search Result
4.Verify that www.uftHelp.com, page is opened
5.Search any given value on this Tutorial website
6.Close the Application 

Solution Explorer:-
This is how our Framework Solution would look like.

Solution Explorer in UF
Solution Explorer in UFT

TC_01_Search :-Name of our Test Case

We have taken 2 input parameters:
FilePath:- Path of our Excel sheet containing Keywords
SheetName:- Keyword sheet in the Excel

Input Parameters property window UFT
Input Parameters 

How to set input Parameters?

  • Select the Test Case(TC_01_Search) in Solution Explorer
  • View -> Properties
  • Open “Parameters Tab” -> Add -> Add Input Parameter
  • Fill the Name and Default Value , Leave Type as String 

Driver:-
It contains our driving script to control and call the other components of the framework.

Note:- 

  • We tried to add appropriate comments to explain the code,Please contact us or add comments below for any clarification.
  • For clear comprehension of the code try to run the script in debug mode (F11).

You can also follow us on FB,G+,Twitter or LinkedIn for more updates.

Code:-

Option Explicit
'*******************Variable Declaration-Start*********************
'File Path for Test Sheet
Dim sTestFilename
'Taking file path from Test Arguments
sTestFilename = TestArgs("FilePath")
'Test Sheet Name
Dim sSheetName
sSheetName = TestArgs("SheetName")
'Excel Objects
Dim objExcel,objWB,objWS
'*******************Variable Declaration-Ends***********************

'Killing any Running Excel Processes
SystemUtil.CloseProcessByName "Excel.exe"
'Killing Browser Instance of Chrome or IE
'We can add other browsers also like firefox
SystemUtil.CloseProcessByName ("iexplore.exe")
SystemUtil.CloseProcessByName ("chrome.exe")
'Saving the Total Row counts in Test Data sheet into Enviornment Variable
Environment("TotalRowCount") = fn_ExcelGetRowCount(sTestFilename,sSheetName)

'Setting the Value of Starting ROW = 2
'We have taken 1 row for header fields in the KeywordSheet
Environment("StartRow") = 2
'Execute Each row till the End of Test Data
Do While Environment("StartRow") <= Environment("TotalRowCount")
'Function call to handle the Keyword Sepearation
fn_SeperateKeywords
Loop

'Killing the working Excel Processes
'It is closing the Our Testcase Excel sheet
SystemUtil.CloseProcessByName "Excel.exe"




Explanation:-

  • Driver script fetches the input FilePath and SheetName from input parameters of the test(As explained above)
  • Closes any opened instance of Excel or browser(chrome or IE).We have various techniques to kill a browser instance from task manager.
  • It further calls the Excel lib function to fetch the number of rows in the Keyword sheet, then it iterates from the 2nd row of the sheet(1st is for headers) and Calls Main Lib to work with keywords.
  • When run is completed for all the rows, it closes the working excel (Keyword Sheet, which is opened while fetching data)

How to fetch date from a Excel file.

Object Repository:-
Common_OR, it is our shared object repository which contains the objects for the above Test scenario.

Object Repository view in Keyword Framework
OR View

We have followed the below naming conventions for objects:-
Note:- it is crucial to use the naming conventions as it is implemented in the framework to segregate the object type based in the first 2 characters of the object name.

Keyword
Object
br
Browser
pg
Page
im
Image
ln
Web Link
bt
Web Button
ed
Web Edit
el
Web Element
rd
WebRadioGroup

Function Libraries:-
It is the container for our reusable functions, we would be calling these functions to do action on the object in our AUT(Application under Test)

Keyword Lib :-

Toolbox view of Functions in UFT
Keyword Functions

Code:-

'*******************************************Function*********************************
'Function Name:- fn_Launch
'Function Description:- Opens a given browser type and redirects to given URL
'Input Parameters:- Browser exe, URL
'Output Parameters:- N/A
''***********************************************************************************
Public Function fn_Launch(sBrowser,sURL)
'Launching Browser
systemutil.Run sBrowser,sURL,"","Open",3
End Function

'*******************************************Function*********************************
'Function Name:- fn_Close
'Function Description:- To close the running Application
'Input Parameters:- Application Image Name (like chrome.exe)
'Output Parameters:- N/A
''***********************************************************************************
Public Function fn_Close(sProcess)
'Killing the running process
systemutil.CloseProcessByName(sProcess)
End Function

'*******************************************Function*********************************
'Function Name:- fn_InputData
'Function Description:- Doing Keyword level input action on the Application
' like Set,Click
'Input Parameters:- Mainwindow,Sub screen,Keyword(like Set),Main object
' (like webedit), Value
'Output Parameters:- N/A
''***********************************************************************************
Public Function fn_InputData(sMainWindow,sScreen,sKeyword,sObject, sValue)
Dim oObject, oParent
On Error Resume Next
Err.Clear
Set oObject = Nothing
Set oParent = Nothing
'Creating the Parent object Heiarchy
'Example :- browser("brGoogle").page("pgGoogle")
Set oParent = fn_CreateObjMain(sMainWindow, sScreen)
'Creating the child object Heiarchy,i.e. our object on which action is taken
'Example:-browser("brGoogle").page("pgGoogle").webedit("edTest")
Set oObject = fn_CreateObjTree(oParent, sObject)
'Adding Sync incase of Page object
If oParent.GetROProperty("micclass") = "Page" Then
oParent.Sync
End If
'Doing the operation on the Object
Select Case UCase(sKeyword)
Case "CLICK": oObject.Click
Case "SUBMIT": oObject.Submit
Case "SET": oObject.Set Cstr(sValue)
Case "SET_SECURE": oObject.SetSecure Cstr(sValue)
Case "SELECT": oObject.Select Cstr(sValue)
Case "EXTENDSELECT": oObject.ExtendSelect Cstr(sValue)
Case "SELECTINDEX": oObject.Select Cstr("#"&sValue)
Case "TYPE": oObject.Type Cstr(sValue)
Case "FIRE_EVENT": oObject.FireEvent Cstr(sValue)
Case "SEND_KEYS": oObject.Click : Wait 1:SendKeys Cstr(sValue)
Case "CLOSE": oObject.Close : Wait 2
Case Else
Reporter.ReportEvent micFail,"Input type is not recognised,"&_
"Inside fn_InputData function ", sInputOperation
'ExitTest
End Select
'Incase of Error
If Err.Number <> 0 Then
Reporter.ReportEvent micFail, "Error Happened inside fn_InputData "_
, Err.Description
On Error GoTo 0
'Exit test run
'ExitTest
End If

Set oParent = Nothing
Set oObject = Nothing
End Function

'*******************************************Function*********************************
'Function Name:- fn_CheckData
'Function Description:- To check the Expected property of the object like innertext
' should match for the object
'Input Parameters:- Mainwindow,Sub screen,Keyword(like value,innertext),Main object
' (like webedit),Value,comparision type (=,<>,>)
'Output Parameters:- N/A
''***********************************************************************************
Function fn_CheckData(sMainWindow,sScreen,sProperty,sObject, sValue,sParam1)
Dim sExpectedValue,sActualValue,sCompareType
'Expected value to be compared
sExpectedValue = sValue
'Comparison factor like '=','<>'
sCompareType = sParam1
On Error Resume Next
Err.Clear
Set oObject = Nothing
Set oParent = Nothing
'Creating the Parent object Heiarchy
'Example :- browser("brGoogle").page("pgGoogle")
Set oParent = fn_CreateObjMain(sMainWindow, sScreen)
'Creating the child object Heiarchy,i.e. our object on which action is taken
'Example:-browser("brGoogle").page("pgGoogle").webedit("edTest")
Set oObject = fn_CreateObjTree(oParent, sObject)
'Adding Sync incase of Page object
If oParent.GetROProperty("micclass") = "Page" Then
oParent.Sync
End If

'Actual value of the object
sActualValue = oObject.getroproperty(sProperty)

'Taking the comparision based on the comparison Type
Select Case sCompareType
Case "=":
If Ucase(Trim(sExpectedValue)) = Ucase(Trim(sActualValue) )Then
Reporter.ReportEvent micPass,"Checkpoint Passed.","Expected value of '"_
&sProperty&"' = '"& sValue&"' found in the application"
End If

Case "<>":
If Ucase(Trim(sExpectedValue)) <> Ucase(Trim(sActualValue) )Then
Reporter.ReportEvent micPass,"Checkpoint Passed.","Expected value of '"_
&sProperty&"' = '"& sValue&"' Not found in the application"
End If
Case ">":
If Ucase(Trim(sExpectedValue)) > Ucase(Trim(sActualValue) )Then
Reporter.ReportEvent micPass,"Checkpoint Passed.","Expected value of '"_
&sProperty&"' = '"& sValue&"' found greater in the application"
End If
Case else
Reporter.ReportEvent micPass,"Error Happened inside 'fn_CheckData'",_
"Invalid Comparator"&"Type = '"&sCompareType&"'"
End Select
'Incase of Error
If Err.Number <> 0 Then
Reporter.ReportEvent micFail, "Error Happened inside 'fn_CheckData' "_
,Err.Description
On Error GoTo 0
'Exit test run
'ExitTest
End If
fn_CheckData = True
End Function


Main Lib :-

Keyword functions in UFT
Main Functions

Code:-

'*******************************************Function*********************************
'Function Name:- fn_SeperateKeywords
'Function Description:- Function to fetch the cell data from Test Sheet
'Input Parameters:- N/A
'Output Parameters:- N/A
''***********************************************************************************
'Function to separate the Keywords
Public Function fn_SeperateKeywords
'Variable Declaration
Dim sTData
Dim sTestStep, sMainWindow, sScreen,sKeyword,sObject,sValue
Dim sParam1, sParam2
'Creating an Array from data in each Row
sTData = fn_ExcelReadRowData(objWS,sTData, Environment("StartRow"))
'Taking the values from the Array sTData
sTestStep = sTData(0)
sMainWindow = sTData(1)
sScreen = sTData(2)
sKeyword = split(sTData(3),":")
sObject = sTData(4)
sValue = sTData(5)
sParam1 = sTData(6)
sParam2 = sTData(7)

'Incase Keyword is SKIPME, UFT will skip that Row Run
If sKeyword(0) <> "SKIPME" Then
Select Case UCase(sKeyword(0))
'Keyword to Launch Application
Case "LAUNCH": fn_Launch sValue,sParam1
'Keyword for adding Wait
Case "WAIT": Wait sValue
'Keyword for adding Comment in output results
Case "COMMENT": Reporter.ReportEvent micInfo, " < Comment given by User:>", sTestStep
'Keyword for working with Input operations
Case "IN": fn_InputData sMainWindow,sScreen,sKeyword(1),sObject, sValue
'Keyword to check property of the Object
Case "CHECK": fn_CheckData sMainWindow,sScreen,sKeyword(1),sObject, sValue,sParam1
'Keyword to Kill running Process
Case "CLOSE": fn_Close sValue
'Incase of mismatch of Keyword
Case Else : Reporter.ReportEvent micFail, "UnKnown Keyword" ,_
"Keyword Not Recognised: " & Cstr(sKeyword)
End Select
End If
'Increasing the Row Counter to fetch data from the next Row in Excel
Environment("StartRow") = Environment("StartRow")+1
End Function

'*******************************************Function*********************************
'Function Name:- fn_CreateObjMain
'Function Description:- Calls fn_CreateObjTree to get the Parent hierarchy of the object
'Input Parameters:- sWindow(Parent Object),sScreen(Child object)
'Output Parameters:- N/A
''***********************************************************************************
Function fn_CreateObjMain(sWindow, sScreen)
Dim sObjects, iObjectCount
Set fn_CreateObjMain = Nothing
'Creating the Parent tree object
'Example:- browser("brGoogle")
Set fn_CreateObjMain = fn_CreateObjTree(fn_CreateObjMain, Cstr(sWindow))
'If Screen value is non empty, then creating the sub tree
'Example:- browser("brGoogle").page("pgGoogle")
If Cstr(sScreen) <> "" Then
'This is required incase of further node inside a sub tree
'Example:- browser("brGoogle").page("pgGoogle").frame("frGoogle")
sObjects = Split(sScreen, ";")
'Looping till the end of nesting on the Screen column
For iObjectCount = 0 To Ubound(sObjects)
'Calling the object tree function
Set fn_CreateObjMain = fn_CreateObjTree(fn_CreateObjMain, Cstr(sObjects(iObjectCount)))
Next
End If
'Incase of errors
If fn_CreateObjMain Is Nothing Then
Reporter.ReportEvent micFail ,"Error Happened inside fn_CreateObjMain",_
"The parent object could not be located" & sWindow & " " & sScreen
End If
End Function

'*******************************************Function*********************************
'Function Name:- fn_CreateObjTree
'Function Description:- Finds the childobject and attaches it to the parent hierarchy
'Input Parameters:- objParent(Parent object),sObjectName(Name of child object
' in object repository)sWindow(Parent Object),sScreen(Child object)
'Output Parameters:- N/A
''***********************************************************************************
Function fn_CreateObjTree(objParent, sObjectName)
'Taking the left value of the objectname
'Example:- Taking 'br' from 'brGoogle' object
Select Case LCase(Left(sObjectName, 2))
Case "br": Set fn_CreateObjTree = Browser(sObjectName)
Case "dl": If objParent Is Nothing Then
Set fn_CreateObjTree = Dialog(sObjectName)
Else
Set fn_CreateObjTree = objParent.Dialog(sObjectName)
End If
Case "pg": Set fn_CreateObjTree = objParent.Page(sObjectName)
Case "rd": Set fn_CreateObjTree = objParent.WebRadioGroup(sObjectName)
Case "ln": Set fn_CreateObjTree = objParent.Link(sObjectName)
Case "im": Set fn_CreateObjTree = objParent.Image(sObjectName)
Case "fr": Set fn_CreateObjTree = objParent.Frame(sObjectName)
Case "ed": Set fn_CreateObjTree = objParent.WebEdit(sObjectName)
Case "bt": Set fn_CreateObjTree = objParent.WebButton(sObjectName)
Case "el": Set fn_CreateObjTree = objParent.WebElement(sObjectName)
Case "ls": Set fn_CreateObjTree = objParent.WebList(sObjectName)

Case Else
Reporter.ReportEvent micFail, "Error Happened inside 'fn_CreateObjTree' " ,_
"Object type is not found: " & sObject
fn_CreateObjTree = False
End Select
End Function



Excel Lib :-

Toolbox view of Functions in UFT
Excel Functions

Code:-

'Delcaring common excel objects
Dim objWB,objWS,objRange
'*******************************************Function*********************************
'Function Name:- ExcelGetRowCount
'Function Description:- Returns No. of rows in the Test Data Sheet
'Input Parameters:- Excel file path,Sheet Name
'Output Parameters:- No. of Rows
''***********************************************************************************
Public Function fn_ExcelGetRowCount(sTestFilename,sSheetName)
'Open Excel Connection
Set objExcel= CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.EnableEvents = False
objExcel.DisplayAlerts = False
'Open WorkBook
Set objWB = objExcel.Workbooks.Open(sTestFilename)
'Refering the Sheet
Set objWS = objWB.Worksheets(sSheetName)
'Taking the Range
Set objRange = objWS.Range("A1").CurrentRegion
fn_ExcelGetRowCount = objRange.Rows.Count
Set objRange = Nothing
End Function


'*******************************************Function*********************************
'Function Name:- fn_ExcelReadRowData
'Function Description:- Reading the Row and returns Each cell in a Array
'Input Parameters:- N/A
'Output Parameters:- N/A
''***********************************************************************************
Public Function fn_ExcelReadRowData(objWS, sData, iRow)
'Variable declaration
Dim iCount
'Fetching the Excel range
Set objRange = objWS.Range("A1").CurrentRegion
ReDim sData(objRange.Columns.Count)
For iCount = 1 To objRange.Columns.Count
sData(iCount-1) = CStr(objRange.Rows(iRow).Columns(iCount).Value)
Next
'Destroying object
Set objRange = Nothing
fn_ExcelReadRowData = sData
End Function


Recovery Scnearios:-

ApplicationCrash:- we have created Recovery Scenario to handle exceptional crash of our AUT.

How to create Recovery Scenario:-

  • Resources -> Recovery Scenario Manager -> New Scenario

Trigger Event:– Is crashing of any browser (Chrome or IE)

Trigger Event in Recovery Scenario
Trigger Event

RecoveryOpeations:– Close the Browser window

Recovery Operation in UFT
Recovery Operation

Post-Recovery Operation :- Re-run the test 

Post recovery Operation in Recovery Scenarion
Post-Recovery Operation UFT

Understand Keyword Driven Framework by using UFT – (Part 1)
Understand Keyword Driven Framework by using UFT – (Part 3)

ufthelp

Leave a Reply

Your email address will not be published. Required fields are marked *