QTP/UFT tutorial- Parameterization the simple way 2 (Parameterization Directly in Your Script)

Hi. Last time I showed you how to parameterize by importing an excel sheet into the global sheet within QTP/UFT. You can find the tutorial here. The set up for using excel is a bit longer but it pays off in the longer with little maintainability, and being to modify the values in excel instead of QTP/UFT. Today, I will show you how to parameterize directly within your script with minimum set up. This way takes a lot less effort to set up, but is not as dynamic or as easy to maintain as using an external data sheet.

Example: Let's say that you are testing the login functionality of an application. And let's say that you want to test this login functionality for three separate users. Now instead of creating three separate scripts for each of those users, you can use parameterization to consolidate those three scripts within one.

1. Wrap your full script within a for loop that would loop for the amount of users you would like to test with. In our example, we have 3 separate users, so our loop will loop 3 times.

Dim i
 For i = 1 To 3 Step 1

2. Set up the conditions for the parameterization. Ex. If the loop is on the first loop, then username1, else if the loop is on the second loop, use username2, and if the loop is on the third loop, use username3.

 Dim i
 For i = 1 To 3 Step 1
     If i = 1 Then
        varUserName = "Johnny"
        ElseIf i=2 Then
        varUserName = "Suzie"
        ElseIf i=3 Then
        varUserName = "Fred"
     End If

MsgBox varUserName

3. Now run this script and you will see the same variable will show 3 different usernames. I made this tutorial to show you the simplest way of parameterization. Clearly, your scripts will be a bit more complex, but the idea of parameterization will remain the same. You did it! That's all there is to it. Set up a loop and set the conditions for the various user names you want to test.

Leave your comments and questions below. I love all of your feedback!!

  • KO

    Hello sir,
    I always get error anytime I call a function from your function library like, ” fn_getImage() ” . Could it be that you identified the Browser using index, for I identified my Browser using name.? If that is true, does it mean that to use your function library one has to identify the browser by the index? Please help.

    • QTPtutorialnet

      Sure we can help. Please paste here the function that you are trying to use. THe error message. ANd your line of code that is executing the function. And how you identify the browser.

  • Pingback: QTP/UFT tutorial- Parameterization the simple way 3 (Parameterization Only in Excel) |()

  • KO



    Option Explicit

    Dim wPage,wPage1,wPage2,objImage

    set wPage = Browser(“name:=Welcome: Mercury Tours”).Page(“Title:=.*”)

    set wPage1 = Browser(“name:=Find a Flight: Mercury Tours:”).Page(“Title:=.*”)

    Set wPage2 = Browser(“name:=Select a Flight: Mercury Tours”).Page(“Title:=.*”)

    ‘=====================Main Processing=====================================

    ‘Open new application

    SystemUtil.CloseProcessByName “iexplore.exe”

    SystemUtil.Run “iexplore.exe”, “http://newtours.demoaut.com”


    ‘login to book a flight


    wPage.webEdit(“name:=userName”).set “Fidus”


    wPage.webEdit(“name:=password”).setsecure “5620a4b401233e67f9805759d65d99ba0cf0”

    wPage.Image(“html tag:=INPUT”,”name:=login”).highlight

    wPage.Image(“html tag:=INPUT”,”name:=login”).Click


    ‘validate that you are at the flightfinder page———

    set objImage = fn_getImage (“file name:=mast_flightfinder.gif”)

    ‘If isObject(objImage) Then

    ‘Print”Flightfinder page loaded. Test passed!”


    ‘Print”Flightfinder page did not load. Test failed!”

    ‘End If

    Set objImage = nothing


    Function fn_getImage(strProperties)

    Print “———- fn_getImage ———” & strProperties

    Dim browserPage,obj,strResult

    Set browserPage = browser(strIndex0).Page(strTitleDescr)

    ‘create the object description so that I can use it to access the object

    fn_createDescription “objImage”,strProperties

    Set obj = browserPage.image(objImage)

    ‘If Environment(“debugMode”) Then

    strResult = fn_getObjectExistence(obj, “GetImage_” & fn_removeSpecialCharacters(strProperties), 10, False,””, “”)

    ‘ Else

    ‘strResult = fn_getObjectExistence(obj, “GetImage_” & fn_removeSpecialCharacters(strProperties), 10, False, environment(“ResultsPath”), DataTable.value(“TCID”,TCsheet))

    ‘End If

    If strResult = p Then

    Set fn_getImage = obj


    Set fn_getImage = Nothing

    End If

    End Function


    But it worked perfectly when I used: wPage1.Image(“file name:=mast_flightfinder.gif”).highlight

    wPage1.Image(“file name:=mast_flightfinder.gif”).highlight

    • QTPtutorialnet

      So this is what I see. “Set browserPage = browser(strIndex0).Page(strTitleDescr)” that line, do you have strIndex0 or strTitleDescr defined?

      If not, you can either define those variables appropriately. Or, you can pass in the browser and page description into the function. Try those and let me know if that works.

  • KO

    Sir, strIndex0 or strTitleDescr are defined in the main function library as

    Const strTitleDescr = “title:=.*”
    Const strIndex0 = “index:=0”,

    Also fn_getImage(strProperties) works perfectly when I call it in my homepage ( which has browser index = 0) and only assign image filename as the parameter, but I get the error message when I call fn_getImage(strProperties) in another page( which has the browser index > 0 ).

    I guess making strindex() to be dynamic( thus to take any index number) will solve the problem for me. For now it seems to be const, hence it can only be used when the browser page always has an index = 0.

    Please correct me if I am still mixing a point. and help me overcome this hurdle.

    • QTPtutorialnet

      Yes, seems right. You can either pass in the index, which seems silly because it will almost always be tied to one browser. The other option is to close the first browser if you’re opening a second one and just work on the one that you open so that your index remains the same. Let me know if that helps.

      • QTPtutorialnet

        Yes also, if you have an application where you need to change browser windows often, make the browser index a variable and set that variable in your script. As soon as you transition to another window, you can set the index accordingly. Let me know if that helps.

Pin It on Pinterest

Clef two-factor authentication