Save time and effort on your scripts with this simple step using QTP/UFT

Hi. Today is a great day to go over a quick technique that will save you hours of time on creating your scripts! As an example, I will use a simple script I created quickly for our website qtptutorial.net. Follow along so you can grasp the concept. If you paste this code in your QTP/UFT action window, you will notice that the script does the following:

1. QTP/UFT closes all your browsers,

2. QTP/UFT navigates to QTPtutorial.net

3. QTP/UFT clicks on "Start Learning"

4.  QTP/UFT clicks on "Free Registration"

5. QTP/UFT clicks on "Free Access".

Example1:
Systemutil.CloseProcessByName "iexplore.exe"
Systemutil.CloseProcessByName "firefox.exe"
Systemutil.CloseProcessByName "chrome.exe"

Systemutil.Run "iexplore.exe", "www.qtptutorial.net"
Browser("name:=.*").Page("title:=.*").Link("name:=Start Learning", "Index:=1").Click
Browser("name:=.*").Page("title:=.*").Link("name:=Free Registration", "Index:=1").Click
Browser("name:=.*").Page("title:=.*").Link("name:=Free Access", "Index:=1").Click


 

If you notice, we have a lot of repetitive code highlighted in blue. Let's take this repetitive code and make it more readable, and less time consuming to create.

1. Let's create an object for all of the repetitive lines Browser("name:=.*").Page("title:=.*") This object will represent a container that holds the code that we place in it.  

Set objBrowserPage = Browser("name:=.*").Page("title:=.*")

2. Now call the object by using it's name objBrowserPage, and click the links directly through this object. See example below.

objBrowserPage.Link("name:=Start Learning", "Index:=1").Click
objBrowserPage.Link("name:=Free Registration", "Index:=1").Click
objBrowserPage.Link("name:=Free Access", "Index:=1").Click

3. Now add this changes into the script and it should look like the code below!

Final Script:

Set objBrowserPage = Browser("name:=.*").Page("title:=.*")

Systemutil.CloseProcessByName "iexplore.exe"
Systemutil.CloseProcessByName "firefox.exe"
Systemutil.CloseProcessByName "chrome.exe"

Systemutil.Run "iexplore.exe", "www.qtptutorial.net"

objBrowserPage.Link("name:=Start Learning", "Index:=1").Click
objBrowserPage.Link("name:=Free Registration", "Index:=1").Click
objBrowserPage.Link("name:=Free Access", "Index:=1").Click

It's that simple. Now your code looks a lot cleaner, and it's going to save you a lot of time! You are one step closer to becoming an automation guru!

Pin It on Pinterest

Clef two-factor authentication