This is Part5 of QTP and DotNetFactory series by Saket. Refer Part1, Part2,Part3, Part4

QTP DotNetFactory Part-5

In the earlier parts of this series you learned to create a user form and placing button, text box, check box and radio button control on the form. In this part you will learn how to create a progress bar control on the form.

A progress bar is a control used to convey the progress of a task. Consider a case where you need a continuous progress of your script that is how much part of your script is completed or remaining. So In QTP you can use this control to get the progress status of your script.

You can use this control for other tasks as well in your script like file reading, file transfer etc.

To create the Progress Bar control we will use Set statement and to create the COM interface using CreateInstance we will use ‘System.Windows.Forms.ProgressBar’ as type name from ‘System.Windows.Forms’ assembly

Set MyProgressBar = DotNetFactory.CreateInstance(
"System.Windows.Forms.ProgressBar", "System.Windows.Forms")

To Place this on the form we will use Point object and set the x and y co-ordinates value. And assign to the Location property.

Set Pos = DotNetFactory.CreateInstance(
"System.Drawing.Point", "System.Drawing")

Pos.X = 10
Pos.Y = 20

MyProgressBar.Location = Pos

We need to set some more properties of progress bar control in order to make it functional.

‘Maximum’ property which is used to specify the upper limit of Progress bar value.

‘Minimum’ property which is used to specify the lower limit of Progress bar value.

‘Step’ to specify the amount that each completed task changes the value of progress bar. Step should always be decided based on the upper limit and the number of tasks in operation.

MyProgressBar.Maximum = 100
MyProgressBar.Minimum = 0
MyProgressBar.Step = 20

We will use ‘PerformStep’ method of progress bar to increment the value of progress bar by the amount specified by the ‘Step’ property.

MyProgressBar.PerformStep()

Try this with a for loop to display the progress bar

Example 15

Set MyForm = DotNetFactory.CreateInstance(
"System.Windows.Forms.Form", "System.Windows.Forms")

Set MyProgressBar = DotNetFactory.CreateInstance(
"System.Windows.Forms.ProgressBar", "System.Windows.Forms")

Set Pos = DotNetFactory.CreateInstance(
"System.Drawing.Point", "System.Drawing")

Pos.X = 10
Pos.Y = 20

With MyProgressBar
.Maximum = 100
.Minimum = 0
.Step = 20
.Location = Pos
End with

MyForm.Controls.Add MyProgressBar
MyForm.Show

For i = 0 to 5
MyProgressBar.PerformStep()
Next

MyForm.Close

This gives output as

QTP Progress Bar Control

Let us now see how we can use this control in the script. Consider a script which counts the number of links on a page. The following script can be used to achieve this task.

Open http://www.google.com in a browser and run the code below

Example 16

nLinks=0
Set oDesc = Description.Create()
Set sControls = Browser("name:=Google").
Page("title:=Google").ChildObjects(oDesc)
nTotal = sControls.Count()

For i=0 to nTotal-1
sClass = sControls(i).GetTOProperty("Class Name")

If sClass = "Link" Then
nLinks = nLinks +1
End If

Next

msgbox "Number of Links "& nLinks

Next

msgbox “Number of Links “& nLinks

The script returns the required output as the number of links on the page.

We will now use the progress bar control to determine the progress of the script.

Progress bar properties should always set as per the operation behavior like In the above example we get the Total number of objects as nTotal and loop through each object. so if we take the upper limit of progress bar as 100 as above then the progress bar will not work properly as the value may be more than 100 or less than 100.

In this case we will set nTotal as the upper limit of control

MyProgressBar.Maximum = nTotal

Set the lower limit as 0

MyProgressBar.Minimum = 0

As we are going through each object, Set the step as 1. In the Example 15 we have set it 20 as we have upper limit as 100 and number of task is 5.

MyProgressBar.Step = 1

After each iteration in the loop we will call the ‘PerformStep’ method to increment the progress bar value

Combining it all together

Example 17

Set MyForm = DotNetFactory.CreateInstance("
System.Windows.Forms.Form", "System.Windows.Forms")

Set MyProgressBar = DotNetFactory.CreateInstance("
System.Windows.Forms.ProgressBar", "System.Windows.Forms")

Set Pos = DotNetFactory.CreateInstance("
System.Drawing.Point", "System.Drawing")

Pos.X = 10
Pos.Y = 20

MyProgressBar.Location = Pos
MyForm.Controls.Add MyProgressBar

MyForm.Show
nLinks=0

Set oDesc = Description.Create()
Set sControls = Browser("name:=Google").
Page("title:=Google").ChildObjects(oDesc)

nTotal = sControls.Count()
MyProgressBar.Maximum = nTotal
MyProgressBar.Minimum = 0
MyProgressBar.Step = 1

For i=0 to nTotal-1
sClass = sControls(i).GetTOProperty("Class Name")
If sClass = "Link" Then
nLinks = nLinks +1
End If

MyProgressBar.PerformStep()
Next

MyForm.Close
msgbox "Number of Links "& nLinks

This will give you the output as:

Progress Bar Control QTP

As per the second last statement in code above, the form will close and the result of script will be:

Total No. of Links Google QTP

Want to share an article with the thousands of LearnQTP readers?. Contact Us