UFT Function to Select an item from a WebList or WinComboBox

. 3 mins read

UFT Function to Select an item from a WebList or WinComboBox

UFT/QTP Function to select an item form WebList or WinComboBox. This function is capable of selecting a list item specified in the function call and report pass or fail based on the result. To create and test this function, we have made use of the sample WebList given below and WinComboBox from HP QTP Flight Application.

Sample WebList
Select a destination:

The Function

Overview

Name
SelectItemFromList
Description
UFT function to Select an item from a WebList or WinComboBox

Parameters

TestObject
Specify the WebList or WinComboBox.
ListItem
Specify the list item to be selected from the WebList.
'--------------------------------------------------------------------------------------------------------------------------------------------
'Function Name        : SelectItemFromList (ByVal TestObject, ByVal ListItem)
'Function Description : Function to Select an item from a WebList or WinComboBox
'Data Parameters      : TestObject:- Specify the WebList or WinComboBox. eg for WebList: Browser("-----").Page("-----").WebList("-----")
'                       eg for WinComboBox:  Window("-----").WinComboBox("-----")
'                       ListItem:- Specify the list item to be selected from the WebList. eg: "Cruises"
'--------------------------------------------------------------------------------------------------------------------------------------------
Function SelectItemFromList (ByVal TestObject, ByVal ListItem)
 Rem: to handle any unexpected error
 On Error Resume Next
 MatchFound = FALSE
 SelectItemFromList = FALSE
 ValidObject = FALSE
 Rem: checking whether the TestObject in the AUT
 ObjectExist = TestObject.Exist(10)
 If ObjectExist Then
  Rem: finding the TestObject class
  Select Case TestObject.GetROProperty("micclass")
   Case "WebList"
    Rem: getting the number of items present in the TestObject
    ListItemsCount = TestObject.GetROProperty("items count")
    ListStart = 1
    ListEnd = ListItemsCount
    ValidObject = TRUE
   Case "WinComboBox"
    Rem: getting the number of items present in the TestObject
    ListItemsCount = TestObject.GetROProperty("items count")
    ListStart = 0
    ListEnd = ListItemsCount - 1
    ValidObject = TRUE
   Case Else
    Rem: reporting error in case the TestObject specified in function call is not WebList or WinComboBox
    Reporter.ReportEvent micFail, "Object Error", "Object '" & TestObject.ToString & "' specified in function call is not a WebList or WinComboBox."
  End Select
  If ValidObject Then
   For iList = ListStart to ListEnd
    CurrentListItem = TestObject.GetItem(iList)
    If StrComp(Trim(CurrentListItem), Trim(ListItem), vbTextCompare) = 0 Then
     MatchFound = TRUE
     TestObject.Select CurrentListItem
     Item_Selected = TestObject.CheckProperty("selection", CurrentListItem, 1000)
     Exit For
    End If
   Next                
   If MatchFound And Item_Selected Then
    SelectItemFromList = TRUE
    Reporter.ReportEvent micPass, "Specified List item selected.", "Specified item '" & ListItem & "' was selected from '" & TestObject.ToString & "'."
   ElseIf Not MatchFound Then
    Reporter.ReportEvent micFail, "List item selection failed.", "Specified item '" & ListItem & "' was not selected from '" & TestObject.ToString & "'."
   End If
  End If
 ElseIf Not ObjectExist Then
  Rem: reporting error if TestObject is not found in AUT
  Reporter.ReportEvent micFail, "Cannot Find The Object", "Object specified in function call cannot be found."
 End If
End Function

Usage

Using for Web List

Rem: Using function for WebList

Call SelectItemFromList(WebListTestObject, "-Select-")
Rem: Result:- Specified item '-Select-' was selected from '[ select ] list'.

Call SelectItemFromList(WebListTestObject, "Dublin")
Rem: Result:- Specified item 'Dublin' was selected from '[ select ] list'.

Call SelectItemFromList(WebListTestObject, "Dublins")
Rem: Result:- Specified item 'Dublins' was not selected from '[ select ] list'.

Call SelectItemFromList(InvalidWebListTestObject, "Dublin")
Rem: Result:- Object '[ Aneejian] Web page' specified in function call is not a WebList or WinComboBox.

Using for WinComboBox

Rem: Using Function for WinComboBox

Call SelectItemFromList(WinComboBoxTestObject, "Zurich")
Rem: Result:- Specified item 'Zurich' was selected from '[ Fly From: ] list'.

Call SelectItemFromList(WinComboBoxTestObject, "Denver")
Rem: Result:- Specified item 'Denver' was selected from '[ Fly From: ] list'.

Call SelectItemFromList(WinComboBoxTestObject, "Zuricsh")
Rem: Result:- Specified item 'Zuricsh' was not selected from '[ Fly From: ] list'.

Call SelectItemFromList(InvalidWinComboBoxTestObject, "Zurich")
Rem: Result:- Object '[ Button ] button' specified in function call is not a WebList or WinComboBox.

Software Testing
Drag and drop items from windows explorer to application UFT
08 Aug 2016

Subroutine to drag and drop item from windows explorer to application using UFT or QTP. By default this feature is not yet available in QTP or UFT.

Software Testing
Optional Steps in UFT
27 May 2015

Master the use of Optional Steps in UFT or QTP. Understand what are optional steps.

Software Testing
Reporting Modes in UFT
21 May 2015

Reporter object in UFT has different reporting modes. This post discusses about various mode available in reporter object.