UFT Function to click link inside a WebTable

. 4 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

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.

Software Testing
UFT Function to check the presence of an item in a WebTable
17 Nov 2012

UFT/QTP function to validate the presence or absence of an item in a WebTable. Condition specified in the function call is validated to report result.

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.