Understand Keyword Driven Framework by using UFT – (Part 2) – User friendly Tech help

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.
n
nTest Scenario:- 
nWe would be using the following scenario to exemplify our Keyword Framework
n1.Launch web browser 
n2.Search “uftHelp” on “Google”
n3.Open the first link of search Result
n4.Verify that www.uftHelp.com, page is opened
n5.Search any given value on this Tutorial website
n6.Close the Application 
n
Solution Explorer:-
nThis is how our Framework Solution would look like.

n

n

n

n

n

n

n

n

Solution Explorer in UFT

n

TC_01_Search :-Name of our Test Case
n
nWe have taken 2 input parameters:–
nFilePath:- Path of our Excel sheet containing Keywords
nSheetName:- Keyword sheet in the Excel

n

n

n

n

n

n

n

n

Input Parameters 

n

How to set input Parameters?

n

    n

  • Select the Test Case(TC_01_Search) in Solution Explorer
  • n

  • View -> Properties
  • n

  • Open “Parameters Tab” -> Add -> Add Input Parameter
  • n

  • Fill the Name and Default Value , Leave Type as String 
  • n

n

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

n

    n

  • We tried to add appropriate comments to explain the code,Please contact us or add comments below for any clarification.
  • n

  • For clear comprehension of the code try to run the script in debug mode (F11).
  • n

n

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

n

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") )
'Output Parameters:- N/A
''***********************************************************************************
Function fn_CheckData(sMainWindow,sScreen,sProperty,sObject, sValue,sParam1)
Dim sExpectedValue,sActualValue,sCompareType
'Expected value to be comparedsExpectedValue = sValue
'Comparison factor like '=',''sCompareType = sParam1
On Error Resume NextErr.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

n


Main Lib :-

n

n

n

n

n

n

n

n

Main Functions

n

Code:-
n

n

'*******************************************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

n



Excel Lib :-

n

n

n

n

n

n

n

n

Excel Functions

n

Code:-
n

n

'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

n

Recovery Scnearios:-

n

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

n

    n

  • Resources -> Recovery Scenario Manager -> New Scenario
  • n

n

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

n

n

n

n

n

n

n

n

Trigger Event

n

RecoveryOpeations:– Close the Browser window

n

n

n

n

n

n

n

n

Recovery Operation

n

Post-Recovery Operation :- Re-run the test 

n

n

n

n

n

n

n

n

Post-Recovery Operation UFT

n

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

Was this article helpful?
YesNo

Similar Posts