Part 3: QTP/UFT beginner tutorial-creating a dynamic click link function from scratch!

Hi. So last time when we left off, we created a dynamic link that clicked on an any link object on any page using only two properties. It used a unique property of your choosing as well as an index. I recently received feedback asking for a function that can take any number of properties and can be dynamically used for any object property and it's value. This is what I call a super click link function. It will save you tons and tons of time and can be used for any application and any scenario. Below, I created three identical links to show you how this super function will work.

Link1

Link1

Link1

The final code: If you want to copy the code directly, I pasted all of the code below. Just copy all of the code right in your script, and call the identify fnIdentifyBrowserPage function, and the fnIdentifyBrowserPage with the property names and values following the syntax name:=Link1,Index:=1,innertext:=Link1 If you would like the full explanation of how I came up with this function, I will paste the steps below.

code--------------------------------------------------------------------------------------------------

fnIdentifyBrowserPage
 
fn_click_anylink "name:=Link1,Index:=1,innertext:=Link1"
 
 Function  fn_click_anylink(strProperties)
 Set objBrowserPage = Browser("hwnd:="& hwindow).Page("title:="& page_title)
     arrStrProperties = Split(strProperties, ",")
     arrCount = uBound(arrStrProperties) + 1
    If arrCount = 1 Then
        objBrowserPage.Link(strProperties).Click
        ElseIf arrCount = 2 Then    
        strProperty1 = arrStrProperties(0)
        strProperty2 = arrStrProperties(1)
        objBrowserPage.Link(strProperty1, strProperty2).highlight
        objBrowserPage.Link(strProperty1, strProperty2).Click
        ElseIf arrCount = 3 Then
        strProperty1 = arrStrProperties(0)
        strProperty2 = arrStrProperties(1)
        strProperty3 = arrStrProperties(2)
        objBrowserPage.Link(strProperty1, strProperty2, strProperty3).highlight
        objBrowserPage.Link(strProperty1, strProperty2, strProperty3).Click
    End If
 End Function

Function fnReplaceRegExp(StrLabel)
     StrLabel = replace(StrLabel, "|", "\|")
     fnReplaceRegExp = StrLabel
 End Function

 Function fnIdentifyBrowserPage()
     hwindow = Browser("creationTime:=0").GetROProperty("hwnd")
     page_title = Browser("hwnd:=" & hwindow).Page("title:=.*").GetROproperty("title")
     page_title = fnReplaceRegExp(page_title)
 End Function

code--------------------------------------------------------------------------------------------------

So how did I come up with this super function? Let's see the mastery below...

1. So we wanted to create a function that can take any number of property names and values. Since vbscript doesn't have the option to create optional variables, I have created a workaround. As you can see from the code above, I am calling my function with the syntax

fn_click_anylink "name:=Link1,Index:=1,innertext:=Link1"

This is similar to the vbs syntax when calling a number of arguments. "name:=Link1", "inntertext:=Link1", and so on.. The only difference is I'm calling all of my parameters as one string.  Technically, this is just one parameter that we'll be breaking into multiple parameters for this super function.

We first create an object with our dynamic page and browser using

Set objBrowserPage = Browser("hwnd:="& hwindow).Page("title:="& page_title)

I went over this functionality in tutorial 2. Tutorial 2  

2. Then I take the string that I'm passing as the parameter, and I split this string into an array of my properties using the comma.

arrStrProperties = Split(strProperties, ",")

I am creating an array arrStrProperties by splitting the strProperties value by the comma. Since my paremeter strProperties is  "name:=Link1,Index:=1,innertext:=Link1" It will create an array of 3 separate values.

arrStrProperties (0)   =   name:=Link1 

arrStrProperties (1)   =   Index:=1

arrStrProperties (2)    =   innertext:=Link1

Since we want this function to be dynamic and pass as many properties and whichever values we need, we want to dynamically get a count of how many properties we're passing.

arrCount = uBound(arrStrProperties) + 1

Here, we are counting how many properties we are setting for this function. If you set a message box MsgBox arrCount, it will return the value 3.

3. Now remember when we created the object objBrowserPage = Browser("hwnd:="& hwindow).Page("title:="& page_title) we can now set this object to click the link within our function.

ex.

objBrowserPage.Link("name:=1").Click

objBrowserPage.Link("name:=1", "index:=1").Click

The cool thing about creating this object, is we can pass any number of properties and value in it to click our link. So now we are going to set the condition stating that if there is one property, run the code like this..

array value 0 = property1

objBrowserPage.Link(property1).Click

If there is two properties, run the code like this...

array value 0 = property1

array value 1 = property2

objBrowserPage.Link(property1, property2).Click

if there are three properties, run the code like this...

array value 0 = property1

array value 1 = property2

array value 3 = property3

objBrowserPage.Link(property1, property2, property3).Click

You can create as many conditions as you need for all of any number of properties you may need. Every application is different and some many need only one property to identify an object, and some may need more. See example below:

If arrCount = 1 Then
        objBrowserPage.Link(strProperties).Click
        ElseIf arrCount = 2 Then    
        strProperty1 = arrStrProperties(0)
        strProperty2 = arrStrProperties(1)
        objBrowserPage.Link(strProperty1, strProperty2).highlight
        objBrowserPage.Link(strProperty1, strProperty2).Click
        ElseIf arrCount = 3 Then
        strProperty1 = arrStrProperties(0)
        strProperty2 = arrStrProperties(1)
        strProperty3 = arrStrProperties(2)
        objBrowserPage.Link(strProperty1, strProperty2, strProperty3).highlight
        objBrowserPage.Link(strProperty1, strProperty2, strProperty3).Click
    End If

So currently, we can take up to 3 different properties with their values. If you need more, just follow the syntax above and modify this function based on your needs. You can use any properties such as html id, innertext, xpath, index, and anything else you may desire.

Now if you call your function like below, it should highlight and click on the second link above.

fnIdentifyBrowserPage
 
fn_click_anylink "name:=Link1,Index:=1"

You can also call it with two properties fn_click_anylink "name:=Link1,Index:=1" or even with one property fn_click_anylink "name:=Link1" This gives us the option to use any amount of properties we need while keeping the values and properties dynamic.

 

Final Code-----------------------------------------------------------------

fnIdentifyBrowserPage
 
fn_click_anylink "name:=Link1,Index:=1,innertext:=Link1"
 
 Function  fn_click_anylink(strProperties)
 Set objBrowserPage = Browser("hwnd:="& hwindow).Page("title:="& page_title)
     arrStrProperties = Split(strProperties, ",")
     arrCount = uBound(arrStrProperties) + 1
    If arrCount = 1 Then
        objBrowserPage.Link(strProperties).Click
        ElseIf arrCount = 2 Then    
        strProperty1 = arrStrProperties(0)
        strProperty2 = arrStrProperties(1)
        objBrowserPage.Link(strProperty1, strProperty2).highlight
        objBrowserPage.Link(strProperty1, strProperty2).Click
        ElseIf arrCount = 3 Then
        strProperty1 = arrStrProperties(0)
        strProperty2 = arrStrProperties(1)
        strProperty3 = arrStrProperties(2)
        objBrowserPage.Link(strProperty1, strProperty2, strProperty3).highlight
        objBrowserPage.Link(strProperty1, strProperty2, strProperty3).Click
    End If
 End Function

Function fnReplaceRegExp(StrLabel)
     StrLabel = replace(StrLabel, "|", "\|")
     fnReplaceRegExp = StrLabel
 End Function

 Function fnIdentifyBrowserPage()
     hwindow = Browser("creationTime:=0").GetROProperty("hwnd")
     page_title = Browser("hwnd:=" & hwindow).Page("title:=.*").GetROproperty("title")
     page_title = fnReplaceRegExp(page_title)
 End Function

------------------------------------------------------------------------------

Make sure to follow the syntax for calling the function properly. You did it!!! Now you have a super function for clicking any link on absolutely any application!!!!!!!!!!!!!!!!!!! Post questions and comments below if you liked this tutorial.

 

Pin It on Pinterest

Clef two-factor authentication