UFT Function to check the presence of an item in a WebTable

Aneejian · Nov 17, 2012 · 6 mins read.

UFT Function to check the presence of an item in a WebTable

In this post, we will discuss how to check the presence of an item in a web table using Micro Focus UFT (formerly Quick Test Professional or QTP).

We will validate the presence or absence of an item in a web table using a function. The condition specified in the function call is validated to generate results.

Sample Web Table

Sl. No Student Name Status
1 Rahul Passed
2 Sameer Passed
3 Austin Passed
4 Libin Passed
5 Mahesh Passed
6 Vineeth Passed
7 Hareesh Passed
8 Praveen Passed
9 Arun Passed
10 Anu Passed

So let’s take an example of a web table which holds the name of students who have passed in an exam. Let’s say Mahesh passed the exam and unfortunately Basheer failed in it. Now we have to test whether the web table contains Mahesh and that it does not hold Basheer.

The Function

Overview

Name
SearchItemInWebTable
Description
Function to validate the presence of an item in a web table.

Parameters

TestObject
Specify the WebTable.

e.g. Browser("-----").Page("-----").WebTable("-----")

SearchText
Specify the text to search in the WebTable.

e.g. "Cruises"

SearchTextInColumn
Specify the column in which text is to be searched as long.

e.g. 2

ExpectedResult
Specify TRUE or FALSE, TRUE if the item should exist in the WebTable and FALSE if it should not.

e.g. TRUE

'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'Function Name                    : SearchItemInWebTable(ByVal TestObject, ByVal SearchText, ByVal SearchTextInColumn, ByVal ExpectedResult)
'Function Description             : Function to validate the presence of an item in a webtable
'Data Parameters                  : TestObject:- Specify the WebTable. eg: Browser("-----").Page("-----").WebTable("-----")
'                                   SearchText:- Specify the text to search in the WebTable. eg: "Cruises"
'                                   SearchTextInColumn:- Specify the column in which text is to be searched as long. eg: 2
'                                   ExpectedResult:- Specify TRUE or FALSE, TRUE if the item should exist in the webtable and FALSE if it should not
'--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function SearchItemInWebTable(ByVal TestObject, ByVal SearchText, ByVal SearchTextInColumn, ByVal ExpectedResult)
   'in case of any errors
   On Error Resume Next
   'checking whether the object exist
   ObjectExist = TestObject.Exist(10)
   If ObjectExist Then
    'setting MatchFound as false
    MatchFound = FALSE
    'finding the total number of rows in the webtable
    TotalRows = TestObject.RowCount
    'looping in the webtable until a match is found
    For CRow = 1 to TotalRows
     'checking whether Searched Text is found
     If SearchText = Trim(TestObject.GetCellData(CRow, SearchTextInColumn)) Then
      'setting MatchFound as true
      MatchFound = TRUE
      Exit For
     End If
    Next
    If ExpectedResult Then   
     'in case a match is found
     If MatchFound Then
      'reporting pass if specified item is found in the webtable
    Reporter.ReportEvent micPass, "Specified Item Found", "Specified item " & SearchText & " is present in the web table."
    'setting function value
    SearchItemInWebTable = TRUE
    'in case match is not found
     ElseIf Not MatchFound Then
    'reporting error if specified item is not found in the webtable
    Reporter.ReportEvent micFail, "Cannot Find Specified Item", "Cannot find the specified item " & SearchText & " in the web table."
    'setting function value
    SearchItemInWebTable = FALSE
     End If
    ElseIf Not ExpectedResult Then
     'in case a match is found
     If MatchFound Then
      'reporting error if specified item is found in the webtable
    Reporter.ReportEvent micFail, "Specified Item Found", "Specified item " & SearchText & " is present in the web table contrary to what was expected."
    'setting function value
    SearchItemInWebTable = FALSE
    'in case match is not found
     ElseIf Not MatchFound Then
    'reporting error if specified item is not found in the webtable
    Reporter.ReportEvent micPass, "Cannot Find Specified Item", "Cannot find the specified item " & SearchText & " in the web table as expected."
    'setting function value
    SearchItemInWebTable = TRUE
     End If             
    End If
   ElseIf Not ObjectExist Then
   'reporting error if the specified object is not found
  Reporter.ReportEvent micFail, "Cannot Find The Object", "Object specified in function call cannot be found."
   End If
End Function

Usage

'setting the web table as the test object for the function
Set MyWebTable = Browser("QTP Function to Click").Page("QTP Function to Click").WebTable("Sl. No")
'calling the function to check whether student Mahesh is present in the web table
Call SearchItemInWebTable(MyWebTable, "Mahesh", 2, TRUE)
'calling the function to validate the absence of student Basheer in the web table
Call SearchItemInWebTable(MyWebTable, "Basheer", 2, FALSE)
'calling the function to check whether student Basheer is present in the web table
Call SearchItemInWebTable(MyWebTable, "Basheer", 2, TRUE)
'calling the function to validate the absence of student Mahesh in the web table
Call SearchItemInWebTable(MyWebTable, "Mahesh", 2, FALSE)
'alternate usage of the function. Function returns TRUE or FALSE for the condition
FoundPassedStudent = SearchItemInWebTable(MyWebTable, "Mahesh", 2, TRUE)
'custom error reporting
If FoundPassedStudent Then
 Reporter.ReportEvent micPass, "Found Passed Student", "Student Mahesh was found in the table of passed students."
End If

Possible use of this function

  • After adding a new employee to your human resource management system, you can check whether he is listed in search results.
  • After deleting an employee you can check whether they come up in the search result.

We have another article which details on how to click a link inside a web table. Read more by clicking here.