QTP function for validating the items in Dropdown object
Here is an awesome function to validate the dropdown list items by QTP. This function will works even if your “WebList” object is been identified as “WebElement” by object spy.
1. Copy Paste this piece of code in your function library (For example Test.qfl)
Function DropdownValidateList(ByRef webEdit, ByRef listOfValues)
itemValues = Split(listOfValues, ";" )
ErrorDetected = False
webEdit.Set ""
webEdit.click
ScreenValues = ""
Set myDeviceReplay = CreateObject("Mercury.DeviceReplay")
myDeviceReplay.PressKey 209
wait 0,100
myDeviceReplay.PressKey 201
wait 0,100
myDeviceReplay.PressKey 201
wait 0,100
myDeviceReplay.PressKey 201
wait 0,100
myDeviceReplay.PressKey 201
wait 0,100
set myDeviceReplay = Nothing
For Each targetValue In itemValues
webEdit.click
screenItem = webEdit.Object.Value
'skip combo box blank field for non mandatory web edits.
If screenItem = "" Then
WshShell.SendKeys "{DOWN}"
wait 0,100
screenItem = webEdit.Object.Value
else
' copy all the screen values for error reporting.
ScreenValues = ScreenValues & screenItem & ";"
End If
If targetValue <> screenItem Then
ErrorDetected = True
Reporter.ReportEvent micFail, "Validating List", "List value does not match screen value. Screen: '" & screenItem & "' Expected: '" & targetValue
End If
LastScreenItem = screenItem
webEdit.click
WshShell.SendKeys "{DOWN}"
wait 0,100
screenItem = webEdit.Object.Value
Next
If ErrorDetected = False Then
'check to make sure we are at the end of the list or error.
screenItem = webEdit.Object.Value
If lastScreenItem = screenItem Then
Reporter.ReportEvent micPass, "DropdownValidateList " & webEdit.ToString, "No errors detected validating list elements: " & listOfValues
else
Reporter.ReportEvent micFail, "Validating List", webEdit.ToString, "Screen has more values than passed list: '" & listOfValues
End If
else
Reporter.ReportEvent micFail, "Validating List", &webEdit.ToString, "List value does not match screen values check order and values. Screen: '" & screenValues & "' Expected: '" & listOfValues
end if
'set WshShell = Nothing
End Function
RegisterUserFunc "WebEdit", "DropdownValidateList", "DropdownValidateList", True
RegisterUserFunc "WebList", "DropdownValidateList", "DropdownValidateList", True
RegisterUserFunc "WebElement", "DropdownValidateList", "DropdownValidateList", True
2. Associate the Test.qfl with your current test
3. Use the function J
Dropdown: Country
Dropdown Items: (first value blank), Andorra, Brazil, Canada
Browser("IE").Page("Example").WebElement("Country").
DropdownValidateList ";Andorra;Brazil;Canada"
Works great! Thanks for the such a advanced level functions
ReplyDelete