UFT Function to click link inside a WebTable

Aneejian · Nov 14, 2012 · 5 mins read.

UFT Function to click link inside a WebTable

In this post, we will discuss how to click a link inside a web table based on value present in the same or another column in the same row using UFT.

Introduction

While we do automation testing using UFT on web applications, we face scenarios where we must click a link inside a web table that holds a lot of items.

In certain scenarios, these links could be dynamic based on several factors like filters applied, permissions, the region of access, etc. Such scenarios make it difficult to click a specific link.

We have a function that works with UFT which can click a link based on link text or based on the text in another column of a web table.

As an example, let us consider the following web table with some student data.

Sample Web Table

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

Scenario

As you can see, each student listed in the table has a link to their address and progress report. They all have the same properties and we cannot distinguish them with the links alone.

In this scenario, we are asked to click the link against the student Mahesh? We can do it with the below function.

The Function

Overview

Name
SearchTextAndClickLink
Description
Function to click a link in a web table based on value present in same or another column in the same row.

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

LinkColum
Specify the column where the link resides in the WebTable as long.

e.g. 3

'------------------------------------------------------------------------------------------------------------------------------------
'Function Name                    : SearchTextAndClickLink(ByVal TestObject, ByVal SearchText, ByVal SearchTextInColumn, ByVal LinkColumn)
'Function Description             : Function to click a link in a web table based on value present in same or another column in the same row.
'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
'                                   LinkColum:- Specify the column where the link resides in the WebTable as long. eg: 3
'------------------------------------------------------------------------------------------------------------------------------------
Function SearchTextAndClickLink(ByVal TestObject, ByVal SearchText, ByVal SearchTextInColumn, ByVal LinkColumn)
 '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 web table
    TotalRows = TestObject.RowCount
    'looping in the web table until a match is found
    For CRow = 1 to TotalRows
     'checking whether Searched Text is found
     If SearchText = Trim(TestObject.GetCellData(CRow, SearchTextInColumn)) Then
      'saving the row where the value is found to a variable
      FoundRow = CRow
      'setting MatchFound as true
      MatchFound = TRUE
      Exit For
     End If
    Next
    'in case a match is found
    If MatchFound Then
     'setting the object link
   Set LinkObject = TestObject.ChildItem(FoundRow, LinkColumn, "Link", 0)
   'clicking the link
   LinkObject.Click
   'reporting successful link click
   Reporter.ReportEvent micPass, "Specified Link Clicked Successfully.", "Successfully clicked the link " & LinkObject.GetROProperty("Text") & "."
   'in case match is not found
       ElseIf Not MatchFound Then
    'reporting error if specified text is not found in the web table
   Reporter.ReportEvent micFail, "Cannot Find Specified Text", "Cannot find the text " & SearchText & " in the object specified in function call."
    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

The code below, when used in UFT, will call the function so that it clicks the address link against the student Mahesh.

Set HomeTable = Browser("Browser").Page("Page").WebTable("Sl. No")
Call SearchTextAndClickLink(HomeTable, "Mahesh", 2, 3)

We have another article which details how to check the presence of an item in a web table. Read more by clicking here.