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

"Today is truly an awesome day for all my beginner automation champions! Today, I am going to show you how to create a dynamic function to click on any link on a page using QTP/UFT. I've added some test links below so you can practice as you move through this tutorial. You can apply this function method for other object classes such as web buttons, web edits, and anything else you may need to create.

Link1

Link2

Link3

First. The syntax to a function can be seen below:

Function FunctionName(parameter1, parmeter2, parameter2...)

The code for the function

End Function

Before we start, you may be wondering a couple of things. How do we find how many parameters we need? And which parameters do we use? So let's think about a few things. What is our function supposed to do? It's supposed to click on a link. To click on a link, QTP/UFT needs a unique property to identify the object. Also, will this function be reused for other scenarios? Can there be several links on a page with the same properties? These are all things we have to consider.

1. Let's look at the 3 links. Each one of them has a unique identifying property "name". This will be one of our parameters. I also like to add an Index as a parameter in case we need to reuse this function in a situation where there are more than one objects with the same name.

2. Let's set up our function with these parameters. You can name your parameters anything you like, but it's good to have a standard way of coding. I am going to name all my parameters with a "p" in front of the name. pName and pIndex. See example below.

Function Click_Any_Link(pName, pIndex)
 

End Function

3. Next, we need the code that our function will execute. In this case, we want to click on the link so we have to use standard descriptive programming to do this. First I used a unique name property for the browser, then the title, and then finally called for the link. When creating a dynamic function, you don't want to hard code values. Instead of setting the name of the link to "Link1", I wrote "name:="& pName I am setting the name to my parameter pName. This way, you can just change the name of the link, and the function will adjust. I did the same exact thing for the index "Index:="& pIndex Take a look at the example below.

Function Click_Any_Link(pName, pIndex)
    Browser("name:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! \|").Page("title:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! \|").Link("name:="& pName, "Index:="& pIndex).Click
End Function

4. Lastly, we just need to call the function with the specific parameters, and the function will do the work for us. Call the function using Click_Any_Link pName, pIndex Just replace the pName with the name of the link you want to call. And replace the Index with the index of the link that you want to call. I can now call any of these links using the same function! The final function and code should look like the example below. If done correctly, the links will take you to a 404 page 🙂

Click_Any_Link "Link1", 0

Function Click_Any_Link(pName, pIndex)
    Browser("name:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! \|").Page("title:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! \|").Link("name:="& pName, "Index:="& pIndex).Click
End Function

You are awesome! Great job on creating this simple yet awesome function. On the next tutorial, I will expand on this function and create a way for this function to dynamically identify the browser and page for us. This way, you will be able to click on any link no matter what page you're on!

 

 

  • TestEngineerX

    Hello, great site and love the advanced content! I did have a question about this function. I am working on 3 different applications, meaning that the browser and page is always changing. We use the html id property to identify our elements. How can I use your Click_Any_Link function for this?
    Btw, how come you’re not following VBscript’s recommended Hungarian notation. Isn’t that the standard?

    • Hey TestEngineerX! Thank you so much for your kind words of support! When you are working on 3 different applications, it becomes a bit of a different story. Clearly, you want your function to dynamically adjust to the different browsers and pages as well. This part is a bit more complicated and I wanted to keep this tutorial very basic. At my job, we have several applications as well and we identify the browser and title based on what window is currently open. We identify the hwindow for the browser and then based on the hwindow, we retrieve the page title. This way, no matter what app or page is open, the function will perform it’s job as long as you are identifying the object based on it’s unique property.
      Example:
      hwindow = Browser(“creationTime:=1”).GetROProperty(“hwnd”)
      msgbox hwindow

      page_title = Browser(“hwnd:=” & hwindow).Page(“name:=.*”).GetROproperty(“title”)
      msgbox page_title

      So based on the creation time, we can grab the hwindow value, then grab the page title using the hwindow value.

      Also, if most of your objects are identified in your app using html id, then you can set up the Click_Any_Link function to adjust for the html id instead of the name property I’m using.

      Example:
      Function Click_Any_Link(pHtmlid, pIndex)
      Browser(“name:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! \|”).Page(“title:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! \|”).Link(“html id:=”& pHtmlid, “Index:=”& pIndex).Click
      End Function

      In our application, most of our links are coded with the name property and not html id.

      And yes. I should be using Hungarian notation. Again, I am trying to keep the tutorial very basic and wanted to focus more on writing a simple function for those just starting with QTP/UFT. Also, every company uses different coding standards and naming conventions. It’s good to use the standardized naming conventions, but that isn’t always the case 🙂

      Does this help?

    • QTPtutorialnet

      Hey TestEngineerX! Thank you so much for your kind words of support! When you are working on 3 different applications, it becomes a bit of a different story. Clearly, you want your function to dynamically adjust to the different browsers and pages as well. This part is a bit more complicated and I wanted to keep this tutorial very basic. At my job, we have several applications as well and we identify the browser and title dynamically based on what window is currently open. We identify the hwindow for the browser and then based on the hwindow, we retrieve the page title. This way, no matter what app or page is open, the function will perform it’s job as long as you are identifying the object based on it’s unique property.
      Example:
      hwindow = Browser(“creationTime:=1″).GetROProperty(“hwnd”)
      msgbox hwindow

      page_title = Browser(“hwnd:=” & hwindow).Page(“name:=.*”).GetROproperty(“title”)
      msgbox page_title

      So based on the creation time, we can grab the hwindow value, then grab the page title using the hwindow value.

      You can also simply and easily set it up this way:

      Browser(“name:=.*”).Page(“title:=.*”)

      It will use any browser and page that is open and click on the link.

      Also, if most of your objects are identified in your app using html id, then you can set up the Click_Any_Link function to adjust for the html id instead of the name property I’m using.

      Example:
      Function Click_Any_Link(pHtmlid, pIndex)
      Browser(“name:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Page(“title:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Link(“html id:=”& pHtmlid, “Index:=”& pIndex).Click
      End Function

      In our application, most of our links are coded with the name property and not html id.

      And yes. I should be using Hungarian notation. Again, I am trying to keep the tutorial very basic and wanted to focus more on writing a simple function for those just starting with QTP/UFT. Also, every company uses different coding standards and naming conventions. It’s good to use the standardized naming conventions, but that isn’t always the case. I will continue expanding on this tutorial and add more fun packed parts to it.

      Does this help?

      • TestEngineerX

        Oh okay, that makes sense. I guess what was confusing for me was the fact that you called the function “Click_Any_Link”. As the name implies, I thought it was going to click ANY link, but that is not the case. It’s more of a “funClickLinkForMyApplication”. The main drawback that I see here is that you have to redesign this function for every single application. However, in my understanding of coding, a function should be reusable, I think that is the purpose? So if I have 3 applications, I will need 3 functions. One using html id, one using name, and another one using name and html id. With that logic, what is the point of even designing this function, would it be cleaner to just do:

        Browser().Page(). Link(pHtmlid, pIndex).Click()

        Now I have 1 line of code and you have 3 lines of code, but we do exactly the same thing. In fact, I would argue that this method is even cleaner. because now instead of writing another function for name property, i can do:

        Browser().Page(). Link(name).Click()

        While you have to write another 3 lines of code to make another function work, allowing for higher chance of human mistake.

        Thoughts?

        • QTPtutorialnet

          I can see how you would think it’s confusing. I will make a few follow up parts to this tutorial that will help users such as yourself create a click link for any application, any browser, and any object property. How does that sound? And also, yes it does have less lines of code, but you’re not taking the fuureu into consideration. Currently, there will only be one line of code which is less than the initial function I showed you. Let’s say you have three separate links you have to click on the same page. Your script will look like this:

          fnClickAnyLink Name1, 0
          fnClickAnyLink Name2, 1
          fnClickAnyLink Name3, 2

          Instead of what you’re suggesting:

          Browser().Page(). Link(name1).Click()

          Browser().Page(). Link(name2).Click()

          Browser().Page(). Link(name3).Click()

          And let’s say that these three links are tested separately in 3 different scripts. So link 1 is in script 1, link2 is in script 2 and so on…
          And finally, let’s say that the developers changed the link name property to something different. Now you have to go in three separate scripts and change that code for each of your click link lines, where as in I can go into my function, change it once and be done with the work. This also applies if the developers change the browser name or page. That makes a lot of work for you instead of changing it once in a function. But yes, I completely understand what you’re saying with the dynamic function. Like I mentioned, I wanted to create the most basic function from scratch that any beginner can understand. But thanks to your feedback, I will continue this post with a few separate parts that will evolve the function into a very advanced function that will click on absolutely any link. I like the fact that you ask questions and challenge the logic. Please keep doing so as I love to help out, learn more myself, and exchange ideas regarding automation!

          • QTPtutorialnet

            Hey, I created part 2 of this tutorial. In it I included a way to dynamically select which property I want to use for the link. Html id, name, innertext, and so on… Also, I covered dynamically identifying the browser and page so that you can click on any link. Not just the one on a specific page. Let me know what you think: https://www.qtptutorial.net/part-2-qtpuft-beginner-tutorial-creating-a-dynamic-click-link-function-from-scratch/

          • TestEngineerX

            Yea, I completely agree about the higher maintenance required for each of the clicks if the property changes. And I also love to challenge and ask questions haha. This is how I would handle it.

            ‘Now there is only 1 instance of this in my whole framework

            Set objBrowserPage = Browser(“index:=0”).Page(“title:=.*”)

            Dim strProp1, strProp2, strProp3

            ‘Now all of the properties have only 1 instance in the entire framework
            strProp1 = “html id:=x”

            strProp2 = “name:=y”

            strProp3 = “title:=z”

            If there was a dynamic click link function, I would use that for ALL 3 of my applications. However, since there is not, I would do this.

            objBrowserPage.Link(strProp1).CLick

            objBrowserPage.Link(strProp2).CLick

            objBrowserPage.Link(strProp3).CLick

            Now I can click any link with 1 line of code. The property lives in 1 place and I don’t have to create a new function every time.

            Compare it to the other way —————————————————————–

            ‘I need 1 function to use name
            Function Click_Any_Link(pName, pIndex)
            Browser(“name:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Page(“title:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Link(“name:=”& pName, “Index:=”& pIndex).Click
            End Function

            ‘I need 1 function to use html id

            Function Click_Any_Link(pHtmlId, pIndex)
            Browser(“name:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Page(“title:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Link(“name:=”& pName, “Index:=”& pIndex).Click
            End Function

            ‘I need 1 function to use title

            Function Click_Any_Link(pTitle, pIndex)
            Browser(“name:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Page(“title:=QTP/UFT beginner tutorial-creating a dynamic click link function from scratch! |”).Link(“name:=”& pName, “Index:=”& pIndex).Click
            End Function

            Now that I have 3 functions that do the same thing, I also need to change their names since we can’t have 3 functions doing different things with the same name. So I will need to do “funClickAnyLinkTitle, funClickAnyLinkHtmlId, funClickAnyLinkName”. Great, now after all of that work, I am ready to call the functions.

            funClickAnyLinkTitle(“title:=z”)

            funClickAnyLinkHtmlId(“title:=z”)

            funClickAnyLinkName( “name:=y”)

            So, I had to write ALL that code just to call 3 functions. Btw, how come you’re forcing me to use index? I don’t need to use index. Html id is always unique in html and i won’t ever need to use index. And, if the properties change, I’m still screwed because I have to go and update every single place where I called that function for that object.

            • QTPtutorialnet

              the thing about that is, there may be hundreds of links in an application. One named “Click me”, one named “Web Link”, one named “Don’t Open” and so on… You can’t spend hours and hours setting up specific values for variables for each of those links. That is usually a great idea for the browser, page, and anything else that repeats over and over. You can still set those variable values and use the variables as the parameters for the clicklink function. The initial function will take 2 seconds to set up, but after calling 4 or 5 links with it, you have a lot less code than just using Browser(name).Page(name).Link(name) those 4 or 5 times and maintenance will be a lot less code.

  • Pingback: SQA(Software Quality Assurance Automation with QTP – easy function to click on any image |()

Pin It on Pinterest

Clef two-factor authentication